emit_update: take token instead of sid#5804
Conversation
This allows the app to be more resilient in the face of websocket reconnects. The event is processed against a token, so there's no reason to maintain websocket affinity for event processing. Whenever the update is ready to send, it will be sent to the current websocket/sid associated.
There was a problem hiding this comment.
Greptile Summary
This PR refactors Reflex's event processing architecture to improve websocket connection resilience by shifting from socket-based to token-based event routing. The core change modifies the emit_update method in the EventNamespace class to accept a client token instead of a socket ID (sid).
The changes affect two main files:
reflex/app.py: The emit_update method signature changes from (update, sid) to (update, token). The new implementation resolves the token to the current socket ID using _split_substate_key and the existing token_to_sid mapping. All call sites in modify_state and background task processing are updated to pass tokens instead of socket IDs. Error messages now reference client tokens rather than session IDs.
reflex/istate/proxy.py: The StateProxy class is optimized by pre-calculating the substate token during initialization rather than computing it on each context manager entry. The _substate_key() calculation moves from __aenter__() to __init__() and is stored as _self_substate_token.
This architectural change leverages Reflex's existing token management infrastructure to enable seamless handling of websocket reconnections. Previously, events were tightly coupled to specific websocket sessions, making the system fragile when connections dropped. Now, events are processed against tokens (representing browser tabs/clients), and the system dynamically looks up the current websocket for delivery. This allows the same client to reconnect with a new socket ID while maintaining event continuity.
The change integrates well with Reflex's state management system, which already uses tokens to identify client sessions across the application lifecycle. The token-based approach aligns with how Reflex handles state routing and client identification throughout the codebase.
Confidence score: 5/5
- This PR is safe to merge with minimal risk as it maintains existing functionality while improving system resilience
- Score reflects well-structured changes that leverage existing infrastructure and maintain backward compatibility through established token management patterns
- No files require special attention as the changes are straightforward refactoring with clear intent
2 files reviewed, no comments
CodSpeed Performance ReportMerging #5804 will not alter performanceComparing Summary
|
|
merged into #5805 |
This allows the app to be more resilient in the face of websocket reconnects. The event is processed against a token, so there's no reason to maintain websocket affinity for event processing. Whenever the update is ready to send, it will be sent to the current websocket/sid associated.