Files
WRNexusJSDoc/app/pages/packages/pubsub.wrn
T
2026-07-12 16:14:06 +05:30

138 lines
10 KiB
Plaintext

page wrnexuspubsub {
seo {
title = "@wrnexus/pubsub"
description = "In-process and Redis-backed publish/subscribe."
}
view {
<div class="docs-shell">
<header class="topbar">
<a class="brand" href="/"><span>W</span> WRNexusJS</a>
<nav><a href="/getting-started">Get started</a><a href="/packages">Packages</a><a href="/language">Language</a><a href="/architecture">Architecture</a></nav>
<button data-wire-theme-toggle class="theme-button" aria-label="Toggle theme">Theme</button>
</header>
<main class="page package-page">
<aside class="sidebar"><a href="/packages">← All packages</a><span class="category">Realtime</span><h1>@wrnexus/pubsub</h1><p>In-process and Redis-backed publish/subscribe.</p><code>bun add @wrnexus/pubsub@0.2.12</code><nav><a href="#guide">Guide</a><a href="#api">Complete API</a></nav></aside>
<article class="documentation"><section class="doc-intro"><span class="eyebrow">Realtime</span><h1>@wrnexus/pubsub</h1><p>In-process and Redis-backed publish/subscribe.</p><pre><code>bun add @wrnexus/pubsub@0.2.12</code></pre></section><section id="guide" class="prose"><blockquote>Topic-based publish/subscribe with a pluggable driver — in-process by default, Redis for cross-process messaging.</blockquote>
<p>Part of the <strong>WRNexusJS</strong> framework — an SSR-first, Bun-native full-stack web framework.</p>
<h3 id="overview">Overview</h3>
<p><code>@wrnexus/pubsub</code> is a small server-side pub/sub bus. You publish messages to a topic and subscribe with topic patterns; handlers fire for matching topics. The default driver keeps everything in-process, and you can swap in the Redis driver (<code>@wrnexus/pubsub/redis</code>) to fan messages out across processes or hosts. It also backs <code>@wrnexus/core</code>'s realtime bridge for horizontal scaling.</p>
<h3 id="installation">Installation</h3>
<pre data-language="bash"><code>bun add @wrnexus/pubsub</code></pre>
<blockquote>Private package — the machine must be authenticated to the <code>wrnexus</code> npm org</blockquote>
<blockquote>(a read token in <code>~/.npmrc</code>). Requires <strong>Bun</strong> (Node is not supported).</blockquote>
<h3 id="api">API</h3>
<h4 id="createpubsub-driver-pubsub"><code>createPubSub(driver?): PubSub</code></h4>
<p>Creates a bus over a driver. Defaults to <code>memoryDriver()</code> (in-process).</p>
<pre data-language="ts"><code>interface PubSub &#123;
publish&lt;T = unknown&gt;(topic: string, message: T): Promise&lt;void&gt;;
subscribe&lt;T = unknown&gt;(pattern: string, handler: Handler&lt;T&gt;): () =&gt; void;
&#125;
type Handler&lt;T = unknown&gt; = (message: T, topic: string) =&gt; void | Promise&lt;void&gt;;</code></pre>
<ul>
<li><code>publish(topic, message)</code> — resolves once the driver has dispatched the message.</li>
<li><code>subscribe(pattern, handler)</code> — returns an unsubscribe function.</li>
</ul>
<h4 id="pattern-matching">Pattern matching</h4>
<p>Subscription patterns match in three ways:</p>
<ul>
<li><strong>Exact</strong> — <code>&quot;order:created&quot;</code> matches only that topic.</li>
<li><strong>Prefix</strong> — <code>&quot;order:*&quot;</code> matches any topic starting with <code>&quot;order:&quot;</code>.</li>
<li><strong>Everything</strong> — <code>&quot;*&quot;</code> matches all topics.</li>
</ul>
<h4 id="memorydriver-pubsubdriver"><code>memoryDriver(): PubSubDriver</code></h4>
<p>The default in-process driver. Handlers are invoked synchronously (fire-and-forget for async handlers) whenever a published topic matches a registered pattern.</p>
<pre data-language="ts"><code>interface PubSubDriver &#123;
publish(topic: string, message: unknown): void | Promise&lt;void&gt;;
subscribe(pattern: string, handler: Handler): () =&gt; void;
&#125;</code></pre>
<h4 id="wrnexus-pubsub-redis-redisdriver-url"><code>@wrnexus/pubsub/redis</code> — <code>redisDriver(url?)</code></h4>
<p>A cross-process driver backed by Redis. It speaks RESP over a raw TCP socket via <code>Bun.connect</code>, so it adds <strong>no npm dependency</strong>. <code>url</code> defaults to <code>$REDIS_URL</code>, then <code>redis://localhost:6379</code>. The URL may carry a password and a database index (e.g. <code>redis://:secret@host:6379/2</code>).</p>
<pre data-language="ts"><code>function redisDriver(url?: string): PubSubDriver &amp; &#123; close(): void &#125;;</code></pre>
<ul>
<li>Exact topics use Redis <code>SUBSCRIBE</code>; wildcard patterns (<code>ns:*</code>, <code>*</code>) use</li>
<p><code>PSUBSCRIBE</code>, whose glob semantics line up with this library's matching.</p>
<li>Messages are JSON-stringified on publish and <code>JSON.parse</code>d on receipt; a payload</li>
<p>that isn't valid JSON is delivered as the raw string.</p>
<li><code>close()</code> tears down both the subscriber and publisher connections.</li>
</ul>
<h4 id="resp-codec-internal">RESP codec (internal)</h4>
<p><code>redis.ts</code> uses a minimal RESP implementation exported from <code>resp.ts</code> (<code>encodeCommand</code>, <code>parseReply</code>, <code>concat</code>, and the <code>RespValue</code> type). These are implementation details of the Redis driver, not part of the public package entry.</p>
<h3 id="usage">Usage</h3>
<p>In-process (default):</p>
<pre data-language="ts"><code>import &#123; createPubSub &#125; from &quot;@wrnexus/pubsub&quot;;
const bus = createPubSub();
const off = bus.subscribe(&quot;order:*&quot;, (msg, topic) =&gt; &#123;
console.log(topic, msg);
&#125;);
await bus.publish(&quot;order:created&quot;, &#123; id: 7 &#125;);
off(); // unsubscribe</code></pre>
<p>Cross-process with Redis:</p>
<pre data-language="ts"><code>import &#123; createPubSub &#125; from &quot;@wrnexus/pubsub&quot;;
import &#123; redisDriver &#125; from &quot;@wrnexus/pubsub/redis&quot;;
const driver = redisDriver(&quot;redis://localhost:6379&quot;);
const bus = createPubSub(driver);
bus.subscribe(&quot;order:*&quot;, (msg, topic) =&gt; &#123;
// received on any app process subscribed to this pattern
&#125;);
await bus.publish(&quot;order:created&quot;, &#123; id: 7 &#125;);
// on shutdown
driver.close();</code></pre>
<h3 id="requirements-notes">Requirements / Notes</h3>
<ul>
<li><strong>Bun-only.</strong> The Redis driver depends on <code>Bun.connect</code>; it throws</li>
<p><code>redisDriver requires the Bun runtime (Bun.connect).</code> outside Bun. The default in-memory driver has no runtime dependencies.</p>
<li>The Redis driver reads <code>REDIS_URL</code> from the environment when no <code>url</code> is passed.</li>
<li>Backs [<code>@wrnexus/core</code>](../core)'s realtime bridge for horizontal scaling.</li>
<li>No external npm dependencies — the Redis client is a self-contained RESP codec.</li>
</ul></section><section id="api" class="prose api"><h2>Complete TypeScript API</h2><p>This declaration is generated from the exact published package and lists its exported functions, classes, interfaces, and types.</p><pre data-language="typescript"><code>/**
* @wrnexus/pubsub — topic-based publish/subscribe with a pluggable driver.
* The default is in-process; swap in a Redis/NATS driver for cross-instance
* messaging (it also backs @wrnexus/core's realtime bridge).
*
* const bus = createPubSub();
* const off = bus.subscribe(&quot;order:*&quot;, (msg, topic) =&gt; &#123;...&#125;);
* await bus.publish(&quot;order:created&quot;, &#123; id: 7 &#125;);
*
* Subscriptions match exact topics, &quot;ns:*&quot; prefixes, and &quot;*&quot; (everything).
*/
type Handler&lt;T = unknown&gt; = (message: T, topic: string) =&gt; void | Promise&lt;void&gt;;
interface PubSubDriver &#123;
publish(topic: string, message: unknown): void | Promise&lt;void&gt;;
subscribe(pattern: string, handler: Handler): () =&gt; void;
&#125;
interface PubSub &#123;
publish&lt;T = unknown&gt;(topic: string, message: T): Promise&lt;void&gt;;
subscribe&lt;T = unknown&gt;(pattern: string, handler: Handler&lt;T&gt;): () =&gt; void;
&#125;
/** In-process pub/sub driver (default). */
declare function memoryDriver(): PubSubDriver;
/** Create a pub/sub bus over a driver (in-memory by default). */
declare function createPubSub(driver?: PubSubDriver): PubSub;
export &#123; type Handler, type PubSub, type PubSubDriver, createPubSub, memoryDriver &#125;;
</code></pre></section><section id="examples" class="prose examples"><h2>Examples</h2><p>Copy-ready examples taken from this package's published documentation.</p><div class="example-grid"><article class="example-card"><h3>Example 1</h3><pre data-language="bash"><code>bun add @wrnexus/pubsub</code></pre></article><article class="example-card"><h3>Example 2</h3><pre data-language="ts"><code>interface PubSub &#123;
publish&lt;T = unknown&gt;(topic: string, message: T): Promise&lt;void&gt;;
subscribe&lt;T = unknown&gt;(pattern: string, handler: Handler&lt;T&gt;): () =&gt; void;
&#125;
type Handler&lt;T = unknown&gt; = (message: T, topic: string) =&gt; void | Promise&lt;void&gt;;</code></pre></article><article class="example-card"><h3>Example 3</h3><pre data-language="ts"><code>interface PubSubDriver &#123;
publish(topic: string, message: unknown): void | Promise&lt;void&gt;;
subscribe(pattern: string, handler: Handler): () =&gt; void;
&#125;</code></pre></article><article class="example-card"><h3>Example 4</h3><pre data-language="ts"><code>function redisDriver(url?: string): PubSubDriver &amp; &#123; close(): void &#125;;</code></pre></article></div></section></article>
<aside class="on-this-page"><h2>On this page</h2><nav><a class="toc-level-2" href="#guide">Guide</a><a class="toc-level-3" href="#overview">Overview</a><a class="toc-level-3" href="#installation">Installation</a><a class="toc-level-3" href="#api">API</a><a class="toc-level-4" href="#createpubsub-driver-pubsub">createPubSub(driver?): PubSub</a><a class="toc-level-4" href="#pattern-matching">Pattern matching</a><a class="toc-level-4" href="#memorydriver-pubsubdriver">memoryDriver(): PubSubDriver</a><a class="toc-level-4" href="#wrnexus-pubsub-redis-redisdriver-url">@wrnexus/pubsub/redis — redisDriver(url?)</a><a class="toc-level-4" href="#resp-codec-internal">RESP codec (internal)</a><a class="toc-level-3" href="#usage">Usage</a><a class="toc-level-3" href="#requirements-notes">Requirements / Notes</a><a class="toc-level-2" href="#api">Complete API</a><a class="toc-level-2" href="#examples">Examples</a></nav></aside>
</main>
<footer>WRNexusJS 0.2.12 · SSR-first · Bun-native · Documentation generated from published package APIs.</footer>
</div>
}
}