Files
skills/public/realtime-pubsub-patterns.html
zeekayandHanzo Dev 9e10c1c10e Each llms index points at the other two
hanzoskills.com/llms.txt now opens with the install line, the API
reference index and the company index, so an agent that lands on any
one of the three can find the rest.

robots.txt claimed 537 documents while llms.txt counts 920. The number
is gone from robots.txt; llms.txt derives it.

public/ is regenerated from scripts/site.py, which it had fallen
behind: the copy-prompt menu and agent bar were in the generator and
not in the committed pages. tests/site.mjs passes against the result.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-11 00:27:59 -07:00

804 lines
32 KiB
HTML

<!doctype html>
<html lang="en"><head>
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>realtime-pubsub-patterns</title>
<meta name="color-scheme" content="dark">
<style>
/* Committed to one visual world: a dark instrument panel. No light variant —
the dot field and the hairline rules only read on black, and a naive
inversion would wash both out. Every colour is painted explicitly so the page
never borrows a host background. */
@font-face{font-family:'Zen';src:url('/fonts/Zen-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
@font-face{font-family:'Zen';src:url('/fonts/Zen-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
@font-face{font-family:'Zen Pixel Square';src:url('/fonts/ZenPixel-Square.woff2') format('woff2');font-style:normal;font-display:swap}
:root{
--ink:#f4f4f2; --soft:#9c9c98; --faint:#6a6a66; --ground:#000; --panel:#0a0a0a;
--line:#232323; --line-hi:#3a3a3a; --accent:#f4f4f2;
--font-zen-sans:'Zen',-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif;
--font-zen-mono:'Zen Mono',ui-monospace,"SF Mono",SFMono-Regular,Menlo,Consolas,monospace;
--font-zen-pixel-square:'Zen Pixel Square',monospace;
--mono:var(--font-zen-mono);
--sans:var(--font-zen-sans);
/* Controls are pills and surfaces are soft, as on hanzo.ai. */
--r:999px; --r-soft:14px;
}
*{box-sizing:border-box}
html{-webkit-text-size-adjust:100%}
body{margin:0;background:var(--ground);color:var(--ink);font-family:var(--sans);
font-size:16px;line-height:1.65;-webkit-font-smoothing:antialiased}
.wrap{max-width:60rem;margin:0 auto;padding:0 1.75rem}
/* The dot field. A halftone made of one repeating radial-gradient, faded out
along the diagonal by a mask so it reads as a wave rather than wallpaper. */
/* No overflow clip here: the dot field is inset to the box already, and a clip
would cut off the copy-prompt menu. */
.hero{position:relative;border-bottom:1px solid var(--line)}
.hero::before{content:"";position:absolute;inset:0;
background-image:radial-gradient(var(--line-hi) 1px, transparent 1px);
background-size:7px 7px;
-webkit-mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
pointer-events:none}
.hero .wrap{position:relative;padding-top:5.5rem;padding-bottom:4rem}
nav{border-bottom:1px solid var(--line)}
nav .in{max-width:60rem;margin:0 auto;padding:1.1rem 1.75rem;display:flex;
justify-content:space-between;align-items:center;gap:1rem}
.brand{font-family:var(--mono);font-size:.85rem;letter-spacing:.14em;
text-transform:uppercase;color:var(--ink);text-decoration:none;
display:inline-flex;align-items:center;gap:.6rem}
.brand i{font-style:normal;color:var(--faint)}
.mark{width:1.15em;height:1.15em;display:block;flex:0 0 auto}
nav a.up{font-family:var(--mono);font-size:.72rem;letter-spacing:.14em;
text-transform:uppercase;color:var(--soft);text-decoration:none}
nav a.up:hover{color:var(--ink)}
h1{font-family:var(--mono);font-size:clamp(2rem,5.4vw,3.4rem);line-height:1.12;
letter-spacing:-.02em;margin:0 0 1.1rem;font-weight:500;text-wrap:balance}
h1 em{font-style:normal;color:var(--faint)}
.lede{color:var(--soft);font-size:1.05rem;max-width:52ch;margin:0 0 2.25rem}
.btns{display:flex;flex-wrap:wrap;gap:.7rem;margin:0 0 .5rem;align-items:center}
.btn{display:inline-flex;align-items:center;gap:.5rem;font-family:var(--mono);
font-size:.72rem;letter-spacing:.14em;text-transform:uppercase;
padding:.8rem 1.4rem;border:1px solid var(--line-hi);border-radius:var(--r);
color:var(--ink);text-decoration:none;background:transparent;cursor:pointer;
line-height:1.2;font-weight:400}
.btn:hover{background:#141414;text-decoration:none;border-color:var(--soft)}
.btn.solid{background:var(--ink);color:#000;border-color:var(--ink)}
.btn.solid:hover{background:#d8d8d4}
.btn .chev{width:.7em;height:.7em;flex:0 0 auto;transition:transform .15s ease}
.btn[aria-expanded="true"] .chev{transform:rotate(180deg)}
/* Copy-prompt selector. Each row copies the prompt for its own target. */
.copy{position:relative;display:inline-flex}
.menu{position:absolute;top:calc(100% + .5rem);left:0;z-index:20;min-width:21rem;
background:var(--panel);border:1px solid var(--line-hi);border-radius:var(--r-soft);
padding:.4rem;display:flex;flex-direction:column;
box-shadow:0 12px 32px rgba(0,0,0,.75)}
.menu[hidden]{display:none}
.mi{display:flex;align-items:center;gap:.7rem;width:100%;text-align:left;
padding:.62rem .8rem;border:0;border-radius:calc(var(--r-soft) - .35rem);
background:transparent;color:var(--ink);font-family:var(--mono);font-size:.76rem;
letter-spacing:.04em;text-decoration:none;cursor:pointer;line-height:1.3;
white-space:nowrap}
.mi:hover,.mi:focus-visible{background:#181818;outline:none}
.mi .mark{width:1.05rem;height:1.05rem;color:var(--soft)}
.mi:hover .mark{color:var(--ink)}
.mi u{margin-left:auto;color:var(--faint);text-decoration:none;font-size:.68rem;
letter-spacing:.1em;text-transform:uppercase}
/* Works-with bar: one scrolling row, name and mark per agent. */
.strip{border-bottom:1px solid var(--line);background:var(--panel)}
.strip .in{max-width:60rem;margin:0 auto;padding:.9rem 1.75rem;display:flex;
align-items:center;gap:1.25rem;font-family:var(--mono);font-size:.72rem;
letter-spacing:.1em;text-transform:uppercase;color:var(--faint)}
.strip b{color:var(--soft);font-weight:400;flex:0 0 auto;
border-right:1px solid var(--line);padding-right:1.25rem;align-self:stretch;
display:flex;align-items:center}
.bar{display:flex;align-items:center;gap:.5rem;flex:1 1 auto;min-width:0;
overflow-x:auto;scrollbar-width:none;scroll-snap-type:x proximity;
-webkit-overflow-scrolling:touch;
-webkit-mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent);
mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent)}
.bar::-webkit-scrollbar{display:none}
.agent{display:inline-flex;align-items:center;gap:.45rem;flex:0 0 auto;
padding:.4rem .85rem;border:1px solid var(--line);border-radius:var(--r);
color:var(--soft);white-space:nowrap;scroll-snap-align:start;
text-decoration:none;background:var(--ground)}
.agent:hover{color:var(--ink);border-color:var(--line-hi);text-decoration:none}
.agent .mark{width:.95rem;height:.95rem}
h2{font-family:var(--mono);font-size:1.1rem;letter-spacing:-.01em;margin:2.75rem 0 .75rem;font-weight:500}
h3{font-size:1rem;margin:2rem 0 .5rem;font-weight:600}
h4,h5,h6{font-size:.94rem;margin:1.5rem 0 .5rem;font-weight:600}
p{margin:0 0 1rem;max-width:70ch}
ul,ol{margin:0 0 1rem;padding-left:1.35rem;max-width:70ch}
li{margin:.22rem 0}
a{color:var(--ink);text-decoration:underline;text-underline-offset:3px;
text-decoration-color:var(--line-hi)}
a:hover{text-decoration-color:var(--ink)}
a:focus-visible{outline:1px solid var(--ink);outline-offset:3px}
code{font-family:var(--mono);font-size:.85em;background:var(--panel);
border:1px solid var(--line);border-radius:5px;padding:.1em .36em}
pre{background:var(--panel);border:1px solid var(--line);padding:1rem 1.1rem;
border-radius:var(--r-soft);overflow-x:auto;margin:0 0 1.25rem}
pre code{background:none;border:0;padding:0;font-size:.82rem;line-height:1.7}
blockquote{margin:0 0 1rem;padding-left:1rem;border-left:1px solid var(--line-hi);color:var(--soft)}
hr{border:0;border-top:1px solid var(--line);margin:2.5rem 0}
table{border-collapse:collapse;width:100%;margin:0 0 1.25rem;font-size:.92rem;
display:block;overflow-x:auto}
th,td{border:1px solid var(--line);padding:.5rem .7rem;text-align:left}
.curl{display:block;background:var(--panel);border:1px solid var(--line);
border-radius:var(--r-soft);padding:.95rem 1.1rem;font-family:var(--mono);
font-size:.8rem;color:var(--soft);overflow-x:auto;margin:0 0 2.5rem;
white-space:pre;line-height:1.9}
.curl b{color:var(--ink);font-weight:400}
.curl i{font-style:normal;color:var(--faint)}
/* index */
#q{width:100%;padding:.85rem 1.2rem;font-family:var(--mono);font-size:.85rem;color:var(--ink);
background:var(--panel);border:1px solid var(--line);border-radius:var(--r);margin:0 0 .6rem}
#q::placeholder{color:var(--faint);text-transform:uppercase;letter-spacing:.1em;font-size:.72rem}
#q:focus{outline:none;border-color:var(--soft)}
#count{color:var(--faint);font-family:var(--mono);font-size:.72rem;letter-spacing:.1em;
text-transform:uppercase;margin:0 0 2rem}
.cat{margin:2.5rem 0 .5rem;font-family:var(--mono);font-size:.7rem;letter-spacing:.16em;
text-transform:uppercase;color:var(--faint);border-bottom:1px solid var(--line);
padding-bottom:.45rem}
.row{display:block;padding:.6rem .9rem;margin:0 -.9rem;border-bottom:1px solid var(--line);
border-radius:var(--r-soft);text-decoration:none}
.row:hover{background:#0a0a0a}
.nm{font-family:var(--mono);font-size:.84rem;color:var(--ink);display:block}
.ds{color:var(--soft);font-size:.9rem;display:block;margin-top:.12rem}
footer{margin-top:4rem;padding:2rem 0 4rem;border-top:1px solid var(--line);
color:var(--faint);font-size:.85rem}
footer a{color:var(--soft)}
.fmap{display:grid;gap:1.75rem;grid-template-columns:repeat(auto-fit,minmax(11rem,1fr));
margin:0 0 2rem;font-size:.85rem}
.fmap b{display:block;font-family:var(--mono);font-size:.68rem;letter-spacing:.16em;
text-transform:uppercase;color:var(--faint);font-weight:400;margin:0 0 .6rem;
border-bottom:1px solid var(--line);padding-bottom:.4rem}
.fmap a{display:inline-block;color:var(--ink);text-decoration:none;margin:.3rem .35rem 0 0}
.fmap a:hover{text-decoration:underline}
.fmap div{color:var(--faint);line-height:1.5}
.fend{border-top:1px solid var(--line);padding-top:1.25rem;font-size:.82rem}
@media (max-width:640px){
.hero .wrap{padding-top:3.25rem;padding-bottom:2.75rem}
.btns{gap:.5rem}
.btn{padding:.75rem 1.15rem;font-size:.68rem}
.copy,.copy .btn{width:100%;justify-content:center}
.menu{left:0;right:0;min-width:0}
.strip .in{flex-direction:column;align-items:flex-start;gap:.6rem;padding:.9rem 1.75rem}
.strip b{border-right:0;padding-right:0}
.bar{width:100%}
}
</style>
<link rel="alternate" type="text/markdown" href="/realtime-pubsub-patterns.md">
<link rel="icon" href="/favicon.svg">
<link rel="canonical" href="https://hanzoskills.com/realtime-pubsub-patterns">
</head>
<!-- Reading this with a machine? Every page here is also plain markdown.
This one: https://hanzoskills.com/realtime-pubsub-patterns.md
Index: https://hanzoskills.com/llms.txt
Corpus: https://hanzoskills.com/all.md
Append .md to any path for the markdown twin. -->
<body>
<nav><div class="in">
<a class="brand" href="/"><svg class="mark" viewBox="0 0 67 67" fill="currentColor" aria-hidden="true"><path d="M22.21 67V44.6369H0V67H22.21Z"/><path d="M66.7038 22.3184H22.2534L0.0878906 44.6367H44.4634L66.7038 22.3184Z"/><path d="M22.21 0H0V22.3184H22.21V0Z"/><path d="M66.7198 0H44.5098V22.3184H66.7198V0Z"/><path d="M66.7198 67V44.6369H44.5098V67H66.7198Z"/></svg> HANZO<i>SKILLS</i></a>
<a class="up" href="/">All skills</a>
</div></nav>
<div class="wrap">
<h1>realtime-pubsub-patterns</h1><p class="lede">Scope: Publish/subscribe architecture, Redis Pub/Sub, message queues, and fan-out patterns Lines: 372 Last Updated: 2025-10-18</p><h1>Pub/Sub Patterns</h1>
<p><strong>Scope</strong>: Publish/subscribe architecture, Redis Pub/Sub, message queues, and fan-out patterns <strong>Lines</strong>: 372 <strong>Last Updated</strong>: 2025-10-18</p>
<h2>When to Use This Skill</h2>
<p>Use pub/sub patterns when you need:</p>
<ul><li><strong>Decoupled communication</strong>: Publishers don&#x27;t know about subscribers</li><li><strong>Broadcasting</strong>: Send messages to multiple consumers simultaneously</li><li><strong>Event-driven architecture</strong>: React to events across distributed systems</li><li><strong>Real-time notifications</strong>: Push updates to multiple clients (chat, notifications)</li><li><strong>Scalability</strong>: Add/remove subscribers without affecting publishers</li></ul>
<p><strong>Don&#x27;t use</strong> when:</p>
<ul><li>Point-to-point communication sufficient (use direct messaging)</li><li>Message ordering critical across topics (use message queue with ordering)</li><li>Request-response pattern needed (use RPC or REST)</li><li>Single consumer per message (use work queue)</li></ul>
<h2>Core Concepts</h2>
<h3>Pub/Sub Architecture</h3>
<pre><code>Publisher Pub/Sub Broker Subscribers
| | |
|-- Publish(topic, msg) ------&gt;| |
| |-- topic:A -------------------&gt;| Sub-1
| |-- topic:A -------------------&gt;| Sub-2
| |-- topic:B -------------------&gt;| Sub-3
| | |</code></pre>
<p><strong>Key Properties</strong>:</p>
<ul><li>Publishers and subscribers are decoupled</li><li>Many-to-many communication</li><li>Topics/channels for message routing</li><li>No message persistence (fire-and-forget)</li><li>Real-time delivery (push-based)</li></ul>
<h3>Pub/Sub vs Message Queue</h3>
<pre><code>Pub/Sub:
- One message → Many consumers
- No persistence (usually)
- Real-time delivery
- Example: Redis Pub/Sub, MQTT
Message Queue:
- One message → One consumer
- Persistent messages
- At-least-once delivery
- Example: RabbitMQ, SQS, Kafka</code></pre>
<h3>Common Patterns</h3>
<pre><code>1. Fan-Out: One publisher → Multiple subscribers
2. Topic-Based: Route by topic/channel name
3. Pattern-Based: Subscribe to pattern (e.g., &quot;user.*&quot;)
4. Request-Reply: Pub/sub with response channel
5. Priority: Different topics for different priorities</code></pre>
<h2>Patterns</h2>
<h3>1. Basic Redis Pub/Sub (Node.js)</h3>
<pre><code class="lang-typescript">import Redis from &#x27;ioredis&#x27;;
// Publisher
class Publisher {
private redis: Redis;
constructor() {
this.redis = new Redis({
host: &#x27;localhost&#x27;,
port: 6379,
});
}
async publish(channel: string, message: any): Promise&lt;number&gt; {
const payload = JSON.stringify(message);
const subscribers = await this.redis.publish(channel, payload);
console.log(`Published to ${channel}: ${subscribers} subscribers received`);
return subscribers;
}
disconnect() {
this.redis.disconnect();
}
}
// Subscriber
class Subscriber {
private redis: Redis;
private handlers: Map&lt;string, Set&lt;(message: any) =&gt; void&gt;&gt; = new Map();
constructor() {
this.redis = new Redis({
host: &#x27;localhost&#x27;,
port: 6379,
});
this.redis.on(&#x27;message&#x27;, (channel, message) =&gt; {
this.handleMessage(channel, message);
});
}
subscribe(channel: string, handler: (message: any) =&gt; void) {
if (!this.handlers.has(channel)) {
this.handlers.set(channel, new Set());
this.redis.subscribe(channel);
console.log(`Subscribed to ${channel}`);
}
this.handlers.get(channel)!.add(handler);
}
unsubscribe(channel: string, handler?: (message: any) =&gt; void) {
if (!this.handlers.has(channel)) return;
if (handler) {
this.handlers.get(channel)!.delete(handler);
}
if (!handler || this.handlers.get(channel)!.size === 0) {
this.handlers.delete(channel);
this.redis.unsubscribe(channel);
console.log(`Unsubscribed from ${channel}`);
}
}
private handleMessage(channel: string, message: string) {
const handlers = this.handlers.get(channel);
if (!handlers) return;
let parsedMessage: any;
try {
parsedMessage = JSON.parse(message);
} catch {
parsedMessage = message;
}
handlers.forEach((handler) =&gt; {
try {
handler(parsedMessage);
} catch (error) {
console.error(`Error in handler for ${channel}:`, error);
}
});
}
disconnect() {
this.redis.disconnect();
}
}
// Usage
const publisher = new Publisher();
const subscriber = new Subscriber();
subscriber.subscribe(&#x27;notifications&#x27;, (message) =&gt; {
console.log(&#x27;Notification received:&#x27;, message);
});
subscriber.subscribe(&#x27;chat:room1&#x27;, (message) =&gt; {
console.log(&#x27;Chat message:&#x27;, message);
});
await publisher.publish(&#x27;notifications&#x27;, {
type: &#x27;alert&#x27;,
text: &#x27;System maintenance in 5 minutes&#x27;,
});
await publisher.publish(&#x27;chat:room1&#x27;, {
user: &#x27;alice&#x27;,
text: &#x27;Hello, world!&#x27;,
});</code></pre>
<h3>2. Pattern-Based Subscription</h3>
<pre><code class="lang-typescript">// Pattern subscriber using Redis PSUBSCRIBE
class PatternSubscriber {
private redis: Redis;
private handlers: Map&lt;string, Set&lt;(channel: string, message: any) =&gt; void&gt;&gt; = new Map();
constructor() {
this.redis = new Redis();
this.redis.on(&#x27;pmessage&#x27;, (pattern, channel, message) =&gt; {
this.handlePatternMessage(pattern, channel, message);
});
}
psubscribe(pattern: string, handler: (channel: string, message: any) =&gt; void) {
if (!this.handlers.has(pattern)) {
this.handlers.set(pattern, new Set());
this.redis.psubscribe(pattern);
console.log(`Pattern subscribed: ${pattern}`);
}
this.handlers.get(pattern)!.add(handler);
}
punsubscribe(pattern: string, handler?: (channel: string, message: any) =&gt; void) {
if (!this.handlers.has(pattern)) return;
if (handler) {
this.handlers.get(pattern)!.delete(handler);
}
if (!handler || this.handlers.get(pattern)!.size === 0) {
this.handlers.delete(pattern);
this.redis.punsubscribe(pattern);
console.log(`Pattern unsubscribed: ${pattern}`);
}
}
private handlePatternMessage(pattern: string, channel: string, message: string) {
const handlers = this.handlers.get(pattern);
if (!handlers) return;
let parsedMessage: any;
try {
parsedMessage = JSON.parse(message);
} catch {
parsedMessage = message;
}
handlers.forEach((handler) =&gt; {
try {
handler(channel, parsedMessage);
} catch (error) {
console.error(`Error in pattern handler for ${pattern}:`, error);
}
});
}
}
// Usage
const patternSub = new PatternSubscriber();
// Subscribe to all chat rooms
patternSub.psubscribe(&#x27;chat:*&#x27;, (channel, message) =&gt; {
console.log(`Message in ${channel}:`, message);
});
// Subscribe to all user events
patternSub.psubscribe(&#x27;user:*:*&#x27;, (channel, message) =&gt; {
console.log(`User event in ${channel}:`, message);
});</code></pre>
<h3>3. Pub/Sub with WebSocket Bridge</h3>
<pre><code class="lang-typescript">import { WebSocketServer, WebSocket } from &#x27;ws&#x27;;
import Redis from &#x27;ioredis&#x27;;
interface Client {
ws: WebSocket;
id: string;
subscriptions: Set&lt;string&gt;;
}
class PubSubWebSocketBridge {
private wss: WebSocketServer;
private publisher: Redis;
private subscriber: Redis;
private clients: Map&lt;string, Client&gt; = new Map();
constructor(wsPort: number) {
this.wss = new WebSocketServer({ port: wsPort });
this.publisher = new Redis();
this.subscriber = new Redis();
this.subscriber.on(&#x27;message&#x27;, (channel, message) =&gt; {
this.broadcastToChannel(channel, message);
});
this.setupWebSocket();
}
private setupWebSocket() {
this.wss.on(&#x27;connection&#x27;, (ws) =&gt; {
const client: Client = {
ws,
id: this.generateClientId(),
subscriptions: new Set(),
};
this.clients.set(client.id, client);
console.log(`Client ${client.id} connected`);
ws.on(&#x27;message&#x27;, (data) =&gt; {
this.handleClientMessage(client, data.toString());
});
ws.on(&#x27;close&#x27;, () =&gt; {
this.handleClientDisconnect(client);
});
});
}
private handleClientMessage(client: Client, data: string) {
try {
const message = JSON.parse(data);
switch (message.type) {
case &#x27;subscribe&#x27;:
this.handleSubscribe(client, message.channel);
break;
case &#x27;unsubscribe&#x27;:
this.handleUnsubscribe(client, message.channel);
break;
case &#x27;publish&#x27;:
this.handlePublish(message.channel, message.data);
break;
}
} catch (error) {
console.error(&#x27;Invalid message from client:&#x27;, error);
}
}
private async handleSubscribe(client: Client, channel: string) {
if (client.subscriptions.has(channel)) return;
client.subscriptions.add(channel);
// Subscribe to Redis channel if first subscriber
const channelClients = this.getChannelClients(channel);
if (channelClients.length === 1) {
await this.subscriber.subscribe(channel);
console.log(`Subscribed to Redis channel: ${channel}`);
}
console.log(`Client ${client.id} subscribed to ${channel}`);
}
private async handleUnsubscribe(client: Client, channel: string) {
if (!client.subscriptions.has(channel)) return;
client.subscriptions.delete(channel);
// Unsubscribe from Redis if no more subscribers
const channelClients = this.getChannelClients(channel);
if (channelClients.length === 0) {
await this.subscriber.unsubscribe(channel);
console.log(`Unsubscribed from Redis channel: ${channel}`);
}
console.log(`Client ${client.id} unsubscribed from ${channel}`);
}
private async handlePublish(channel: string, data: any) {
const message = JSON.stringify(data);
await this.publisher.publish(channel, message);
}
private broadcastToChannel(channel: string, message: string) {
const clients = this.getChannelClients(channel);
const payload = JSON.stringify({
type: &#x27;message&#x27;,
channel,
data: JSON.parse(message),
});
clients.forEach((client) =&gt; {
if (client.ws.readyState === WebSocket.OPEN) {
client.ws.send(payload);
}
});
}
private getChannelClients(channel: string): Client[] {
return Array.from(this.clients.values()).filter((client) =&gt;
client.subscriptions.has(channel)
);
}
private async handleClientDisconnect(client: Client) {
// Unsubscribe from all channels
for (const channel of client.subscriptions) {
client.subscriptions.delete(channel);
const channelClients = this.getChannelClients(channel);
if (channelClients.length === 0) {
await this.subscriber.unsubscribe(channel);
}
}
this.clients.delete(client.id);
console.log(`Client ${client.id} disconnected`);
}
private generateClientId(): string {
return `client_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
}
}
// Usage
const bridge = new PubSubWebSocketBridge(8080);
// Clients connect via WebSocket and send:
// {&quot;type&quot;: &quot;subscribe&quot;, &quot;channel&quot;: &quot;notifications&quot;}
// {&quot;type&quot;: &quot;publish&quot;, &quot;channel&quot;: &quot;notifications&quot;, &quot;data&quot;: {&quot;text&quot;: &quot;Hello&quot;}}</code></pre>
<h3>4. Request-Reply Pattern</h3>
<pre><code class="lang-typescript">// Request-Reply using pub/sub with correlation IDs
class RequestReplyClient {
private redis: Redis;
private subscriber: Redis;
private clientId: string;
private replyChannel: string;
private pendingRequests: Map&lt;string, {
resolve: (response: any) =&gt; void;
reject: (error: Error) =&gt; void;
timeout: NodeJS.Timeout;
}&gt; = new Map();
constructor() {
this.redis = new Redis();
this.subscriber = new Redis();
this.clientId = this.generateClientId();
this.replyChannel = `reply:${this.clientId}`;
this.subscriber.subscribe(this.replyChannel);
this.subscriber.on(&#x27;message&#x27;, (channel, message) =&gt; {
if (channel === this.replyChannel) {
this.handleReply(message);
}
});
}
async request(channel: string, data: any, timeoutMs: number = 5000): Promise&lt;any&gt; {
const correlationId = this.generateCorrelationId();
const promise = new Promise((resolve, reject) =&gt; {
const timeout = setTimeout(() =&gt; {
this.pendingRequests.delete(correlationId);
reject(new Error(&#x27;Request timeout&#x27;));
}, timeoutMs);
this.pendingRequests.set(correlationId, { resolve, reject, timeout });
});
const request = {
correlationId,
replyTo: this.replyChannel,
data,
};
await this.redis.publish(channel, JSON.stringify(request));
return promise;
}
private handleReply(message: string) {
try {
const reply = JSON.parse(message);
const pending = this.pendingRequests.get(reply.correlationId);
if (pending) {
clearTimeout(pending.timeout);
this.pendingRequests.delete(reply.correlationId);
if (reply.error) {
pending.reject(new Error(reply.error));
} else {
pending.resolve(reply.data);
}
}
} catch (error) {
console.error(&#x27;Error handling reply:&#x27;, error);
}
}
private generateClientId(): string {
return `client_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
}
private generateCorrelationId(): string {
return `${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
}
}
// Request-Reply Server
class RequestReplyServer {
private redis: Redis;
private subscriber: Redis;
private handlers: Map&lt;string, (data: any) =&gt; Promise&lt;any&gt;&gt; = new Map();
constructor() {
this.redis = new Redis();
this.subscriber = new Redis();
this.subscriber.on(&#x27;message&#x27;, (channel, message) =&gt; {
this.handleRequest(channel, message);
});
}
handle(channel: string, handler: (data: any) =&gt; Promise&lt;any&gt;) {
this.handlers.set(channel, handler);
this.subscriber.subscribe(channel);
console.log(`Handling requests on ${channel}`);
}
private async handleRequest(channel: string, message: string) {
const handler = this.handlers.get(channel);
if (!handler) return;
try {
const request = JSON.parse(message);
const result = await handler(request.data);
const reply = {
correlationId: request.correlationId,
data: result,
};
await this.redis.publish(request.replyTo, JSON.stringify(reply));
} catch (error) {
const request = JSON.parse(message);
const reply = {
correlationId: request.correlationId,
error: error instanceof Error ? error.message : &#x27;Unknown error&#x27;,
};
await this.redis.publish(request.replyTo, JSON.stringify(reply));
}
}
}
// Usage
const server = new RequestReplyServer();
server.handle(&#x27;calculate&#x27;, async (data) =&gt; {
return { result: data.a + data.b };
});
const client = new RequestReplyClient();
const response = await client.request(&#x27;calculate&#x27;, { a: 5, b: 3 });
console.log(response); // { result: 8 }</code></pre>
<h2>Quick Reference</h2>
<h3>Redis Pub/Sub Commands</h3>
<pre><code class="lang-typescript">// Publish
await redis.publish(channel, message); // Returns number of subscribers
// Subscribe
await redis.subscribe(channel1, channel2, ...);
redis.on(&#x27;message&#x27;, (channel, message) =&gt; { /* Handle */ });
// Pattern subscribe
await redis.psubscribe(&#x27;pattern:*&#x27;);
redis.on(&#x27;pmessage&#x27;, (pattern, channel, message) =&gt; { /* Handle */ });
// Unsubscribe
await redis.unsubscribe(channel);
await redis.punsubscribe(pattern);</code></pre>
<h3>Message Format Best Practices</h3>
<pre><code class="lang-typescript">// Include metadata
const message = {
timestamp: Date.now(),
type: &#x27;notification&#x27;,
source: &#x27;user-service&#x27;,
data: { /* payload */ },
};
// Use correlation IDs for tracking
const message = {
correlationId: &#x27;abc-123&#x27;,
data: { /* payload */ },
};</code></pre>
<h3>Channel Naming Conventions</h3>
<pre><code>user:123:notifications // User-specific notifications
chat:room:456 // Chat room messages
events:order:created // Domain events
system:alerts // System-wide alerts</code></pre>
<h2>Anti-Patterns</h2>
<h3>❌ Using Pub/Sub for Guaranteed Delivery</h3>
<pre><code class="lang-typescript">// Wrong: Pub/sub doesn&#x27;t guarantee delivery
await redis.publish(&#x27;orders&#x27;, JSON.stringify(order));
// If no subscribers connected, message is lost</code></pre>
<p><strong>Why it&#x27;s bad</strong>: Messages are lost if no subscribers are active</p>
<p><strong>Better approach</strong>:</p>
<pre><code class="lang-typescript">// Use message queue (Redis Streams, RabbitMQ) for guaranteed delivery
await redis.xadd(&#x27;orders&#x27;, &#x27;*&#x27;, &#x27;order&#x27;, JSON.stringify(order));</code></pre>
<h3>❌ Large Message Payloads</h3>
<pre><code class="lang-typescript">// Wrong: Publishing large data
await redis.publish(&#x27;channel&#x27;, JSON.stringify(largeObject)); // Multiple MB</code></pre>
<p><strong>Why it&#x27;s bad</strong>: Blocks Redis, consumes bandwidth, slows all subscribers</p>
<p><strong>Better approach</strong>:</p>
<pre><code class="lang-typescript">// Store data separately, publish reference
const id = await redis.set(&#x27;data:123&#x27;, JSON.stringify(largeObject));
await redis.publish(&#x27;channel&#x27;, JSON.stringify({ type: &#x27;data_ready&#x27;, id }));</code></pre>
<h3>❌ No Error Handling in Subscribers</h3>
<pre><code class="lang-typescript">// Wrong: Unhandled errors crash subscriber
redis.on(&#x27;message&#x27;, (channel, message) =&gt; {
const data = JSON.parse(message); // Can throw
processData(data); // Can throw
});</code></pre>
<p><strong>Why it&#x27;s bad</strong>: One bad message crashes the entire subscriber</p>
<p><strong>Better approach</strong>:</p>
<pre><code class="lang-typescript">redis.on(&#x27;message&#x27;, (channel, message) =&gt; {
try {
const data = JSON.parse(message);
processData(data);
} catch (error) {
console.error(&#x27;Error processing message:&#x27;, error);
}
});</code></pre>
<h3>❌ Synchronous Processing in Subscribers</h3>
<pre><code class="lang-typescript">// Wrong: Blocking subscriber
redis.on(&#x27;message&#x27;, (channel, message) =&gt; {
expensiveOperation(message); // Blocks event loop
});</code></pre>
<p><strong>Why it&#x27;s bad</strong>: Slow processing delays other messages</p>
<p><strong>Better approach</strong>:</p>
<pre><code class="lang-typescript">redis.on(&#x27;message&#x27;, async (channel, message) =&gt; {
setImmediate(async () =&gt; {
await expensiveOperation(message);
});
});</code></pre>
<h2>Related Skills</h2>
<ul><li><strong>websocket-implementation.md</strong>: WebSocket bridge for pub/sub to browsers</li><li><strong>server-sent-events.md</strong>: SSE for unidirectional pub/sub to clients</li><li><strong>realtime-sync.md</strong>: Synchronization patterns using pub/sub</li><li><strong>network-resilience-patterns.md</strong>: Handling failures in pub/sub systems</li></ul>
<hr>
<p><strong>Last Updated</strong>: 2025-10-18 <strong>Format Version</strong>: 1.0 (Atomic)</p>
<footer>
<div class="fmap">
<div><b>Build</b>
<a href="https://hanzo.app">hanzo.app</a> app builder
<a href="https://hanzo.codes">hanzo.codes</a> code with an agent
<a href="https://hanzoskills.com">hanzoskills.com</a> what an agent reads</div>
<div><b>Run</b>
<a href="https://console.hanzo.ai">console.hanzo.ai</a> cloud console
<a href="https://api.hanzo.ai">api.hanzo.ai</a> the API</div>
<div><b>Account</b>
<a href="https://hanzo.id">hanzo.id</a> sign in
<a href="https://pay.hanzo.ai">pay.hanzo.ai</a> top up
<a href="https://billing.hanzo.ai">billing.hanzo.ai</a> invoices and usage</div>
<div><b>Company</b>
<a href="https://hanzo.ai">hanzo.ai</a> canonical
<a href="https://hanzo.ventures">hanzo.ventures</a> ventures</div>
</div>
<div class="fend"><a href="/realtime-pubsub-patterns.md">This page as plain text</a> · <a href="/llms.txt">Index for agents</a></div>
</footer>
</div></body></html>