Skip to content

emit_update: take token instead of sid#5804

Closed
masenf wants to merge 2 commits into
mainfrom
masenf/emit-update-by-token
Closed

emit_update: take token instead of sid#5804
masenf wants to merge 2 commits into
mainfrom
masenf/emit-update-by-token

Conversation

@masenf

@masenf masenf commented Sep 19, 2025

Copy link
Copy Markdown
Collaborator

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.

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.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Edit Code Review Bot Settings | Greptile

@codspeed-hq

codspeed-hq Bot commented Sep 19, 2025

Copy link
Copy Markdown

CodSpeed Performance Report

Merging #5804 will not alter performance

Comparing masenf/emit-update-by-token (c09db7f) with main (4468e14)

Summary

✅ 8 untouched

@masenf

masenf commented Sep 22, 2025

Copy link
Copy Markdown
Collaborator Author

merged into #5805

@masenf masenf closed this Sep 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant