Redis Client
Factory that wraps ioredis with lifecycle management. All Redis extensions accept this client.
// src/lib/server/redis.js
import { createRedisClient } from 'svelte-adapter-uws-extensions/redis';
export const redis = createRedisClient({
url: 'redis://localhost:6379',
keyPrefix: 'myapp:' // optional, prefixes all keys
}); Options
| Option | Default | Description |
|---|---|---|
url | 'redis://localhost:6379' | Redis connection URL |
keyPrefix | '' | Prefix for all keys |
autoShutdown | true | Disconnect on sveltekit:shutdown |
options | {} | Extra ioredis options |
API
| Method | Description |
|---|---|
redis.redis | The underlying ioredis instance |
redis.key(k) | Returns keyPrefix + k |
redis.duplicate(overrides?) | New connection with same config. Pass ioredis options to override defaults. |
redis.quit() | Gracefully disconnect all connections |
Pub/Sub Bus
Distributes platform.publish() calls across multiple server instances via Redis pub/sub. Each instance publishes locally AND to Redis. Incoming Redis messages are forwarded to the local platform with echo suppression - messages from the same instance are ignored.
When to use over the built-in plugin: The core adapter’s platform.publish() only reaches clients connected to the current process. If you run two or more instances behind a load balancer, clients on instance A will not see messages published on instance B. The pub/sub bus fixes that.
Setup
// src/lib/server/bus.js
import { redis } from './redis.js';
import { createPubSubBus } from 'svelte-adapter-uws-extensions/redis/pubsub';
export const bus = createPubSubBus(redis); Usage
// src/hooks.ws.js
import { bus } from '$lib/server/bus';
let distributed;
export function open(ws, { platform }) {
// Start subscriber (idempotent, only subscribes once)
bus.activate(platform);
// Get a wrapped platform that publishes to Redis + local
distributed = bus.wrap(platform);
}
export function message(ws, { data, platform }) {
const msg = JSON.parse(Buffer.from(data).toString());
// This publish reaches local clients AND all other instances
distributed.publish('chat', 'message', msg);
} Options
| Option | Default | Description |
|---|---|---|
channel | 'uws:pubsub' | Redis channel name |
API
| Method | Description |
|---|---|
bus.wrap(platform) | Returns a new Platform whose publish() sends to Redis + local |
bus.activate(platform) | Start the Redis subscriber (idempotent) |
bus.deactivate() | Stop the subscriber |
Was this page helpful?