Can someone please explain how the ping/pong works with this module?
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
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;
});
I'm so sorry for that missing understand.
https://github.com/websockets/ws#how-to-detect-and-close-broken-connections

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); });
Uncaught TypeError: ws.on is not a function

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
that error is beacuse you are using the pure JS WebSocket not the ws library from node
Most helpful comment
The endpoint receiving the ping frame (including the browser) will automatically respond with a pong frame. This is per spec.