Skip to content

Commit

Permalink
[doc] Suggest implementation of heartbeat on the client (#1469)
Browse files Browse the repository at this point in the history
  • Loading branch information
th37rose committed Nov 8, 2018
1 parent 1fd7707 commit 04e69b8
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,31 @@ const interval = setInterval(function ping() {
Pong messages are automatically sent in response to ping messages as required
by the spec.

Just like the server example above your clients might as well lose connection
without knowing it. You might want to add a ping listener on your clients to
prevent that. A simple implementation would be:

```js
function heartbeat() {
clearTimeout(this.pingTimeout);

// Use `WebSocket#terminate()` and not `WebSocket#close()`. Delay should be
// equal to the interval at which your server sends out pings plus a
// conservative assumption of the latency.
this.pingTimeout = setTimeout(() => {
this.terminate();
}, 30000 + 100);
}

const client = new WebSocket(url);

client.on('open', heartbeat);
client.on('ping', heartbeat);
client.on('close', function clear() {
clearTimeout(this.pingTimeout);
});
```

### How to connect via a proxy?

Use a custom `http.Agent` implementation like [https-proxy-agent][] or
Expand Down

0 comments on commit 04e69b8

Please sign in to comment.