Broadcast Groups

Same API as the core createGroup plugin, but membership is stored in Redis so groups work across multiple server instances. Local WebSocket tracking is maintained per-instance, and cross-instance events are relayed via Redis pub/sub.

When to use over the built-in plugin: The core groups plugin only tracks members on the local process. If your game lobby or editor room spans instances, members on one instance cannot send to members on another. The Redis version keeps a shared membership list and relays publishes through Redis pub/sub.

Setup

// src/lib/server/lobby.js
import { redis } from './redis.js';
import { createGroup } from 'svelte-adapter-uws-extensions/redis/groups';

export const lobby = createGroup(redis, 'lobby', {
  maxMembers: 50,
  meta: { game: 'chess' }
});

Note: the API signature is createGroup(client, name, options) instead of createGroup(name, options) - the Redis client is the first argument.

Usage

// src/hooks.ws.js
import { lobby } from '$lib/server/lobby';

export async function subscribe(ws, topic, { platform }) {
  if (topic === 'lobby') await lobby.join(ws, platform);
}

export async function close(ws, { platform }) {
  await lobby.leave(ws, platform);
}

Options

OptionDefaultDescription
maxMembersInfinityMaximum members allowed (enforced atomically)
meta{}Initial group metadata
memberTtl120Member entry TTL in seconds. Entries from crashed instances expire after this period.
onJoin-Called after a member joins
onLeave-Called after a member leaves
onFull-Called when a join is rejected (full)
onClose-Called when the group is closed

API

MethodDescription
join(ws, platform, role?)Add a member (returns false if full/closed)
leave(ws, platform)Remove a member
publish(platform, event, data, role?)Broadcast to all or filter by role
send(platform, ws, event, data)Send to a single member
localMembers()Members on this instance
count()Total members across all instances
has(ws)Check membership on this instance
getMeta() / setMeta(meta)Read/write group metadata
close(platform)Dissolve the group
destroy()Stop the Redis subscriber

Was this page helpful?