Cursor
Same API as the core createCursor plugin, but cursor positions are shared across instances via Redis. Each instance throttles locally (same leading/trailing edge logic as the core), then relays broadcasts through Redis pub/sub so subscribers on other instances see cursor updates.
Hash entries have a TTL so stale cursors from crashed instances get cleaned up automatically.
When to use over the built-in plugin: The core cursor plugin broadcasts positions only to clients on the local process. In a multi-instance setup, users on different instances would not see each other’s cursors. The Redis version relays updates through pub/sub and stores positions in a shared hash.
Setup
// src/lib/server/cursors.js
import { redis } from './redis.js';
import { createCursor } from 'svelte-adapter-uws-extensions/redis/cursor';
export const cursors = createCursor(redis, {
throttle: 50,
select: (userData) => ({ id: userData.id, name: userData.name, color: userData.color }),
ttl: 30
}); Usage
// src/hooks.ws.js
import { cursors } from '$lib/server/cursors';
export function message(ws, { data, platform }) {
const msg = JSON.parse(Buffer.from(data).toString());
if (msg.type === 'cursor') {
cursors.update(ws, msg.topic, msg.position, platform);
}
}
export function close(ws, { platform }) {
cursors.remove(ws, platform);
} Options
| Option | Default | Description |
|---|---|---|
throttle | 50 | Minimum ms between broadcasts per user per topic |
select | strips __-prefixed keys | Extract user data to broadcast alongside position |
ttl | 30 | Per-entry TTL in seconds (auto-refreshed on each broadcast). Stale entries from crashed instances are filtered out individually, even if other instances are still active on the same topic. |
API
| Method | Description |
|---|---|
update(ws, topic, data, platform) | Broadcast cursor position (throttled per user per topic) |
remove(ws, platform, topic?) | Remove from a specific topic, or all topics if omitted |
list(topic) | Get current positions across all instances |
clear() | Reset all local and Redis state |
destroy() | Stop the Redis subscriber and clear timers |
Was this page helpful?