Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do we enable ping/pong? #977

Closed
john-doherty opened this issue Jan 24, 2017 · 9 comments
Closed

How do we enable ping/pong? #977

john-doherty opened this issue Jan 24, 2017 · 9 comments
Labels

Comments

@john-doherty
Copy link

john-doherty commented Jan 24, 2017

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

@lpinca
Copy link
Member

lpinca commented Jan 25, 2017

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.

@john-doherty
Copy link
Author

john-doherty commented Jan 25, 2017

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

@lpinca
Copy link
Member

lpinca commented Jan 25, 2017

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

@john-doherty
Copy link
Author

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

@voxpopmxadmin
Copy link

voxpopmxadmin commented Jun 4, 2018

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);
});

@YarikST
Copy link

YarikST commented Jun 26, 2018

@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

@xgqfrms
Copy link

xgqfrms commented Jul 30, 2019

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

@xgqfrms
Copy link

xgqfrms commented Jul 30, 2019

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

@vrfox
Copy link

vrfox commented Oct 30, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants