# ws-heartbeat

> Server-side and client-side heartbeat library for ws and browser-side Websocket.

Latest version **1.3.0** (published 2024-10-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install ws-heartbeat
pnpm add ws-heartbeat
yarn add ws-heartbeat
bun add ws-heartbeat
```

## Health

**Score 25/100 (F)** — status: maintenance-mode.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.0 |
| Published | 2024-10-04 |
| First published | 2016-12-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 6.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 17 |
| Author | York Yao |
| Maintainers | plantain_00 |

## Links

- npm: https://www.npmjs.com/package/ws-heartbeat
- Repository: https://github.com/plantain-00/ws-heartbeat
- Homepage: https://github.com/plantain-00/ws-heartbeat#readme
- Issues: https://github.com/plantain-00/ws-heartbeat/issues
- npm.io page: https://npm.io/package/ws-heartbeat

## Dependencies (1)

- [ws](https://npm.io/package/ws.md) 7 || 8

## Recent versions

- 1.3.0 (latest) — 2024-10-04
- 1.2.0 — 2020-11-17
- 1.1.0 — 2019-07-06
- 1.0.5 — 2019-07-04
- 1.0.4 — 2017-06-29
- 1.0.3 — 2016-12-29
- 1.0.2 — 2016-12-29
- 1.0.1 — 2016-12-29
- 1.0.0 — 2016-12-29

## README

[![Dependency Status](https://david-dm.org/plantain-00/ws-heartbeat.svg)](https://david-dm.org/plantain-00/ws-heartbeat)
[![devDependency Status](https://david-dm.org/plantain-00/ws-heartbeat/dev-status.svg)](https://david-dm.org/plantain-00/ws-heartbeat#info=devDependencies)
[![npm version](https://badge.fury.io/js/ws-heartbeat.svg)](https://badge.fury.io/js/ws-heartbeat)
[![Downloads](https://img.shields.io/npm/dm/ws-heartbeat.svg)](https://www.npmjs.com/package/ws-heartbeat)

# ws-heartbeat
Server-side and client-side heartbeat library for ws and browser-side Websocket.

#### install

`npm i ws-heartbeat`

#### client side usage

```ts
import { setWsHeartbeat } from "ws-heartbeat/client";
// import * as WebSocket from "ws";
const ws = new WebSocket("ws://localhost:8000");

setWsHeartbeat(ws, '{"kind":"ping"}');
```

options:

```ts
setWsHeartbeat(ws, '{"kind":"ping"}', {
    pingTimeout: 60000, // in 60 seconds, if no message accepted from server, close the connection.
    pingInterval: 25000, // every 25 seconds, send a ping message to the server.
});
```

#### server-side usage

```ts
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"}') { // send pong if recieved a ping.
        ws.send('{"kind":"pong"}');
    }
});
```

options:

```ts
setWsHeartbeat(wss, (ws, data, flag) => {
    if (data === '{"kind":"ping"}') {
        ws.send('{"kind":"pong"}');
    }
}, 60000); // in 60 seconds, if no message accepted from client, close the connection.
```

---
_Source: https://npm.io/package/ws-heartbeat · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
