Ws: How do we enable ping/pong?

Created on 24 Jan 2017  路  9Comments  路  Source: websockets/ws

Can someone please explain how the ping/pong works with this module?

question

Most helpful comment

The endpoint receiving the ping frame (including the browser) will automatically respond with a pong frame. This is per spec.

All 9 comments

Hi,
ping/pong works as specified in the RFC.
An endpoint can send a ping frame via ws.ping() at any time after the connection is established.
Upon receipt of a ping frame, an endpoint sends a pong frame in response which triggers the pong event.

Using this one can implement a heartbeat system to check if the connection is still alive.

Maybe in future, after 2.0.0 is released we can add a built-in version of this heartbeat/keep-alive system basing it on the awesome work done by @FlorianBELLAZOUZ in #635.

Thanks @lpinca. Is the connecting client expected to respond to the ping or would the browser take care of that?

The endpoint receiving the ping frame (including the browser) will automatically respond with a pong frame. This is per spec.

Thats great, thanks for taking the time to explain it!

Have a question....
Why am I receiving a "pong" event from the client twice? I applyed the example given in this main project page.

// On pong from the store... keep it alive ws.on('pong', function heartbeat() { this.isAlive = true; console.log('Pong from store: ' + this.store_id); });

@voxpopmxadmin

hello, the server, depending on the implementation, sends a few ping while the connection is alive - and the web for each ping corresponds to pong - in order to keep the connection alive - as soon as it stops responding pong, the server learns that the clip is knocked out

js & websocket & ping / pong

seems not work ?


 // ping
ws.send(0x9);
// pong
ws.send(0xA);

still not work

ws.onping = function(e) {
    log(`ping`, ws.readyState);
    // ws.ping();
};

ws.onpong = function(e) {
    log(`pong`, ws.readyState);
    // ws.ping();
};

ws.on(`ping`, function heartbeat() {
    log(`ping`);
    // this.isAlive = true;
});

ws.on(`pong`, function heartbeat() {
    log(`pong`);
    // this.isAlive = true;
});

ws !== js websocket

I'm so sorry for that missing understand.

https://github.com/websockets/ws#how-to-detect-and-close-broken-connections

image

Have a question....
Why am I receiving a "pong" event from the client twice? I applyed the example given in this main project page.

ws.on('pong', function heartbeat() {
  this.isAlive = true;
  console.log('Pong from store: ' + this.store_id);
});

what's wrong with this?

Uncaught TypeError: ws.on is not a function

image

Have a question....
Why am I receiving a "pong" event from the client twice? I applyed the example given in this main project page.

ws.on('pong', function heartbeat() {
    this.isAlive = true;
    console.log('Pong from store: ' + this.store_id);
});

what's wrong with this?

Uncaught TypeError: ws.on is not a function

image

that error is beacuse you are using the pure JS WebSocket not the ws library from node

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cmnstmntmn picture cmnstmntmn  路  4Comments

pmcalabrese picture pmcalabrese  路  4Comments

ImBundle picture ImBundle  路  3Comments

NodePing picture NodePing  路  5Comments

anonaka picture anonaka  路  5Comments