-
Notifications
You must be signed in to change notification settings - Fork 10.1k
how to stay connected to Socket Server from the Client even on refresh on any browser #5380
-
Hello,
I've written Sockets.IO Server Code in Nodejs
+ Expressjs
with TypeScript
and the Server code is working perfectly fine.
but I am struggling for the Reactjs
Client Side Web App, where I am not able to stay connected to the Sockets Server even on the page refresh, if I am refreshing the Page, then it's getting disconnected and then connect again, but I can't able to find a way to stay connected to the Sockets Server even on page refresh...
Furthermore, Socket Client Options I tried this as well, but this also not working in my Reactjs App.
can anyone help me on this?
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 3 comments 2 replies
-
Hi!
The WebSocket connection does not survive the page reload. If you need to track which user is connected, you will need to have some kind of authentication (a cookie, for example).
Reference: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
Beta Was this translation helpful? Give feedback.
All reactions
-
Hello @darrachequesne -- so I can say that, it's the normal & expected behavior of SocketsIO to get disconnect on the page reload?
Beta Was this translation helpful? Give feedback.
All reactions
-
Yes, that's right. As far as I know, there are no way to persist the connection across page reloads, even within a SharedWorker, which will be killed once there is no more tab connected to it.
References:
Beta Was this translation helpful? Give feedback.
All reactions
-
but how Cookie or something for some kind of authentication will work? @darrachequesne
Beta Was this translation helpful? Give feedback.
All reactions
-
You can utilize PWA and service workers if you really need to be connected after refresh.
Beta Was this translation helpful? Give feedback.