Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
learn-reconnecting-websocket
Advanced tools
A small JavaScript library that decorates the WebSocket API to provide a WebSocket connection that will automatically reconnect if the connection is dropped.
A small JavaScript library that decorates the WebSocket API to provide a WebSocket connection that will automatically reconnect if the connection is dropped.
It is API compatible, so when you have:
var ws = new WebSocket('ws://....');
you can replace with:
var ws = new ReconnectingWebSocket('ws://....');
Minified library with gzip compression is less than 600 bytes.
With the standard WebSocket
API, the events you receive from the WebSocket instance are typically:
onopen
onmessage
onmessage
onmessage
onclose // At this point the WebSocket instance is dead.
With a ReconnectingWebSocket
, after an onclose
event is called it will automatically attempt to reconnect. In addition, a connection is attempted repeatedly (with a small pause) until it succeeds. So the events you receive may look something more like:
onopen
onmessage
onmessage
onmessage
onclose
// ReconnectingWebSocket attempts to reconnect
onopen
onmessage
onmessage
onmessage
onclose
// ReconnectingWebSocket attempts to reconnect
onopen
onmessage
onmessage
onmessage
onclose
This is all handled automatically for you by the library.
var socket = new ReconnectingWebSocket(url, protocols, options);
url
protocols
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, {debug: true, reconnectInterval: 3000});
or
var socket = new ReconnectingWebSocket(url);
socket.debug = true;
socket.timeoutInterval = 5400;
debug
console.debug()
.true
or false
false
automaticOpen
true
or false
true
reconnectInterval
integer
1000
maxReconnectInterval
integer
30000
####reconnectDecay
integer
or float
1.5
timeoutInterval
integer
2000
maxReconnectAttempts
integer
or null
.null
binaryType
'blob'
or 'arraybuffer'
.'blob'
ws.open()
ws.close(code, reason)
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.6ws.refresh()
ws.send(data)
Like this? Check out websocketd for the simplest way to create WebSocket backends from any programming language.
FAQs
A small JavaScript library that decorates the WebSocket API to provide a WebSocket connection that will automatically reconnect if the connection is dropped.
The npm package learn-reconnecting-websocket receives a total of 1 weekly downloads. As such, learn-reconnecting-websocket popularity was classified as not popular.
We found that learn-reconnecting-websocket demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.