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

@cqrs-toolkit/realtime

v0.1.0

Published

Canonical WebSocket message protocol types for the CQRS realtime gateway

Downloads

132

Readme

@cqrs-toolkit/realtime

Canonical WebSocket message protocol types and serialization helpers for CQRS real-time event streaming.

Defines a topic-based pub/sub protocol between clients and servers using discriminated unions for type-safe message handling. Parsing is defensive — malformed input returns undefined rather than throwing.

Install

npm install @cqrs-toolkit/realtime

Protocol Overview

Client → Server         Server → Client
─────────────────       ───────────────────────
subscribe               connected
unsubscribe             subscribed
                        unsubscribed
                        subscription_denied
                        subscription_revoked
                        event
                        heartbeat

Usage

Client Side

Parse incoming server messages and send subscriptions:

import { parseServerMessage, serializeClientMessage } from '@cqrs-toolkit/realtime'

// Parse incoming message
const message = parseServerMessage(wsEvent.data)
if (!message) return // malformed — ignore

switch (message.type) {
  case 'connected':
    // Send subscriptions after connection is established
    ws.send(
      serializeClientMessage({
        type: 'subscribe',
        topics: ['Todo:*', 'Note:*'],
      }),
    )
    break
  case 'event':
    // message.event: ISerializedEvent
    // message.aggregateType: string
    // message.service: string
    // message.topics: string[]
    handleEvent(message)
    break
  case 'heartbeat':
    reportConnectivity()
    break
  case 'subscribed':
  case 'unsubscribed':
    break
  case 'subscription_denied':
  case 'subscription_revoked':
    console.warn(message.message)
    break
}

Server Side

Parse client requests and push events:

import {
  parseClientMessage,
  serializeServerMessage,
  type ServerMessage,
} from '@cqrs-toolkit/realtime'

// Acknowledge connection
socket.send(
  serializeServerMessage({
    type: 'connected',
    heartbeatInterval: 30000,
    userId: 'user-1',
    expiresAtMs: null,
  }),
)

// Handle client messages
const msg = parseClientMessage(raw)
if (!msg) return

switch (msg.type) {
  case 'subscribe':
    registerTopics(socket, msg.topics)
    socket.send(serializeServerMessage({ type: 'subscribed', topics: msg.topics }))
    break
  case 'unsubscribe':
    removeTopics(socket, msg.topics)
    socket.send(serializeServerMessage({ type: 'unsubscribed', topics: msg.topics }))
    break
}

// Push an event to matching subscribers
socket.send(
  serializeServerMessage({
    type: 'event',
    topics: ['Todo:abc123'],
    service: 'task-service',
    aggregateType: 'Todo',
    event: serializedEvent,
  }),
)

Message Types

Topic strings are opaque from this package's perspective — the server defines the topic format and matching semantics.

Server → Client

| Type | Key Fields | | ---------------------- | --------------------------------------------- | | connected | heartbeatInterval, userId, expiresAtMs | | event | topics, service, aggregateType, event | | subscribed | topics | | unsubscribed | topics | | subscription_denied | topics, message | | subscription_revoked | topics, message | | heartbeat | (none) |

Client → Server

| Type | Key Fields | | ------------- | ---------- | | subscribe | topics | | unsubscribe | topics |

Serialization Helpers

| Function | Direction | Description | | ------------------------ | ------------- | ------------------------------------------ | | serializeServerMessage | Server → wire | Serialize ServerMessage to JSON string | | parseClientMessage | Wire → server | Parse JSON string, strict field validation | | parseServerMessage | Wire → client | Parse JSON string, discriminant check only | | serializeClientMessage | Client → wire | Serialize ClientMessage to JSON string |

Client-to-server parsing validates all required fields (type, topics array, string elements). Server-to-client parsing validates only the type discriminant — the client trusts the server's format.

API Reference

Full API documentation is generated from source and available at docs/api.

License

MIT