![Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility](https://cdn.sanity.io/images/cgdhsj6q/production/97774ea8c88cc8f4bed2766c31994ebc38116948-1664x1366.png?w=400&fit=max&auto=format)
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@october/reconnecting-websocket
Advanced tools
WebSocket that will automatically reconnect if the connection is closed.
npm install --save reconnecting-websocket
# clone
git clone https://github.com/pladaria/reconnecting-websocket
# enter
cd reconnecting-websocket
# install deps
npm install
# run tests
npm test
# review the test coverage report
npm run report
So this documentation should be valid: MDN WebSocket API.
Ping me if you find any problems. Or, even better, write a test for your case and make a pull request :)
const ReconnectingWebSocket = require('reconnecting-websocket');
const rws = new ReconnectingWebSocket('ws://my.site.com');
rws.addEventListener('open', () => {
rws.send('hello!');
});
The url
parameter also accepts a function
so you have a chance to update the URL before connecting:
const ReconnectingWebSocket = require('reconnecting-websocket');
const urls = ['ws://my.site.com', 'ws://your.site.com', 'ws://their.site.com'];
let urlIndex = 0;
// Round robin url provider
const getUrl = () => urls[urlIndex++ % urls.length];
const rws = new ReconnectingWebSocket(getUrl);
Options should be self explanatory
const defaultOptions = {
constructor: isGlobalWebSocket() ? WebSocket : null,
maxReconnectionDelay: 10000,
minReconnectionDelay: 1500,
reconnectionDelayGrowFactor: 1.3,
connectionTimeout: 4000,
maxRetries: Infinity,
debug: false,
};
const ReconnectingWebSocket = require('reconnecting-websocket');
const options = {connectionTimeout: 1000};
const rws = new ReconnectingWebSocket('ws://my.site.com', [], options);
The close
function has an additional options parameter
close(code = 1000, reason = '', {keepClosed: boolean, fastClose: boolean, delay: number})
keepClosed
option to keep the WebSocket closed or automatically reconnect (default false
).fastClose
option is true
, all close listeners are executed as soon as the close() method is called, otherwise it waits until the websocket closing protocol finishes, this can be a long time if there's no connection (default true
). Keep in mind that with this option, it may happen that the close event is fired with a ready state of CLOSING
.delay
option to set the initial delay for the next connection retry (ignored if 0
).If you set any attributes of WebSocket itself, such as binaryType
, make sure to set them again after each reconnection, i.e. on the open
event:
rws.addEventListener('open', () => {
rws.binaryType = 'arraybuffer';
rws.send('i am ready to receive some data!');
});
This way you can use this module in cli/testing/node.js or use a decorated/alternative WebSocket. The only requisite is that the given constructor must be compatible with the WebSocket API.
The example uses the html5-websocket module.
const Html5WebSocket = require('html5-websocket');
const ReconnectingWebSocket = require('reconnecting-websocket');
const options = {constructor: Html5WebSocket};
const rws = new ReconnectingWebSocket('ws://my.site.com', undefined, options);
When the max retries limit is reached, an error event with code EHOSTDOWN
is emitted.
By default, maxRetries
is set to Infinity
.
const ReconnectingWebSocket = require('reconnecting-websocket');
const rws = new ReconnectingWebSocket('ws://my.site.com', undefined, {maxRetries: 3});
rws.onerror = (err) => {
if (err.code === 'EHOSTDOWN') {
console.log('server down');
}
};
MIT
FAQs
Reconnecting WebSocket
The npm package @october/reconnecting-websocket receives a total of 12 weekly downloads. As such, @october/reconnecting-websocket popularity was classified as not popular.
We found that @october/reconnecting-websocket demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 11 open source maintainers collaborating on the project.
Did you know?
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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.