Distributed Pub/Sub with Redis
Goal: Run multiple app replicas with cross-instance messaging.
Install
npm install svelte-adapter-uws-extensions ioredis Set up the Redis client
// src/lib/server/redis.js
import { createRedisClient, createPubSub } from 'svelte-adapter-uws-extensions/redis';
export const redis = createRedisClient({
url: process.env.REDIS_URL || 'redis://localhost:6379'
});
export const bus = createPubSub(redis); Wire it into hooks
// src/hooks.ws.js
import { createMessage } from 'svelte-realtime/server';
import { bus } from '$lib/server/redis';
export function upgrade() {
return { id: crypto.randomUUID() };
}
export function open(ws, { platform }) {
bus.activate(platform);
}
export const message = createMessage({
platform: (p) => bus.wrap(p)
}); Now ctx.publish() in any live() function reaches subscribers on all instances, not just the local one.
Run multiple replicas
SO_REUSEPORT=1 node build &
SO_REUSEPORT=1 node build & Both instances share port 443. Redis forwards publishes between them.
Related guides
- Scaling Guide - step-by-step from single instance to distributed
- Scaling and Resilience - when and why you need extensions
- Replay and Presence - persistent replay and cross-instance presence
- Observability - monitoring and metrics
- Extensions package - full API reference
Was this page helpful?