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

event-router-client

v1.0.1

Published

Client for Event Router

Readme

event-router-client

TypeScript client for the Event Router.
Supports publish, subscribe (callback + async iterator), auto-reconnect, and clean close using AbortController.

Install

npm i event-router-client
# if local file:
# import { EventRouterClient } from './event-router-client'

Node ≥ 18 recommended.

Quick start

import { EventRouterClient } from 'event-router-client';

const bus = new EventRouterClient({ host: '127.0.0.1', port: 4040 });

// Subscribe (callback style)
const orders = bus.subscribe({ source: ['app.orders'], detail: { status: [{ prefix: 'created' }] } });
const off = orders.on(e => console.log('got:', e));

// Subscribe (async iterator style)
(async () => {
  let seen = 0;
  for await (const e of bus.subscribe({ 'detail-type': ['order'] })) {
    console.log('iter:', e);
    if (++seen === 2) break;
  }
})();

// Publish
await bus.publish({
  source: 'app.orders',
  'detail-type': 'order',
  detail: { status: 'created.v1', id: 'o-1' },
});

// Unsubscribe / close
off();           // remove this callback
orders.close();  // cancel the subscription
await bus.close(); // close socket, reject pending publishes

API

new EventRouterClient(opts?: { host?: string; port?: number; backoffMaxMs?: number; })

  • host (default 127.0.0.1)
  • port (default 4040)
  • backoffMaxMs — reconnection cap (default 5000)

Auto-connects on first publish() or subscribe() and reconnects on drop.

publish<T extends object>(event: T): Promise<number>

Sends an event to the bus; resolves with delivered (number of matching subscriptions across all connected clients).
Rejects on ack timeout or if the client is closed.

subscribe<T = any>(pattern: object): Subscription<T>

Creates a server-side subscription (unique subId) and returns a Subscription.

Subscription<T>:

  • subId: string
  • pattern: object
  • on((event: T) => void): () => void — add a callback; returns an unsubscriber.
  • close(): void — cancels server subscription and wakes any pending iterator calls.
  • closed: boolean
  • [Symbol.asyncIterator](): AsyncIterator<T> — iterate events:
for await (const e of sub) {
  // ...
}

The iterator stops when:

  • sub.close() is called (internally uses an AbortController to wake the waiter), or
  • the client is closed.

close(): Promise<void>

Closes the TCP connection, rejects all in-flight publish() promises with Error('client closed'), and force-closes all subscriptions.

Behavior details

  • Reconnect: exponential backoff (×1.8, capped at backoffMaxMs). On reconnect, the client re-subscribes all open subscriptions.
  • Backpressure: the client writes length-prefixed frames; if the kernel send buffer fills, it waits for 'drain'.
  • Unsubscribe: best-effort if disconnected. Server clears subs on socket close.
  • Delivery semantics: at-most-once to each live subscription. No persistence/replay.
  • IDs: the server assigns event.id if missing; you can set your own for dedupe downstream if you need it.

Pattern cheatsheet

Same as the server’s supported subset:

// matches 'created.*'
{ detail: { status: [{ prefix: 'created' }] } }

// numeric ranges
{ detail: { total: [{ numeric: { '>=': 10, '<': 100 } }] } }

// anything-but (array)
{ region: [{ 'anything-but': ['dev', 'test'] }] }

// cidr on string field
{ clientIp: [{ cidr: '10.0.0.0/8' }] }

// wildcard (picomatch)
{ source: [{ wildcard: 'app.*' }] }

// equals-ignore-case
{ env: [{ 'equals-ignore-case': 'PROD' }] }

Troubleshooting

  • No events? Ensure your pattern keys match event shape; unknown keys never match.
  • Iterator hangs on shutdown? Call sub.close() or client.close() — both wake the iterator via AbortController.
  • Ack timeout in publish()? Server not reachable or overloaded; increase server CPU, split topics, or batch.