Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io keep the connection on client side when client refresh or open new page

Have some way to keep the same socket.io connection on client side if user open a new page or refresh the page, maybe store the socket in session or it's impossible?

like image 342
John Avatar asked Jul 21 '17 18:07

John


People also ask

Does socket.io reconnect automatically?

In the first case, the Socket will automatically try to reconnect, after a given delay.

Does socket.io reconnect after disconnect?

Socket disconnects automatically, reconnects, and disconnects again and form a loop. #918.


1 Answers

It's impossible. You cannot keep the same socket.io or webSocket client connection when the page is changed or refreshed. The browser simply does not do that. When a new page is loaded or the current page is refreshed, all resources from the previous page are closed and freed by the browser, including socket.io/webSocket connections.

So, your server has to expect a new socket.io connection from the newly loaded page. If you use cookies or a server-side session object, you can identify, on the server, when a connection is coming from a client that you have previously seen and the server can then act accordingly to realize that this is just a previous client reconnecting on a new page.

like image 141
jfriend00 Avatar answered Oct 24 '22 19:10

jfriend00