NodeJs websocket getting disconnected - node.js

My NodeJs server is using websocket and accepting 2 connections, one from a web client (Firefox v83.0) and the other from an Android application.
Everything works fine and messages flow back and forth from the web client to the mobile and the opposite.
However, after what it looks to me some time of non-activity (like 30 or 60 secs) the websocket connection gets disconnected.
I have logged the close event in the server side like this:
wsServer.on('close', (data) => {
console.log("CONNECTION CLOSED")
console.log(data.closeReasonCode)
console.log(data.closeDescription)
})
And what I get is:
1006
Peer not responding.
I googled the 1006 error and the docs only tells connection was closed abnormally...great explanation....thanks :S
I need some help figuring out what is going on here, and what is this peer not responding message that I get. Since I do not know if the issue is in the server, in any of the 2 clients, is it any timer that closes the connection that I am not aware of?
Thanks.

According to "symptoms" I would say you use nginx for reverse proxy.
Try to set
proxy_read_timeout 600s; # deafult is 60 seconds
check docs

Related

Debugging lost HTTPS UPGRADE requests on node

I have a node HTTPS linux server which handles UPGRADE requests to allow secure WebSocket connections as well as other HTTPS requests. Works well 99% of the time.
Periodically and unpredictably, client websocket connection attempts to the server timeout (client gives up after 30 seconds).
I listen for upgrade requests on the HTTPS server as follows:
server.on('upgrade', function upgrade(req, socket, head) {
console.log('Upgrade - Beg - '+req.url);
...
In the cases where the client timeout occurs, I never get the 'upgrade' event.
How can I debug this? Is there a lower-level Node https server event that I can listen for that might indicate something (so that I know the connection is actually getting to the server, for example)?
Notes:
When I detect the timeout on the client side (actually, even before the 30 seconds), I attempt another HTTPS connection to the same server (a POST). It works! The problem only seems to happen with websocket connections.
I have code that retries the websocket connection when it experiences the timeout, but usually it takes several retries before the timeout magically disappears.
Any help in how to debug this would be greatly appreciated.

Sockjs/socketio disconnect delay behind apache proxy

I need to handle users disconnecting from my sockjs application running in xhr-polling mode. When I connect to localhost, everything works as expected. When I put apache between nodejs and browser, I get ~20 sec delay between closed browser and disconnect event inside nodejs. My apache proxy config is following:
<Location />
ProxyPass http://127.0.0.1:8080/
ProxyPassReverse http://127.0.0.1:8080/
</Location>
The rest of the file is default, you can see it here. I tried playing with ttl=2 and timeout=2 options, but either nothing changes, or I get reconnected each 2 seconds without closing browser. How can I reduce additional disconnect timeout, introduced, but apache, somewhere in its defaults?
It's possible that your Apache server is configured to use HTTP Keep Alive which will keep a persistent connection open. In that case I would try disabling KeepAlive, or lowering the KeepAliveTimeout setting in your Apache configuration to see if this solves the problem.
If that doesn't work, I would also take a look at netstat and see what is the status of each socket and start a root cause analysis. This chart gives is the TCP state machine and can tell you where each connection is. Wireshark can also give you some information on what is going on.
In long polling the connection happens like below
<client> ---> apache ---> <node.js>
When client breaks the connection
<client> -X-> apache ---> <node.js>
Apache still keeps the connection open. Now there are two workaround to this
ProxyTimeout
You can add below to your apache config
ProxyTimeout 10
This will break the connection after 10 seconds, but then this break every long polling connection after 10 seconds, which you don't want
Ping
Next option is to ping the client
pingTimeout (Number): how many ms without a pong packet to consider the connection closed (60000)
pingInterval (Number): how many ms before sending a new ping packet (25000).
var io = require('socket.io')(server, { 'transports': ['polling'], pingTimeout: 5000, pingInterval: 2500});
Above will make sure the client is disconnected within 5 seconds of going off, you can lower it again further but then this may impact the usual loading scenarios
But reading through all the posts, threads and sites, I don't think you can replicate the behavior you get when connect to socket.io directly, because the connection break then can be detected easily by socket.io
The 20 sec delay not in the apache proxy. I got the same issue, delay not happening in the local URL, but delay happening in global URL.
The issue was solved in the NodeJs itself. Need to send one time data from server to client to make sure it's initialized. This problem not in the documentation and issues blog at the WebSocket plugin.
Send a dummy message after request accepted in the server, like below.
let connection = request.accept(null, request.origin);
connection.on('message', function (evt) {
console.log(evt);
});
connection.on('close', function (evt) {
console.log(evt);
});
connection.send("Success"); //dummy message to the client from server

Socket.io connection to server doesn't work sometimes

I have setup a Node.JS server with socket.io on a VPS and I broadcast every 10 seconds the number of connected clients to all. This usually works fine, though often times, the connection can't be established and I get this error (I changed the IP a bit):
GET http://166.0.0.55:8177/socket.io/1/?t=1385120872574
After reloading the site, usually the connection can be established, though I have no idea why the failed connection happens in the first place, also I don't know how to debug the socket.io code. Sometimes I can't connect to the server anymore and I have to restart the server.
Additional information:
My main site runs on a different server (using a LAMP environment with CakePHP) than the Node.js server.
I use forever to run the server
I have a lot of connected clients (around 1000)
My VPS has 512 MB Ram and the CPU is never higher than 25%
After top command, try:
socket.on('error', function (err) {
console.log("Socket.IO Error");
console.log(err.stack); // this is changed from your code in last comment
});
Also, you could try a the slower transport. Socket.io use Websocket by default but if your server cannot allocate enough resource, you can try another transport which is slower but use less resources
io = socketIo.listen(80);
io.set('transports', ['xhr-polling']);

How to check socket.io connection locally?

My app is in browser, which connects to socket.io server, it works fine, but, when client disconnect for a while ( sleep for example ) , the connection will be automatically closed by server, this is the default behavior for socket.io
How can I re-establish the connection at client side without refreshing the page? is there a status that would tell me that connection is currently off at client side? so that I re-connect when necessary?
I can't rely on an event, I think I need to know if connection is on or off in an easy way locally .. well?
socket.io does reconnect automatically. To check if your socket is connected check socket.socket.connected has Boolean value. See socket.socket.connecting if you are trying to connect.
var socket = io.connect(''); //reconnects by default
console.log(socket.socket.connected);
console.log(socket.socket.connecting);

Node.js and Socket.IO Not Responding in Some Servers

I have simple Node.js and Socket.IO app running on Nodejitsu. The App is running perfectly from home and some other Internet services but not working from my work!
Here is the error Message which I am getting
The connection to ws://behseini-socketproject.jit.su/socket.io/1/websocket/QtVm6E79WwGLwBVjN7el was interrupted while the page was loading.
this.websocket = new Socket(this.prepareUrl() + query);
socket.io.js (line 2371)
GET http://behseini-socketproject.jit.su/socket.io/1/?t=1370531712581 200 OK
535ms
socket.io.js (line 1659)
ParamsHeadersResponse
fPtjDNnugmPPXKbnN7ep:60:60:websocket,htmlfile,xhr-polling,jsonp-polling
Can you please let me know why this is happening? is this kind of Firewall issue?
Thanks
After talking with you on the #nodejitsu IRC channel, it seems that this may indeed be a problem with your work proxy/firewall.
As was suggested to you in the chat last night, you could always try to use secure websocket connections (wss rather than ws) to see if that will allow your connections to pass through.
If that doesn't work, you would need to send your connection outside of your work's connection. For instance, proxying into a computer at your home and using your home network while you're at work and want to use websockets.

Resources