I have a socket.io client connected to a node.js server. If I kill node.js at the command line, the client immediately freezes (i.e., communication stops), but there is a ~20 second delay before the "disconnect" event is fired. Is this behavior by design? Is there a configuration option to reduce the delay in firing the disconnect event?
It appears that this behavior changed in a relatively recent (last 6 months) update of socket.io. Before the reconnect functionality was built in to socket.io itself, I implemented my own reconnect logic using a "disconnect" event handler and at that time the "disconnect" event fired almost instantly when server communication halted.
I think this is likely a design pattern. The client may be presuming the server is 'temporarily' unreachable (network trafic etc) and essentially will keep trying to reach it... until the client timeout kicks in.
I send a disconnect (socket.disconnect()) to the server directly from the client, and I don't get this issue.
Related
In my express/socket app, (which is running behind HAproxy server), I am using sticky session(cookie based) to route requests to same worker. I have total 16 processes running (8 /machine- 2 machines). Socket session data is being stored in Redis adapter.
The problem I have is, when an event is fired from server, client can't receive it. Inspite, it will keep throwing disconnection errors, after every few seconds (4-5) :
Update : It will only fire event if transport was opened when event was fired, which is getting closed instantly, and than restarting.
Can someone please suggest something on this..
Finally, I found the solution. It was timeout client which was set to too low in HAproxy config. Increasing it, fixed the issue.
I'm sorry if this is the wrong stack for this question.
I have a Node.js server running on Heroku. Whenever I commit something, the server restarts. I want to warn the users that there's going to be a restart so that I don't completely ruin their experience. Just a simple notification with the text "Server restart in X minutes".
Let's say I have the client side all set up and a Socket.IO emit is all that's needed for the notification to be shown. How would I do that? I thought of having some sneaky function on the client side that would make the server emit the notification, but I'm afraid it can be easily exploited.
The answer is to detect the SIGTERM signal that Heroku sends to your app to shut it down, and once that signal is detected, to emit the notification to every connected client:
process.on('SIGTERM', () => {
// send your signal
})
https://devcenter.heroku.com/articles/dynos#shutdown
However, a better user experience is just to ensure that users never even know your server restarted. With node this shouldn't be difficult, you only need:
To ensure your process ends and starts quickly (quickly being a few seconds)
To ensure your clients all have reconnection logic (this is built into socket.io so you shouldn't have to do anything)
Optionally, to turn on preboot, which will make the delta between one server going down and another coming up be close to zero (heroku features:enable -a myapp preboot)
https://devcenter.heroku.com/articles/preboot#enabling-and-disabling-preboot
I'm writing a chat application with Node.js and ws
there is a on close event which is fired when user gracefully disconnects but when the internet is lost, this event is not fired.
is there anyway we can detect when user has lost the connection?
Best way is to handle own ping/pong methods. It's usable also for measure connection latency.
I have a NodeJS server set up that accepts TLS connections using the tls module: http://nodejs.org/api/tls.html
The clients are are using the NodeJS TLS module for the connections. I'm also storing a list/hashmap of all connected client and their IDs. If a client disconnects, then I will remove it from the list using the "error", "clientError" and "close" events.
This works in any normal case - however, when I "kill" the client (unplug power, unplug network cable) it seems like there is no event fired and the stream is open forever. Maybe I have overlooked something, but is there an event for something like this or how can I detect when the stream is not there any longer?
Sure, I could poll it in a certain interval, but that does not sound pretty good, since it will cause a lot of traffic (for almost no reason).
In the end, the stream is actually closed. If you try to call write, then it will cause an "write after end" error. Sadly, it seems like there is no event fired when the stream itself closes.
So right now, I'm just trying to write something every few minutes to see if the stream is still alive.
I'm using Node.JS and Socket.IO to create a chat, I use XHR polling with a duration of 10 to host it on Heroku.
When I leave the page with my client, I'd like to calll the disconnect method automatically on the server which removes it from other clients. It's in fact done but... 2 minutes after the client has really left the page, which is quite awkward.
Is there a way to call the disconnect method as soon as the client doesn't reply ? Or at least being a little faster to know when the client is not there anymore ?
I've tried to close the socket client-side on page exit with jQuery but it doesn't work. The problem is that using socket.disconnect() is asynchronous and non-blocking and the user leaves before the request is completed...
You can play with heartbeat timeout, heartbeat interval and close timeout values of socket.io configuration.
Search for heartbeat and timeout in this page: https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO
If you set the 'sync disconnect on unload' option on the socket.io client to true, it will send a synchronous xhr disconnect request in onbeforeunload that will disconnect the socket.