ws-heartbeat
Server-side and client-side heartbeat library for ws and browser-side Websocket.
install
npm i ws-heartbeat
client side usage
import { setWsHeartbeat } from "ws-heartbeat/client";
const ws = new WebSocket("ws://localhost:8000");
setWsHeartbeat(ws, '{"kind":"ping"}');
options:
setWsHeartbeat(ws, '{"kind":"ping"}', {
pingTimeout: 60000,
pingInterval: 25000,
});
server-side usage
import { setWsHeartbeat } from "ws-heartbeat/server";
import * as WebSocket from "ws";
const wss = new WebSocket.Server();
setWsHeartbeat(wss, (ws, data, binary) => {
if (data === '{"kind":"ping"}') {
ws.send('{"kind":"pong"}');
}
});
options:
setWsHeartbeat(wss, (ws, data, flag) => {
if (data === '{"kind":"ping"}') {
ws.send('{"kind":"pong"}');
}
}, 60000);