-
Notifications
You must be signed in to change notification settings - Fork 4.2k
feat(ws): add context logic to enterprise WS worker fixes NV-6821 #9385
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
feat(ws): add context logic to enterprise WS worker fixes NV-6821 #9385
Conversation
✅ Deploy Preview for dashboard-v2-novu-staging canceled.
|
private formatContextDisplay(contextKeys?: string[]): string { | ||
if (contextKeys === undefined) { | ||
return 'FF disabled'; | ||
} | ||
|
||
if (contextKeys.length === 0) { | ||
return 'no context'; | ||
} | ||
|
||
return contextKeys.join(', '); | ||
} | ||
|
||
/** | ||
* Check if feature flag is OFF (contextKeys is undefined) | ||
*/ | ||
private isFeatureFlagOff(contextKeys?: string[]): boolean { | ||
return contextKeys === undefined; | ||
} | ||
|
||
/** | ||
* Broadcast message to all user connections (FF OFF behavior) | ||
*/ | ||
private async broadcastToAllSockets( | ||
userId: string, | ||
event: string, | ||
message: string, | ||
sockets: WebSocket[] | ||
): Promise<void> { | ||
console.log(`Sending event ${event} to all ${sockets.length} socket(s) (FF disabled)`); | ||
|
||
const sendPromises = sockets.map(async (ws) => { | ||
try { | ||
ws.send(message); | ||
} catch (error) { | ||
console.error(`Failed to send message to user ${userId}:`, error); | ||
throw error; | ||
} | ||
}); | ||
|
||
await Promise.allSettled(sendPromises); | ||
} |
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.
This part can be deleted when we remove the feature flag
@coderabbitai summary |
✅ Actions performedSummary regeneration triggered. |
WalkthroughThis pull request implements context-aware message routing for WebSocket connections. ContextKeys are extracted from JWT payloads via middleware, propagated through handlers during WebSocket handshake, and stored in socket attachments. Messages can now be routed either to all connected sockets or selectively to sockets with matching context keys, controlled by feature flags. Changes
Sequence DiagramsequenceDiagram
participant Client
participant AuthMW as Auth Middleware
participant Handler as WebSocket Handler
participant Handshake as WS Handshake
participant Room as WebSocket Room (DO)
participant Sockets as Connected Sockets
Client->>AuthMW: Request with JWT
AuthMW->>AuthMW: Extract contextKeys from JWT
AuthMW->>Handler: Forward context with contextKeys
Client->>Handler: WebSocket upgrade
Handler->>Handler: Get contextKeys from context
Handler->>Handshake: includeHeaders: X-Context-Keys
Handshake->>Room: Establish connection
Room->>Room: Extract X-Context-Keys header
Room->>Room: Store contextKeys in attachment
Client->>Handler: Send message
Handler->>Handler: Extract contextKeys from body
Handler->>Room: sendToUser(userId, event, data, contextKeys)
alt contextKeys is undefined (Feature OFF)
Room->>Sockets: Broadcast to all sockets
else contextKeys is defined (Feature ON)
Room->>Room: Match contextKeys on each socket
Room->>Sockets: Deliver only to matching contexts
end
Sockets-->>Client: Message received
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~35 minutes The changes span multiple layers (middleware, handler, core logic, types) with cohesive context propagation flow. While introducing new conditional routing logic and helpers, the patterns are consistent across files. Public API signature changes require validation but the scope is focused on a single feature. 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 (4)
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 |
What changed? Why was the change needed?
Fixes NV-6817
Screenshots
Expand for optional sections
Related enterprise PR
Special notes for your reviewer
Summary by CodeRabbit