Skip to content
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

Add Reconnection Logic to Websocket #2728

Closed
Alek99 opened this issue Feb 27, 2024 · 10 comments
Closed

Add Reconnection Logic to Websocket #2728

Alek99 opened this issue Feb 27, 2024 · 10 comments
Labels
enhancement Anything you want improved feature request A feature you wanted added to reflex help wanted Extra attention is needed

Comments

@Alek99
Copy link
Member

Alek99 commented Feb 27, 2024

Right now there is no reconnecting logic for the websocket. After some investigation, I think we can use the visibilitychange event to re-establish a connection if there isn't one already.

One the tab is visible ie the use clicks on the tab for the reflex app we would do some check and re establish the connection.

Some short example code of how its done in another context:

document.addEventListener('visibilitychange', function() {
  // Check if the tab is now active
  if (document.visibilityState === 'visible') {
    if (!socket.connected) {
      console.log('Socket is disconnected, attempting to reconnect...');
      socket.connect();
    }
  }
});

For those interested in tackling this ticket we probably need to edit https://github.com/reflex-dev/reflex/blob/main/reflex/.templates/web/utils/state.js to incorporate this logic.

@Alek99 Alek99 added enhancement Anything you want improved help wanted Extra attention is needed feature request A feature you wanted added to reflex labels Feb 27, 2024
@Yummy-Yums
Copy link
Contributor

Yummy-Yums commented Feb 27, 2024

hey @Alek99 , dyu mean Once the tab is visible ie the user clicks on the tab ? and by tab , you mean browser tab?

@Alek99
Copy link
Member Author

Alek99 commented Feb 27, 2024

hey @Alek99 , dyu mean Once the tab is visible ie the user clicks on the tab ? and by tab , you mean browser tab?

Correct on both, when the user clicks on the tab and browser tab

@Alek99 Alek99 mentioned this issue Feb 27, 2024
24 tasks
@Yummy-Yums
Copy link
Contributor

hey @Alek99 , dyu mean Once the tab is visible ie the user clicks on the tab ? and by tab , you mean browser tab?

Correct on both, when the user clicks on the tab and browser tab

apart from the obvious impl you stated above and incorprating it in const connect in state.js , is there any thing else to account for ? I wanna work on this

@Alek99
Copy link
Member Author

Alek99 commented Feb 27, 2024

That pretty much it, just needs to reconnect if the tab is old an/or socket connection is lost.

I don't want to rush you on this ticket we really appreciate you wanting to contribute, but this issue is on the higher priority side for us so ideally looking to get this in for the next release if possible. I can suggest other issues if this timeline doesn't work for you, no worries.

@Yummy-Yums
Copy link
Contributor

when is the next release?

@masenf
Copy link
Collaborator

masenf commented Feb 27, 2024

Cut off for 0.4.3 is Thursday Feb 29; Release on March 4

If it's after the 29th, then it will go into the next release on March 11

@Yummy-Yums
Copy link
Contributor

what dyu mean by a tab is old? I thought only it was only reconnecting.

this is what I had in mind, is it valid? i could raise the PR by tmr morning GMT
image

@Alek99
Copy link
Member Author

Alek99 commented Feb 27, 2024

After a while the socket loses connection with the tab, that's what I meant by old tab

@Yummy-Yums
Copy link
Contributor

Yummy-Yums commented Feb 27, 2024

ohk, i'd raise the PR tmr, thanks.

After a while the socket loses connection with the tab, that's what I meant by old tab

you mean if the user spends a long time outside the "reflex tab" , the connection closes and doesn't reconnect, correct ?

@Alek99
Copy link
Member Author

Alek99 commented Feb 27, 2024

ohk, i'd raise the PR tmr, thanks.

After a while the socket loses connection with the tab, that's what I meant by old tab

you mean if the user spends a long time outside the "reflex tab" , the connection closes and doesn't reconnect, correct ?

Thanks! Yeah that's correct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Anything you want improved feature request A feature you wanted added to reflex help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants