-
-
Notifications
You must be signed in to change notification settings - Fork 4
Open
Description
Use Case
I have a scenario where a handler in one namespace (/chat) needs to emit an event to a different namespace (/subscription). Specifically:
- User sends a message via
/chatnamespace - If they hit their daily limit, I need to emit
messageLimitReachedto the/subscriptionnamespace (where subscription-related events are handled)
Current Solution
I'm currently passing the io instance as a closure variable to the action factory:
export function createChatSocketActions(
chatService: ChatService,
io: Server // <-- Pass io manually
) {
const sendMessage = chatFactory.build({
async handler({ client, withRooms }) {
// ...
if (userHitLimit) {
// Cross-namespace emit
io.of("/subscription")
.to(`user:${userId}`)
.emit("messageLimitReached", {});
}
},
});
}This works, but feels like it might not be the intended pattern.
Question
What's the recommended approach for cross-namespace communication in zod-sockets?
Options I can think of beside my current approach:
- Add
ioto handler context - simple but maybe too permissive? - Add a helper like
emitToNamespace(ns, room, event, data)in the handler context - Extend
withRooms()to support.toNamespace(ns)or similar
Would love guidance on the "blessed" way to handle this, or if it's worth considering adding official support for cross-namespace operations in the library.
Thanks!
Metadata
Metadata
Assignees
Labels
No labels