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
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-reconnecting-websocket

websocket module

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
190
decreased by-17.75%
Maintainers
1
Weekly downloads
 
Created
Source

React-Native-Reconnecting-WebSocket

React native auto reconnect websocket extends WebSocketModule. API design referenced reconnecting-websocket.

It is API compatible, so when you have:

var ws = new WebSocket('ws://....');

you can replace with:

var ws = new ReconnectingWebSocket('ws://....');

Less code, more exponential.

Parameters

var socket = new ReconnectingWebSocket(url, protocols, options);
url
protocols
options
  • Options (see below)

Options

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

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

Methods

See the detail in react-native/WebSocket.js

How to add heartbeat?

  1. usual
ws = new WebSocketJs("ws://...");

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)
};
  1. add heartbeat
ws.onopen = (e) => {
    // execute immediately!
    ws.send("heartbeat string");
    
    heartCheck.reset().start()
};

ws.onmessage = (evt) => {
    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(){
            ws.send("heartbeat string");
            self.serverTimeoutObj = setTimeout(function(){
                ws.close();
            }, self.timeout)
        }, this.timeout)
    }
}

FAQs

Package last updated on 02 Aug 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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