Reconnectable websocket
npm install reconnectable-websocket
import ReconnectableWebSocket from 'reconnectable-websocket'
let socket = new ReconnectableWebSocket(url, protocols, options)
- The URL you are connecting to.
- http://dev.w3.org/html5/websockets/#the-websocket-interface
- Optional string or array of protocols per the WebSocket spec.
- http://dev.w3.org/html5/websockets/#refsWSP
- valid values are
undefined
or[...protocols]
- Options (see below)
Options can either be passed as the 3rd parameter upon instantiation or set directly on the object after instantiation:
let socket = new ReconnectableWebSocket(url, undefined, {reconnectInterval: 3000});
- Whether this instance should log debug messages or not. Debug messages are printed to
console.debug()
. - Accepts
true
orfalse
- Default value:
false
- Whether or not the websocket should attempt to connect immediately upon instantiation. The socket can be manually opened or closed at any time using ws.open() and ws.close().
- Accepts
true
orfalse
- Default value:
true
- Whether or not the websocket tries to reconnect on error
- Accepts
true
orfalse
- Default value:
false
- The number of milliseconds to delay before attempting to reconnect.
- Accepts
integer
- Default:
1000
- 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
orfloat
- Default:
1.5
- The maximum time in milliseconds to wait for a connection to succeed before closing and retrying.
- Accepts
integer
- Default:
2000
- 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
ornull
. - Default:
null
- Actual timeout is calculation like
randomBetween(timeout / randomRatio, timeout)
. If null, just uses non-random timeout. - Accepts
integer
. - Default:
3
- The binary type is required by some applications.
- Accepts strings
'blob'
or'arraybuffer'
. - Default:
'blob'
- Should the connection attempt to reconnect on a clean close. This is used in the case of a server initiated close event.
- Accepts
true
orfalse
- Default:
false
- Open the Reconnecting Websocket
- 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.1reason
is the optional reason that the socket is being closed. https://tools.ietf.org/html/rfc6455#section-7.1.6
- Transmits data to the server over the WebSocket connection.
- Accepts @param data a text string, ArrayBuffer or Blob
MIT