# reconnecting-ws

> wrapper to WS client that try reconnect to server when disconnect by any reasone

Latest version **2.0.4** (published 2020-09-26) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.4 |
| Published | 2020-09-26 |
| First published | 2018-05-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 28.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Haim Kastner |
| Maintainers | haimkastner |
| Keywords | ws, websocket, reconnect, ws client |

## Links

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

## Dependencies (1)

- [ws](https://npm.io/package/ws.md) ^7.3.1

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 2.0.4 (latest) — 2020-09-26
- 2.0.3 — 2019-04-16
- 2.0.2 — 2019-02-25
- 2.0.1 — 2019-02-25
- 2.0.0 — 2019-02-25
- 1.0.4 — 2018-07-02
- 1.0.3 — 2018-06-14
- 1.0.2 — 2018-05-30
- 1.0.1 — 2018-05-30
- 1.0.0 — 2018-05-30

## README

# reconnecting-ws
Wrap WS client to auto reconnect websocket server when disconnected by any reason.

The code is based on https://github.com/websockets/ws/wiki/Websocket-client-implementation-for-auto-reconnect


## Installing

```
npm install --save reconnecting-ws
```

## Using

```typescript
import { WebSocketClient } from 'reconnecting-ws';


class SocketClient {

    private webSocketClient: WebSocketClient;
    constructor() {
        /** 
         * Init WebSocketClient
         * Set trying reconnect when connection lost each 5 seconds, and show console message 
         */
        this.webSocketClient = new WebSocketClient(5000, true);

        /** Subscribe to emitters. */
        this.webSocketClient.on('open', this.onOpen);
        this.webSocketClient.on('error', this.onError);
        this.webSocketClient.on('message', this.onMessage);
        this.webSocketClient.on('close', this.onClose);
        this.webSocketClient.on('reconnect', this.onReconnect);

        /** Connect to server. */
        this.webSocketClient.connect('ws://127.0.0.1:3001');

        /** 
         * It`s possible to access WebSocket instance directly.
         * without using wrapper of WebSocketClient. (see https://github.com/websockets/ws).
         * Note that when trying to reconnect *all* listeners will be removed
         */
        const bufferAmount = this.webSocketClient.WebSocketInstance.bufferedAmount;

        /** Disconnect manually in minute. */
        setTimeout(() => {
            this.webSocketClient.disconnect();
        }, 60000);
    }

    private onOpen() {
        // TODO
        console.log('onOpen');
    }

    private onClose(code: number, reason: string) {
        // TODO
        console.log('onClose');
    }

    private onError(err: Error) {
        // TODO
        console.log('onError');
    }

    private onMessage(data: WebSocket.Data) {
        // TODO
        console.log('onMessage');

    }

    private onReconnect() {
        // TODO
        console.log('onReconnect');

    }
}
```

For real example see `example` folder.

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