0/24
13. Scoped Topics

Lesson 13: Scoped Topics

A chat app has many rooms. A project tracker has many boards. Every multi-tenant app needs data that's isolated by scope.

Instead of a fixed topic string, pass a function to live.stream(). The function receives ctx.args - the arguments the client passed when subscribing:

live.stream(
  (ctx) => 'chat:' + (ctx.args[0] || 'general'),
  (ctx) => rooms[ctx.args[0]] || [],
  { merge: 'crud', key: 'id' }
);

Clients subscribing with 'general' get the chat:general topic. Clients subscribing with 'random' get chat:random. Completely isolated.

Try it

  1. Update sendMessage to take a room parameter, store messages in rooms[room], and publish to 'chat:' + room
  2. Add a getMessages(room) RPC that returns the messages for a specific room
  3. Change the stream's topic from 'chat' to a function that scopes by room

The preview shows two rooms side by side. Send messages to each - they stay isolated. The tests prove it too.

WebSocket
0
No messages yet
/