Installations
npm install robust-websocket
Releases
Unable to fetch releases
Developer
nathanboktae
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
8.11.3
NPM Version
5.6.0
Statistics
155 Stars
59 Commits
24 Forks
3 Watching
4 Branches
9 Contributors
Updated on 18 Nov 2024
Languages
JavaScript (92.06%)
HTML (7.94%)
Total Downloads
Cumulative downloads
Total Downloads
599,583
Last day
6.8%
773
Compared to previous day
Last week
10.1%
3,887
Compared to previous week
Last month
15.8%
14,506
Compared to previous month
Last year
-0.9%
173,100
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
robust-websocket
A robust, reconnecting WebSocket client for the browser
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).
Compared to reconnecting-websocket
- Tests! You know it works like stated and regressions will be caught.
- Is aware of online and offline, and won't burn up the users battery and CPU reconnected when offline, and will reconnect when it is online again.
- Natively aware of error codes
- Any kind of reconnect strategy is possible via functional composition
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
Examples
Reconnect 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
.
Polyfills needed
You may need these polyfills to support older browsers
- Object.assign - npm package or 24-line MDN snippet
- CustomEvent - npm package or 15-line MDN snippet
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: ISC License: LICENSE:0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 2/28 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 4 are checked with a SAST tool
Score
3
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to robust-websocket
react-native-use-websocket
React Native Hook designed to provide robust WebSocket integrations to your Components.
@jknott/typescript-robust-websocket
A robust reconnecting WebSocket client for the browser with ts compatability based on robust-websocket
ng-rx-websocket
A robust websocket streaming service using RxJS
simple-websocket
Simple, EventEmitter API for WebSockets (browser)