Skip to content

Automatic websocket reconnect and reload handling#5805

Merged
adhami3310 merged 6 commits into
mainfrom
masenf/reconnect-dropped-websocket
Sep 22, 2025
Merged

Automatic websocket reconnect and reload handling#5805
adhami3310 merged 6 commits into
mainfrom
masenf/reconnect-dropped-websocket

Conversation

@masenf

@masenf masenf commented Sep 19, 2025

Copy link
Copy Markdown
Collaborator
  • ensureSocketConnected is called when adding events or pumping the queue to trigger an automatic reconnection to the backend
  • when "reload" event is encountered, trigger a re-hydrate and wait until ALL on_load have finished processing and is_hydrated is True before requeue the event that caused the "reload"

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.
* ensureSocketConnected is called when adding events or pumping the queue to
  trigger an automatic reconnection to the backend
* when "reload" event is encountered, trigger a re-hydrate and wait until ALL
  on_load have finished processing and `is_hydrated` is True before requeue the
  event that caused the "reload"

@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 introduces automatic websocket reconnection functionality and enhanced reload event handling to improve the resilience of Reflex applications against network interruptions. The changes are focused entirely on the client-side JavaScript state management system.

The core enhancement adds an ensureSocketConnected() function that automatically detects when the websocket connection is lost and attempts to re-establish it. This function is strategically called before adding events to the queue and before processing the event queue, ensuring operations don't fail due to a dropped connection. When the socket disconnects, socket.current is now explicitly set to null, which enables the automatic reconnection logic.

A significant improvement is the introduction of an on_hydrated_queue mechanism that handles reload events more robustly. Previously, reload events could be processed before the application state was fully initialized, causing race conditions. Now, when a "reload" event is encountered, it triggers a re-hydration process and queues the reload event until all on_load handlers have finished processing and is_hydrated is True. This ensures the application is in a consistent state before processing reload events.

The visibility change handler has also been enhanced to recreate the socket connection when the user returns to a tab and finds the socket is null, improving recovery from browser tab switching scenarios.

These changes integrate seamlessly with Reflex's existing event system and state management architecture, maintaining backward compatibility while significantly improving user experience during network disruptions or server restarts.

Confidence score: 4/5

  • This PR is generally safe to merge with good resilience improvements but introduces some complexity in connection state management
  • Score reflects solid implementation of reconnection logic but potential edge cases around connection state transitions and queue management
  • Pay close attention to the websocket connection state management and the new on_hydrated_queue logic to ensure no race conditions

1 file reviewed, 1 comment

Edit Code Review Bot Settings | Greptile


// When the socket disconnects reset the event_processing flag
socket.current.on("disconnect", () => {
socket.current = null; // allow reconnect to occur automatically

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.

style: Setting socket to null on disconnect enables automatic reconnection, but ensure this doesn't cause issues with concurrent access to socket.current in other parts of the codebase

@codspeed-hq

codspeed-hq Bot commented Sep 19, 2025

Copy link
Copy Markdown

CodSpeed Performance Report

Merging #5805 will not alter performance

Comparing masenf/reconnect-dropped-websocket (31ecf95) with main (4468e14)

Summary

✅ 8 untouched

It doesn't really work, because the frontend will only process one
non-background event at a time, so the disconnect ends up occuring after the
event handler is already done.
@adhami3310 adhami3310 merged commit 21f7629 into main Sep 22, 2025
40 of 41 checks passed
@adhami3310 adhami3310 deleted the masenf/reconnect-dropped-websocket branch September 22, 2025 21:06
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.

2 participants