Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js "ws" websocket server randomly disconnects client after ~30 seconds, error 1006

I have a simple websocket server like so:

const WebSocket = require('ws');
const wss = new WebSocket.Server( { server: server, path: "/ws" });

wss.on('connection', function connection(ws, req) {

    console.log("Connected");
    ws.on('message', function incoming(message) {
        console.log('received: %s', message);
    });

    ws.on('close', function close(code, reason) {
        console.log("Code: "+code+" | Reason: "+reason);
        console.log('disconnected');
    });

    ws.send('something');
});

I have a client that connects to it fine, but after about 30 seconds of connection, the websocket server closes the connection with an error code of "1006". Google tells me this means the connection was abnormally disrupted. However, I'm not sure what could be causing this. The ws.on('error') callback is also not triggered. How do I keep an indefinite/longer connection with my client?

like image 996
BeardMagician Avatar asked Oct 31 '25 00:10

BeardMagician


1 Answers

Nginx has 30 seconds set as the default timeout.

You'll need to increase this number using a proxy_read_timeout directive.

For example, to extend the timeout to a day (which is probably overkill)

proxy_read_timeout 86400s;

Usually I set this in the location section, so that it can be overridden just for the websocket without impacting areas where it can be desirable to have the default.

like image 105
Shadow Avatar answered Nov 02 '25 13:11

Shadow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!