Gathering detailed insights and metrics for @jknott/typescript-robust-websocket
Gathering detailed insights and metrics for @jknott/typescript-robust-websocket
Gathering detailed insights and metrics for @jknott/typescript-robust-websocket
Gathering detailed insights and metrics for @jknott/typescript-robust-websocket
A robust reconnecting WebSocket client for the browser
npm install @jknott/typescript-robust-websocket
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
69 Commits
1 Forks
1 Watching
4 Branches
1 Contributors
Updated on 02 Mar 2022
JavaScript (92.06%)
HTML (7.94%)
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
-25%
15
Compared to previous week
Last month
244.6%
193
Compared to previous month
Last year
-81.8%
1,215
Compared to previous year
typescript-robust-websocket
is a wrapper around the standard WebSocket class that implements the same interface, but can reconnect when disconnected or the user's computer comes back online.
It is error-code aware and will not reconnect on 1008 (HTTP 400 equivalent) and 1011 (HTTP 500 equivalent) by default. This behavior is fully configurable via the shouldConnect
(see Usage).
Use it as you would a normal websocket:
1var ws = new RobustWebSocket('ws://echo.websocket.org/', [], {}) 2 3ws.addEventListener('open', function(event) { 4 ws.send('Hello!') 5}) 6 7ws.addEventListener('message', function(event) { 8 console.log('we got: ' + event.data) 9})
But with an optional set of options you can specify as a 3rd parameter
1var ws = new RobustWebSocket('ws://echo.websocket.org/', [], { 2 // The number of milliseconds to wait before a connection is considered to have timed out. Defaults to 4 seconds. 3 timeout: 4000, 4 // A function that given a CloseEvent or an online event (https://developer.mozilla.org/en-US/docs/Online_and_offline_events) and the `RobustWebSocket`, 5 // will return the number of milliseconds to wait to reconnect, or a non-Number to not reconnect. 6 // see below for more examples; below is the default functionality. 7 shouldReconnect: function(event, ws) { 8 if (event.code === 1008 || event.code === 1011) return 9 return [0, 3000, 10000][ws.attempts] 10 }, 11 // A boolean indicating whether or not to open the connection automatically. Defaults to true, matching native [WebSocket] behavior. 12 // You can open the websocket by calling `open()` when you are ready. You can close and re-open the RobustWebSocket instance as much as you wish. 13 automaticOpen: true, 14 // A boolean indicating whether to disable subscribing to the connectivity events provided by the browser. 15 // By default RobustWebSocket instances use connectivity events to avoid triggering reconnection when the browser is offline. This flag is provided in the unlikely event of cases where this may not be desired. 16 ignoreConnectivityEvents: false 17})
The URL parameter can either be a string, or a function which returns a string. This can be useful if you need the WebSocket to reconnect to a different URL than it connected to initially:
1var ws = new RobustWebSocket((ws) => { 2 return ws.reconnects > 0 ? `ws://echo.websocket.org/?reconnect=${ws.reconnects}` : `ws://echo.websocket.org/` 3});
shouldReconnect
ExamplesReconnect with an exponetial backoff on all errors
1function shouldReconnect(event, ws) { 2 return Math.pow(1.5, ws.attempts) * 500 3}
Reconnect immediately but only 20 times per RobustWebSocket instance
1function shouldReconnect(event, ws) { 2 return ws.reconnects <= 20 && 0 3}
Reconnect only on some whitelisted codes, and only 3 attempts, except on online events, then connect immediately
1function shouldReconnect(event, ws) { 2 if (event.type === 'online') return 0 3 return [1006,1011,1012].indexOf(event.code) && [1000,5000,10000][ws.attempts] 4}
See documentation for CloseEvent and online event, the two types of events that shouldReconnect
will receive.
Typically, websockets closed with code 1000
indicate that the socket
closed normally. In these cases, robust-websocket
won't call
shouldReconnect
(and will not attempt to reconnect), unless you set
shouldReconnect.handle1000
to true
.
You may need these polyfills to support older browsers
No vulnerabilities found.
No security vulnerabilities found.