npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@shelfjs/nats

v1.3.3

Published

NestJS does not provide full NATS support in their framework. Let's create our own :)

Readme

NestJS NATS

NestJS does not provide full NATS support in their framework. Let's create our own :)

Installation

Install @shelfjs/nats library

npm i --save @shelfjs/nats

Features

  • Built above nats.js

  • Nats Core support

  • Nats JetStream support

  • Multi-connections

  • Decorators (ex. @Consume, @Reply)

Usage/Examples

Import NatsModule in your AppModule. NatsModule is a global module.

import { NatsModule } from '@shelfjs/nats'
import { HealthChecksModule } from '@shelfjs/health-checks'
import * as env from 'env-var'

@Module({
    imports: [
        HealthChecksModule, // (optional) import HealthChecksModule to get NATS health status
        NatsModule.forRoot({
            connections: [
                {
                    connectionName: 'main-nats' // optional
                    servers: env.get('NATS_URL').required().asString(),
                    /* all nats.js options here */
                }
            ]
        }),
    ]
})
export class AppModule {}

After that, you can use NATS client services in your modules.

Nats Request/Reply

Inject NatsClientService for access to NATS Core features. Use @Controller() class decorator for @Reply() method decorator

import { Controller } from '@nestjs/common'
import {
    NatsClientService,
    Reply,
    ReplyPayload,
    ReplyResponse,
} from '@shelfjs/nats'

@Controller()
export class CatController {
    constructor(
        private readonly natsClient: NatsClientService,
    ) {}

    async onApplicationBootstrap() {
        // request ping 
        await this.natsClient.request('ping', { text: 'hello' })
    }

    // Reply for ping request
    @Reply('ping')
    async ping(payload: ReplyPayload<any>): Promise<ReplyResponse<string>> {
        const response = `pong with message ${JSON.stringify(payload.data)}`

        const responseHeaders = {
            ping: 'pong',
        }

        return {
            data: response,
            headers: responseHeaders,
        }
    }
}

JetStream Publish/Subscribe

Inject NatsJetStreamClientService for access to NATS JetStream features. Use @Controller() class decorator for @Consume() method decorator

import { Controller, OnModuleInit } from '@nestjs/common'
import {
    Consume,
    ConsumePayload,
    ConsumerAcks,
    NatsJetStreamClientService,
    PublishOptions,
} from '@shelfjs/nats'
import { AckPolicy, PubAck, RetentionPolicy, StorageType } from 'nats'

@Controller()
export class CatController {
    constructor(
        private readonly natsJetStreamClient: NatsJetStreamClientService,
    ) {}

    // create stream
    async onApplicationBootstrap() {
        await this.natsJetStreamClient.createStream({
            autoupdate: true,
            name: 'stream_name',
            retention: RetentionPolicy.Limits,
            storage: StorageType.File,
            subjects: Object.values(SampleNatsJetStreamSubjectsEnum),
        })

        // publish a message
        await this.natsJetStreamClient.publish('ping', 'pong', { timeout: 5000 })
    }

    // consume message from stream
    @Consume({
        stream: 'stream_name',
        consumer: {
            durable_name: 'sample-consumer',
            filter_subject: 'ping',
            ack_policy: AckPolicy.All,
        }
    })
    async consumePing(payload: ConsumePayload<any>, acks: ConsumerAcks) {
        try {
            // do logic here
            acks.ack()
        } catch (error) {
            console.error(error)
            acks.nack()
        }
    }
}

Multi-connections

Multi-connections allows you to connect to multiple NATS connection (it's not about clusters). For example, you have 2 different applications that use different NATS instances. You can connect to them using connections option.

import { NatsModule } from '@shelfjs/nats'
import { HealthChecksModule } from '@shelfjs/health-checks'
import * as env from 'env-var'

@Module({
    imports: [
        HealthChecksModule, // (optional) import HealthChecksModule to get NATS health status
        NatsModule.forRoot({
            connections: [
                {
                    connectionName: '1-nats'
                    servers: env.get('CAT_APP_NATS_URL').required().asString(),
                    /* all nats.js options here */
                },
                {
                    connectionName: '2-nats'
                    servers: env.get('DOG_APP_NATS_URL').required().asString(),
                    /* all nats.js options here */
                }
            ]
        }),
    ]
})
export class AppModule {}

Add optional connectionName option to Reply, Request, Consume, Publish methods

import { Controller } from '@nestjs/common'
import {
    NatsClientService,
    Reply,
    ReplyPayload,
    ReplyResponse,
} from '@shelfjs/nats'

@Controller()
export class CatController {
    constructor(
        private readonly natsClient: NatsClientService,
    ) {}

    async onApplicationBootstrap() {
        // request ping 
        await this.natsClient.request('ping', { text: 'hello' }, {}, '1-nats') // <-- Use connection name here
    }

    // Reply for ping request
    @Reply('ping', '1-nats') // <-- Use connection name here
    async ping(payload: ReplyPayload<any>): Promise<ReplyResponse<string>> {
        const response = `pong with message ${JSON.stringify(payload.data)}`

        const responseHeaders = {
            ping: 'pong',
        }

        return {
            data: response,
            headers: responseHeaders,
        }
    }
}