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
- Update
sendMessageto take aroomparameter, store messages inrooms[room], and publish to'chat:' + room - Add a
getMessages(room)RPC that returns the messages for a specific room - 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