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 🙏

© 2025 – Pkg Stats / Ryan Hefner

valkey-pubsub

v1.0.4

Published

Provides a Valkey-based publish-subscribe mechanism for decoupled communication between components. Provides support for Mercurius subscriptions using Valkey.

Downloads

4

Readme

valkey-pubsub

A lightweight, TypeScript-native Pub/Sub implementation designed for use with Mercurius GraphQL, built on top of Valkey (Redis-compatible).

Supports broadcasting messages to multiple listeners and enables easy event-driven design for GraphQL subscriptions or internal messaging systems.


Features

  • 🔁 Broadcast-style delivery: All listeners receive each message
  • Fast and simple API: Push, subscribe, destroy
  • Compatible with Mercurius PubSub interface
  • 🧪 Tested with Jest
  • 🧱 Type-safe and modular — written in pure TypeScript

Installation

pnpm add valkey-pubsub

Usage

Create the PubSub instance

import ValkeyPubSub, { PubSubGenericQueue } from "valkey-pubsub";

const pubsub = await ValkeyPubSub.create({
  addresses: [{ host: "localhost", port: 6379 }],
  clusterMode: false,
});

Subscribe to a topic

const queue = new PubSubGenericQueue<string>();

await pubsub.subscribe("my-topic", queue);

queue.onItem((message) => {
  console.log("Received:", message);
});

Publish a message

await pubsub.publish({
  topic: "my-topic",
  payload: { hello: "world" },
});

Usage with Mercurius

The original/driving force for the design was the subscription models for Mercurius

const pubsub = await ValkeyPubSub.create();

fastify.decorate("pubsub", pubsub);

fastify.register(mercurius, {
  schema,
  resolvers: await resolvers,
  loaders: await loaders,
  context: async (request: FastifyRequest) => {
    return {
      request,
      db: server.db,
      valkey: server.valkey,
      pubsub: server.pubsub,
      logger: server.log,
    } as ServerDecorators;
  },
  subscription: {
    context: async (_server, request) => {
      return {
        request,
        db: server.db,
        valkey: server.valkey,
        pubsub: server.pubsub,
        logger: server.log,
      } as ServerDecorators;
    },
    pubsub: server.pubsub,
  },
  graphiql: false, // ℹ️ cannot use in place with helmet
  allowBatchedQueries: true,
  path: "/graphql", // 👈 Restricts GraphQL to this endpoint
  prefix: "/",
});

API

ValkeyPubSub

ValkeyPubSub.create(config): Promise<ValkeyPubSub>

Creates a PubSub instance.

Config options:

  • addresses: Array of { host, port } objects (defaults to Valkey on localhost:6379)
  • protocol: 'RESP2' or 'RESP3' (default: 'RESP3')
  • clusterMode: true or false (default: false)

pubsub.subscribe(topic, queue)

  • Subscribes a queue to a topic. Every published message will be pushed to the queue and delivered to all its listeners.

pubsub.publish({ topic, payload }, callback?)

  • Publishes a message to the specified topic. Payload will be stringified before being sent.

pubsub.cleanup()

  • Cleans up all open connections and subscriptions.

PubSubGenericQueue

PubSubGenericQueue<T>

A generic in-memory delivery queue that stores items and allows multiple listeners.

Methods:

  • push(value: T): Pushes a value onto the queue
  • onItem(callback: (value: T) => void): Registers a callback for new items
  • isEmpty(): Returns true if the queue is empty
  • size(): Returns the current number of pending items
  • destroy(): Destroys the queue and runs any registered close callbacks

License

MIT © Chris Schuld

Contact

  • Email - twitter handle @ gmail.com
  • X - @cbschuld

Contributing

Yes, thank you! Please update the docs and tests and add your name to the package.json file.