Reputation: 41
We're using PM2 to run a websocket for our application. A few days ago the PM2 task for our websocket suddenly stopped running.
We have tried to figure out why via the logs of PM2 and winston, which we use to log additional information about the socket, but sadly we couldn't figure out why the websocket stopped.
Since this isn't the first time this happend, we would like to maybe get some hints on how to run our websocket a bit smoother without dying constantly.
Thanks in advance!
Upvotes: 2
Views: 676
Reputation: 108641
You did not mention which npm package you use in your nodejs app to handle websockets. Nor did you mention what your nodejs app connects to at the far end, or which end initiates the connection. All those things are important to troubleshooting.
I'll try to point you in the right direction.
If you're using npm's ws package you'll get close and error events from your connections. (You will in a browser too.) The close event, in particular, carries two parameters: code
and reason
. Codes have values 1000 - 4999. You need to look at the code value to know why the connection dropped. You need to look at the codes and reasons at both ends of the dropped connection to know why.
You may, in your code that accepts connections, need to implement the websocket ping/pong protocol. Idle connections get dropped by various parts of the network.
Notice that browsers cannot accept websocket connections, only originate them. Also notice that browsers automatically pong when they get ping requests from their websocket servers. All your servers have to do is send pings and listen for pong events.
And, because we're dealing with a global decentralized network, connections sometimes drop for no good reason. Robust software has ways to re-establish dropped connections.
Upvotes: 1