Ws: websocket closing connection automatically

Created on 26 Apr 2020  路  11Comments  路  Source: websockets/ws

Description

i have deployed websocket server on heroku and Everything is working good but if there is no transfer between server and client after a certain time the connection is closed. i don't know who is closing the connection server or client.

Client code

const Websocket = require('ws');

var ws = new Websocket('https://secure-mountain-02060.herokuapp.com/');

ws.onmessage = function(event){
console.log(event.data);
}

ws.onclose = function(){
console.log('server close');
}

Most helpful comment

Closing as answered. Discussion can continue if needed.

All 11 comments

Try with sending pings as shown here https://github.com/websockets/ws#how-to-detect-and-close-broken-connections. The server is probably closing connections due to inactivity.

Try with sending pings as shown here https://github.com/websockets/ws#how-to-detect-and-close-broken-connections. The server is probably closing connections due to inactivity.

what is the default time out ?

I don't know you have to ask it to Heroku. The connection is probably closed by their load balancer. Try to send pings from the client every 30 sec.

function noop() {}

const insterval = setInterval(function() {
  ws.ping(noop);
}, 30000);

interval.unref();

is there any function to reconnect the client ?

No, you have to create a new WebSocket. There are many modules/wrapper on npm that can help you with reconnection.

Closing as answered. Discussion can continue if needed.

What is noop supposed to be? The readme says that pong messages will automatically be sent, but this doesn't seem to be the case

What is noop supposed to be? The readme says that pong messages will automatically be sent, but this doesn't seem to be the case

i am also confused @maxp-hover.
please help @lpinca.

The example used it to ignore errors if the WebSocket ready state was not open. It is no longer needed now.

const WebSocket = require('ws');

const ws = new WebSocket('wss://echo.websocket.org/');

ws.on('open', function () {
  console.log('connected');

  setInterval(function() {
    ws.ping();
  }, 10000);
});

ws.on('pong', function () {
  console.log('pong');
});

is there any other method to resolve this problem actually I am now trying to implement a android client, every thing is working fine but facing the same issue of connection closing.
I am asking it because i have a thought that doing ping will decrease the efficiency of the app
please help @lpinca

is there any other method to resolve this problem actually I am now trying to implement a android client, every thing is working fine but facing the same issue of connection closing.
I am asking it because i have a thought that doing ping will decrease the efficiency of the app
please help @lpinca

The ping (with a reasonable interval) impact of performance should be negligible even when using the websocket to stream continuous data. As a side note, Android will close the ws connection (but that's not an issue of this lib and you should look for help elsewhere)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cmnstmntmn picture cmnstmntmn  路  4Comments

dcflow picture dcflow  路  4Comments

robertmylne picture robertmylne  路  3Comments

ORESoftware picture ORESoftware  路  3Comments

NodePing picture NodePing  路  5Comments