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

nestjs-pubnub

v6.0.0

Published

PubNub realtime messaging integration for NestJS

Readme

nestjs-pubnub

npm version npm monthly downloads CI

An injectable PubNub client for NestJS. PubNubService extends the SDK client, so publishing and subscription APIs remain available.

Install it in your NestJS application:

pnpm add nestjs-pubnub pubnub

Requires Node.js 24 or newer, NestJS 12 and PubNub 13.0.3 or a later 13.x release. Both ESM and CommonJS are supported, including strict consumers that set skipLibCheck: false.

Register

import { Module } from "@nestjs/common";
import { PubNubModule } from "nestjs-pubnub";

@Module({
  imports: [
    PubNubModule.register({
      userId: "notification-service",
      subscribeKey: process.env.PUBNUB_SUBSCRIBE_KEY!,
      publishKey: process.env.PUBNUB_PUBLISH_KEY!,
    }),
  ],
})
export class NotificationsModule {}

Set both key environment variables before starting the application. Use a stable userId appropriate to the identity of your application.

Configure asynchronously

With @nestjs/config installed, place this in your module's imports:

import { ConfigModule, ConfigService } from "@nestjs/config";
import { PubNubModule } from "nestjs-pubnub";

PubNubModule.registerAsync({
  imports: [ConfigModule.forRoot()],
  inject: [ConfigService],
  useFactory: (config: ConfigService) => ({
    userId: config.getOrThrow<string>("PUBNUB_USER_ID"),
    subscribeKey: config.getOrThrow<string>("PUBNUB_SUBSCRIBE_KEY"),
    publishKey: config.getOrThrow<string>("PUBNUB_PUBLISH_KEY"),
  }),
});

Multiple clients

Give each registration a unique alias and inject the matching client. alias and global are Nest module settings: place them beside useFactory for async registration, not inside its returned PubNub SDK configuration. For synchronous registration, include them alongside the SDK options in the same object.

import { Inject, Injectable, Module } from "@nestjs/common";
import {
  getPubNubClientToken,
  PubNubModule,
  PubNubService,
} from "nestjs-pubnub";

@Injectable()
class AccountNotifications {
  constructor(
    @Inject(getPubNubClientToken("primary")) readonly primary: PubNubService,
    @Inject(getPubNubClientToken("secondary"))
    readonly secondary: PubNubService,
  ) {}
}

@Module({
  imports: [
    PubNubModule.register({
      alias: "primary",
      userId: "primary-notifications",
      subscribeKey: process.env.PRIMARY_SUBSCRIBE_KEY!,
      publishKey: process.env.PRIMARY_PUBLISH_KEY!,
    }),
    PubNubModule.registerAsync({
      alias: "secondary",
      useFactory: () => ({
        userId: "secondary-notifications",
        subscribeKey: process.env.SECONDARY_SUBSCRIBE_KEY!,
        publishKey: process.env.SECONDARY_PUBLISH_KEY!,
      }),
    }),
  ],
  providers: [AccountNotifications],
})
export class MultiAccountModule {}

Each registration owns its SDK configuration, client and shutdown hook. SDK options such as origin remain available per registration. getPubNubClientToken(alias) supports custom provider factories and testing. Named registrations export only their named token; omit alias (or use "" or the reserved "default" alias) to retain ordinary PubNubService injection. Other aliases are exact, opaque names; use a unique name for each registration. The module stays local unless global: true is requested.

Major release note

Package-specific injection decorators have been removed; use Nest's @Inject(getPubNubClientToken(alias)). Existing single-client PubNubService injection continues to work. Applications registering multiple clients should assign distinct aliases and use @Inject(getPubNubClientToken(alias)) instead of relying on import order to select a class provider.

Publish a message

Register this service in the module that imports PubNubModule:

import { Injectable } from "@nestjs/common";
import { PubNubService } from "nestjs-pubnub";

@Injectable()
export class NotificationsService {
  constructor(private readonly pubnub: PubNubService) {}

  notify(channel: string, text: string) {
    return this.pubnub.publish({ channel, message: { text } });
  }
}

The module closes its SDK client on destruction. Call app.enableShutdownHooks() during Nest bootstrap if shutdown signals should trigger that lifecycle hook.

Module options token

getPubNubOptionsToken() returns the module-local configuration token for custom providers inside a registration. It takes no alias: each registration owns its options provider. The options provider is not exported to parent or importing modules; use this helper only for providers added inside that registration or for registration-local tests. The generated builder token is private and is not exported from the package entry point.

Contributing