-
Notifications
You must be signed in to change notification settings - Fork 4.1k
feat(ws,worker,api): add context-based room routing for ws connections fixes NV-6790 #9341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: next
Are you sure you want to change the base?
Conversation
e00f688
to
c1708b1
Compare
@coderabbitai summary |
✅ Actions performedSummary regeneration triggered. |
commit: |
WalkthroughAdds optional contextKeys propagation across WebSocket payloads and routing. Updates DTOs, commands, services, and gateways to accept and forward contextKeys, enabling context-based room joins and targeted emits. Refactors socket-worker and queue services to object-based sendMessage params. Includes a submodule pointer update. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant API/Worker as API/Worker Usecases
participant SocketSvc as SocketWorkerService
participant WSQueue as WebSocketsQueueService
participant WSProc as WebSocket Worker
participant WSGW as WS Gateway
participant Rooms as Context Rooms
API/Worker->>SocketSvc: sendMessage({ userId, event, data, contextKeys? })
SocketSvc->>WSQueue: add({ userId, event, data, contextKeys?, ... })
WSQueue-->>WSProc: job({ userId, event, data, contextKeys? })
WSProc->>WSGW: sendMessage(userId, event, data, contextKeys?)
Note over WSGW: On connect, subscribers join default<br/>or per-context rooms based on contextKeys
alt contextKeys provided
loop for each key
WSGW->>Rooms: emit to room userId:contextKey
end
else no contextKeys
WSGW->>Rooms: emit to room userId:__default__
end
rect rgba(240,248,255,0.6)
Note over SocketSvc,WSGW: Unread/Unseen count paths pass contextKeys to<br/>repository getCount and to WSGateway.sendMessage
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (15)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
…t-isolated-event-emitting
556defd
to
0c9de11
Compare
✅ Deploy Preview for dashboard-v2-novu-staging canceled.
|
// Join context-specific rooms for efficient message routing | ||
if (contextKeys.length === 0) { | ||
// Join default room for connections without context | ||
await connection.join(`${subscriber._id}:${DEFAULT_CONTEXT_ROOM}`); | ||
Logger.log(`Connection ${connection.id} joined default room for ${subscriber._id}`, LOG_CONTEXT); | ||
} else { | ||
// Join a room for each context key | ||
for (const contextKey of contextKeys) { | ||
await connection.join(`${subscriber._id}:${contextKey}`); | ||
} | ||
Logger.log( | ||
`Connection ${connection.id} joined context rooms for ${subscriber._id}: ${contextKeys.join(', ')}`, | ||
LOG_CONTEXT | ||
); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To route events only to inbox sessions with matching contexts, there are two possible approaches:
-
Room-based routing (chosen): Each inbox session joins a context-specific room on connection. Events are emitted directly to the appropriate rooms.
-
JWT-based filtering: Decode the JWT token for each connected client when sending an event, extract the contextKeys, and decide whether to emit.
I think the #1 approach is better for performance and scalability.
LOG_CONTEXT | ||
); | ||
|
||
await connection.join(subscriber._id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also left the original subscriberId room for backwards compatibility, so after this change is rolled-out, the number of open rooms will double (until we remove this line), but that shouldn't be a problem, its just a server side grouping mechanism not a new WS connection.
What changed? Why was the change needed?
Screenshots
Expand for optional sections
Related enterprise PR
Special notes for your reviewer
Summary by CodeRabbit
New Features
Chores