/ws-pacemaker

💗 Drop-in WebSocket ping/pong heartbeat handling for Node.js

Primary LanguageJavaScriptMozilla Public License 2.0MPL-2.0

ws-pacemaker

Drop-in WebSocket ping/pong heartbeat handling for the ws Node.js module

npm Build Status

Overview

This module wraps around a WebSocket.Server to send periodic heartbeat pings (via WebSocket#ping) to all connected clients, and closes connections when a corresponding pong is not received (via WebSocket#'pong') within an acceptable timeout.

'use strict';

const WebSocket = require('ws');
const Pacemaker = require('ws-pacemaker');
 
const server = new WebSocket.Server({ port: 8080 });

const pacemaker = new Pacemaker(server, {
  pingInterval: Pacemaker.defaultPingInterval,
  // ^ optional - default: send a ping every 25000 ms
  pongTimeout: Pacemaker.defaultPongTimeout,
  // ^ optional - default: terminate connections after 60000 ms without a pong
});

pacemaker.start();

//pacemaker.stop();
// ^ automatically called when the server closes

Development

Dependency Management

Yarn is recommended for managing dependencies and development tooling.

Code Formatting

Code formatting is handled by prettierx, with a few options tweaked.

To check that the code is correctly formatted:

yarn run check

To auto-format the code:

yarn run fmt

Testing

This project uses Jest as its test framework.

To run all tests:

yarn run test

License

Copyright (C) 2019-2020 Michael Smith <michael@spinda.net>

This program is free software: you can redistribute it and/or modify it under the terms of the Mozilla Public License, version 2.0.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Mozilla Public License for more details.

You should have received a copy of the Mozilla Public License along with this program. If not, see https://www.mozilla.org/en-US/MPL/2.0/.

Helpful resources:

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this work by you shall be licensed as above, without any additional terms or conditions.