Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-native-reconnecting-websocket

Package Overview
Dependencies
Maintainers
0
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-reconnecting-websocket - npm Package Compare versions

Comparing version 1.0.3 to 2.0.0

6

index.js

@@ -1,4 +0,6 @@

import {NativeModules} from 'react-native'
const {WebSocketModule} = NativeModules;
// import {NativeModules} from 'react-native'
// const {WebSocketModule} = NativeModules;
import WebSocketModule from 'react-native/Libraries/WebSocket/NativeWebSocketModule';
let settings = {

@@ -5,0 +7,0 @@ /** The number of milliseconds to delay before attempting to reconnect. */

{
"name": "react-native-reconnecting-websocket",
"version": "1.0.3",
"version": "2.0.0",
"description": "websocket module",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -15,2 +15,4 @@ React-Native-Reconnecting-WebSocket

```javascript
import ReconnectingWebSocket from 'react-native-reconnecting-websocket'
var ws = new ReconnectingWebSocket('ws://....');

@@ -24,4 +26,33 @@ ```

```
## 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

@@ -77,2 +108,13 @@

#### `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" } });
```
---

@@ -82,9 +124,35 @@

See the detail in [react-native/WebSocket.js](https://github.com/facebook/react-native/blob/master/Libraries/WebSocket/WebSocket.js)
#### `ws.close(code?: number, reason?: string)`
## How to add heartbeat?
- 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
ws = new WebSocket("ws://...");
import ReconnectingWebSocket from 'react-native-reconnecting-websocket'
const ws = new ReconnectingWebSocket("ws://...");
// ws listeners
ws.onopen = (e) => {

@@ -103,4 +171,6 @@ console.log("onopen",e)

// add listen connecting event
// @params reconnectAttempts 尝试重连的次数
/*
* listen reconnecting event (Powered by ReconnectingWebSocket)
* @params reconnectAttempts 尝试重连的次数
*/
ws.onconnecting = (reconnectAttempts) => {

@@ -123,2 +193,10 @@ console.log("onconnecting", reconnectAttempts)

ws.onclose = () => {
if(logout){//如果前端主动关闭(如退出登陆),不做处理
}else {//如果服务器主动关闭,前端也需要重连时使用
heartCheck.reset().start()
}
};
var heartCheck = {

@@ -136,6 +214,7 @@ timeout: 10000,//default 10s

this.timeoutObj = setTimeout(function(){
ws.send("heartbeat string");
if(ws.readyState === ws.OPEN){
ws.send("heartbeat string");
}
self.serverTimeoutObj = setTimeout(function(){
ws.close();
ws.reconnect();
ws.reconnect();//本库提供
}, self.timeout)

@@ -146,9 +225,1 @@ }, this.timeout)

```
3. add new API `reconnect()`
```javascript
/**
* 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.
*/
ws.reconnect()
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc