# react-native-reconnecting-websocket

> websocket module

Latest version **2.0.0** (published 2024-07-05) · ISC license · 0 weekly downloads

## Install

```sh
npm install react-native-reconnecting-websocket
pnpm add react-native-reconnecting-websocket
yarn add react-native-reconnecting-websocket
bun add react-native-reconnecting-websocket
```

## 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.0 |
| Published | 2024-07-05 |
| First published | 2019-08-02 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 10.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 24 |
| Author | 1498710037@qq.com |
| Maintainers | 1uokun |

## Links

- npm: https://www.npmjs.com/package/react-native-reconnecting-websocket
- Repository: https://github.com/React-Sextant/react-native-reconnecting-websocket
- Homepage: https://github.com/React-Sextant/react-native-reconnecting-websocket#readme
- Issues: https://github.com/React-Sextant/react-native-reconnecting-websocket/issues
- npm.io page: https://npm.io/package/react-native-reconnecting-websocket

## Recent versions

- 2.0.0 (latest) — 2024-07-05
- 1.0.3 — 2020-11-23
- 1.0.2 — 2019-12-23
- 1.0.1 — 2019-10-08
- 1.0.0 — 2019-08-02

## README

React-Native-Reconnecting-WebSocket
=====================

React native auto reconnect websocket extends WebSocketModule.
API design referenced [reconnecting-websocket](https://github.com/joewalnes/reconnecting-websocket).

🇨🇳[中文文档](https://www.jianshu.com/p/a9ead3f2139d)

It is API compatible, so when you have:
```javascript
var ws = new WebSocket('ws://....');
```
you can replace with:
```javascript
import ReconnectingWebSocket from 'react-native-reconnecting-websocket'

var ws = new ReconnectingWebSocket('ws://....');
```
Less code, more exponential.

## Install <a href="https://npmjs.org/package/react-native-reconnecting-websocket"><img alt="npm version" src="http://img.shields.io/npm/v/react-native-reconnecting-websocket.svg?style=flat-square"></a> <a href="https://npmjs.org/package/react-native-reconnecting-websocket"><img alt="npm version" src="http://img.shields.io/npm/dm/react-native-reconnecting-websocket.svg?style=flat-square"></a>
```bash
npm i react-native-reconnecting-websocket
```
## How reconnections occur
With the standard WebSocket API, the events you receive from the WebSocket instance are typically:
```bash
onopen
onmessage
onmessage
onmessage
onclose // At this point the WebSocket instance is dead.
```
With a ReconnectingWebSocket, after an onclose event is called it will automatically attempt to reconnect. In addition, a connection is attempted repeatedly (with a small pause) until it succeeds. So the events you receive may look something more like:
```bash
onopen
onmessage
onmessage
onmessage
onclose
// ReconnectingWebSocket attempts to reconnect
onopen
onmessage
onmessage
onmessage
onclose
// ReconnectingWebSocket attempts to reconnect
onopen
onmessage
onmessage
onmessage
onclose
```
This is all handled automatically for you by the library.

## Parameters

```javascript
var socket = new ReconnectingWebSocket(url, protocols, options);
```

#### `url`
- The URL you are connecting to.
- https://html.spec.whatwg.org/multipage/comms.html#network

#### `protocols`
- Optional string or array of protocols per the WebSocket spec.
- https://tools.ietf.org/html/rfc6455

#### `options`
- Options (see below)

## Options

Options can either be passed as the 3rd parameter upon instantiation or set directly on the object after instantiation:

```javascript
var socket = new ReconnectingWebSocket(url, null, {reconnectInterval: 3000});
```

#### `reconnectInterval`
- The number of milliseconds to delay before attempting to reconnect.
- Accepts `integer`
- Default: `1000`

#### `maxReconnectInterval`
- The maximum number of milliseconds to delay a reconnection attempt.
- Accepts `integer`
- Default: `30000`

#### `reconnectDecay`
- The rate of increase of the reconnect delay. Allows reconnect attempts to back off when problems persist.
- Accepts `integer` or `float`
- Default: `1.5`

#### `timeoutInterval`
- The maximum time in milliseconds to wait for a connection to succeed before closing and retrying.
- Accepts `integer`
- Default: `2000`

#### `maxReconnectAttempts`
- The maximum number of reconnection attempts that will be made before giving up. If null, reconnection attempts will be continue to be made forever.
- Accepts `integer` or `null`.
- Default: `null`

#### `origin`
- Preserve deprecated backwards compatibility for the `origin` option

#### `headers` 
- Specifying `origin` as a WebSocket connection option is deprecated. Include it under `headers` instead
- Accepts `origin` and `Cookie`
- Example:
  ```javascript
  WebSocket(url, '', { headers: { Cookie: 'key=value', origin: "https://secret-host.com" } });
  ```

---

## Methods

#### `ws.close(code?: number, reason?: string)`

- Closes the WebSocket connection or connection attempt, if any. If the connection is already CLOSED, this method does nothing.
- `code` is optional the closing code (default value 1000). [https://tools.ietf.org/html/rfc6455#section-7.4.1](https://tools.ietf.org/html/rfc6455#section-7.4.1)
- `reason` is the optional reason that the socket is being closed. [https://tools.ietf.org/html/rfc6455#section-7.1.6](https://tools.ietf.org/html/rfc6455#section-7.1.6)

#### `ws.send(data: string | ArrayBuffer | ArrayBufferView | Blob)`

- Transmits data to the server over the WebSocket connection.
- Accepts @param data a text string, ArrayBuffer, ArrayBufferView or Blob

#### `ws.ping()`

- Sending websocket ping/pong frame.
- Some servers do not support it and need to be implemented manually, like `How to add heartbeat?`

#### `ws.reconnect()`

- Additional public API method to refresh the connection if still open (close, re-open).
- For example, if the app suspects bad data / missed heart beats, it can try to refresh.


See the more detail in [[react-native/WebSocket.js@3982a2c6]](https://github.com/facebook/react-native/blob/3982a2c6bd116a6dcc6ee6889e4a246b710b70a7/Libraries/WebSocket/WebSocket.js)
<br/>

# For example: How to add heartbeat?
1. usual
```javascript
import ReconnectingWebSocket from 'react-native-reconnecting-websocket'

const ws = new ReconnectingWebSocket("ws://...");

// ws listeners
ws.onopen = (e) => {
    console.log("onopen",e)
};
ws.onmessage = (evt) => {
    console.log("onmessage",JSON.parse(evt.data))
};
ws.onclose = (e) => {
    console.log("onclose",e)
};
ws.onerror = (e) => {
    console.log("onerror",e)
};

/*
 * listen reconnecting event (Powered by ReconnectingWebSocket)
 * @params reconnectAttempts 尝试重连的次数
 */
ws.onconnecting = (reconnectAttempts) => {
    console.log("onconnecting", reconnectAttempts)
}
```
2. add heartbeat
```javascript
ws.onopen = (e) => {
    // execute immediately!
    ws.send("heartbeat string");
    
    heartCheck.reset().start()
};

ws.onmessage = (evt) => {
    heartCheck.reset().start()
};

ws.onclose = () => {
    if(logout){//如果前端主动关闭(如退出登陆)，不做处理

    }else {//如果服务器主动关闭，前端也需要重连时使用
        heartCheck.reset().start() 
    }
};

var heartCheck = {
    timeout: 10000,//default 10s
    timeoutObj: null,
    serverTimeoutObj: null,
    reset:function(){
        clearTimeout(this.timeoutObj);
        clearTimeout(this.serverTimeoutObj);
        return this;
    },
    start:function(){
        let self = this;
        this.timeoutObj = setTimeout(function(){
            if(ws.readyState === ws.OPEN){
              ws.send("heartbeat string");
            }
            self.serverTimeoutObj = setTimeout(function(){
                ws.reconnect();//本库提供
            }, self.timeout)
        }, this.timeout)
    }
}
```

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