Socket.io client disconnection issues on NodeJS - node.js

I have an issue with Socket.io: everytime I close and reopen or refresh the page on the JS Client, for some reason the "old socket.io sockets" are not disconnected/destroyed, I still see them receiving messages from a fake sender on the NodeJS server.
So my debugging console is filled with ghost socket.io sockets receiving a message and I don't know how to destroy them, I thought they would close automatically once the browser tab would have been closed.
How do I force a socket.io socket to disconnect?
I have tried socket.removeAllListeners(), socket.disconnect(), and other roads.
I guess I'm doing something really stupid but I don't get what
For simplicity I have just created a merged file with the poc: https://gist.github.com/Fabryz/c7b2af02682f7d97474eec4e0dc75645
Thanks in advance

Related

How can i use socket.io globally in node.js

Currently I am using an API to get chatList, so the user cannot know if they have received a new msg. They have to refresh the page in order to know that there is a new message.
I am connecting with the socket when the user is in chat and they get disconnected with socket when they leave chat and move to some other page.
But I want them to stay connected if they move to some other page.
Can you please guide me how can I achieve that? Is there any general socket available for applications ?
socket.io and webSocket connections cannot survive from one page to the next. When you leave one page and go to another, all resources from the first page (including any webSocket or socket.io connections) will be closed. That's how the browser works.
You can make a single page app (SPA) where the user clicks on things and the view changes, but it's just loading dynamic content into the same page so the actual web page never closes in the browser and thus the socket.io connection can persist.
Or, you can tag the user with a cookie so that when they leave the page, their connection closes and then they load another page and a new connection is established, your server can see that they are the same user that was just connected and your server can set things up appropriately for them to continue as if the connection was never redone.
Is there any general socket available for applications ?
No, there is not.
I am connecting with the socket when the user is in chat and they get disconnected with socket when they leave chat and move to some other page.
Yes, all browsers disconnect when the web page closes.

How to debug websocket client losing connection and reestablishing it

I'm using node.js, websockets/ws.
In my site sometimes a random client loses connection without losing connection to other internet stuff. Less then a second later they connect back, with a new socket. (There is a code that calls socket.onClose, which tries to reconnect back to server)
On the server side I can't see or log anything wrong. Everything looks like a normal disconnect, same as closing the browser tab.
I am guessing the reason is either socket related or client related but I don't know where to begin to debug this problem.
I got ping/pong responses with 60 second timer, this isn't it. The user usually loses connection while active.
How can I debug this problem and find the reason?
I keep all the session info, data, within the socket and that is why I do not want people to lose their connection.
Thanks

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?
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.
It seems now that WebWorker are a more widespread technology that it could be use to share websocket.
As explain in this article https://crossbario.com/blog/Websocket-Persistent-Connections/
Webworker are Javascript that is running outside the "thread of the page" and thus are not deleted on page change.
Note that it is running only in the same domain.
You can also look at Kanaka's answer here How to maintain a WebSockets connection between pages? (2012-2017 answer beware)

Socket.io: correct way for the server to reconnect the client

I'm building MEAN application with socket.io. When page is just loaded, socket connection is established and kept live while user navigates to various pages, thanks to single-page nature of the app.
The user information is available in my socket connection thanks to passport.socketio.
However, when user logs in or out, I want the connection to be re-initialized, since otherwise socket will contain obsolete data about the user. Currently, I tried to implement it in this way: when user logs in / out, server disconnects this particular client's socket by calling socket.disconnect();.
On the client side, I listen for disconnect event, and try to re-establish the connection, like this:
_socket.on('disconnect', function(reason) {
_socket.connect();
});
Ok, now, when user logs out or in, server disconnects the client, this client connects back, and user information in the socket is up-to date. So far, so good.
But, consider different case when connection is broken: server is restarted. Previously, it "just worked": when I stop my server, connection is broken, but when I start server again, connection is automatically re-established. But after I've added my _socket.connect(); call, it doesn't work anymore: connection is still down until I refresh the page in the browser.
I've checked that when server calls disconnect();, the reason given to disconnect handler is: io server disconnect. And when server stops, the reason is: transport close.
Ok, then, I've implemented my disconnect handler as follows:
_socket.on('disconnect', function(reason) {
if (reason === 'transport close'){
// don't do anything special
} else {
_socket.connect();
}
});
Now it works. But, all of it seems as absolute dirty hack. At the very least, the reasons given (io server disconnect and transport close) seem to be just human-readable strings, so they might change in the future, and this will cause my code to stop working. And, well, there should be better way to do this; I must miss something essential, but unfortunately I can't find any good documentation on socket.io.
So, the question is: what is the correct way for the server to reconnect some particular client?
Additionally, if you have any recommendations on resources to learn about socket.io, I'd appreciate it very much as well.

User not disconnecting in Socket.IO Node.Js app ONLY on https connection - caching issue?

I'm having this issue with my Socket.Io / Node.Js app that when I reload a page Socket.IO still thinks that the "previous" me is connected to the chat room, so it thinks there's two people in the chat room instead of one. After a while (maybe like a minute) this problem disappears.
I tried resolving it but I think there's some issue with cache - that even though I reload the browser window the previous session is still active, so it "thinks" two people are connected.
This issue doesn't occur at all times, but always on HTTPS connection, and almost always on iOS and sometimes in Safari / Chrome and other browsers (on all systems with https connection).
Do you know what the issue might really be and what would be the best way to resolve it?
I use all the standard code for setting up the Socket.IO connection and the app is running on Express / Node.Js.
I can put the code here, but it's quite a lot, the open source code is available on http://github.com/noduslabs/infranodus (the app.js file and public/entries.js is where the socket.io code is).
Thank you!

Resources