
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.
@archmaster/sockette
Advanced tools
Sockette is a tiny (348 bytes) wrapper around WebSocket
that will automatically reconnect if the connection is lost!
In addition to attaching additional API methods, Sockette allows you to reuse instances, avoiding the need to redeclare all event listeners.
You have direct access to the (current) underlying WebSocket
within every EventListener
callback (via event.target
).
This is my fork that supports the ws
WebSocket polyfill.
$ npm install --save sockette
Unlike WebSocket
, you should declare all event listeners on initialization:
const Sockette = require('sockette');
const ws = new Sockette('ws://localhost:3000', {
timeout: 5e3,
maxAttempts: 10,
onopen: e => console.log('Connected!', e),
onmessage: e => console.log('Received:', e),
onreconnect: e => console.log('Reconnecting...', e),
onmaximum: e => console.log('Stop Attempting!', e),
onclose: e => console.log('Closed!', e),
onerror: e => console.log('Error:', e)
});
ws.send('Hello, world!');
ws.json({type: 'ping'});
ws.close(); // graceful shutdown
// Reconnect 10s later
setTimeout(ws.reconnect, 10e3);
Returns: Sockette
Returns the Sockette
instance.
Type: String
The URL you want to connect to — Should be prefixed with ws://
or wss://
. This is passed directly to WebSocket
.
Type: String|Array
Either a single protocol string or an array of strings used to indicate sub-protocols. See the WebSocket
docs for more info.
Type: Number
Default: 1000
The amount of time (in ms
) to wait in between reconnection attempts. Defaults to 1 second.
Type: Number
Default: Infinity
The maximum number of attempts to reconnect.
Important: Pass
-1
if you want to disable this feature. Although, this is main reason to use Sockette! :joy:
Type: Function
The EventListener
to run in response to 'open'
events. It receives the Event
object as its only parameter.
This is called when the connection has been established and is ready to send and receive data.
Important: Sockette will forget the number of previous reconnection attempts, so that the next time connection is lost, you will consistently retry
n
number of times, as determined byoptions.maxAttempts
.
Type: Function
The EventListener
to run in response to 'message'
events. It receives the Event
object as its only parameter.
This is called when a message has been received from the server. You'll probably want
event.data
!
Type: Function
The callback to run when attempting to reconnect to the server.
If Sockette is automatically reconnecting in response to an error
or unexpected close
event, then your onreconnect
callback will receive the forwarded Event
object.
Type: Function
The callback to run when the maxAttempts
limit has been met.
This callback will receive the forwarded Event
object from onclose
.
Type: Function
The EventListener
to run in response to 'close'
events. It receives the Event
object as its only parameter.
This is called when the connection has been closed for any reason.
Important: If the
event.code
is not1000
or1005
an automatic reconnect attempt will be queued.
Type: Function
The EventListener
to run in response to 'error'
events. It receives the Event
object as its only parameter.
This is called anytime an error occurs.
Important: If the
event.code
isECONNREFUSED
, an automatic reconnect attempt will be queued.
Identical to WebSocket#send()
, capable of sending multiple data types.
Identical to WebSocket#close()
.
Note: The
code
will default to1005
unless specified.
Convenience method that passes your obj
(Object) through JSON.stringify
before passing it to WebSocket#send()
.
If options.maxAttempts
has not been exceeded, enqueues a reconnection attempt. Otherwise, it runs your options.onmaximum
callback.
Initializes a new WebSocket
— used on initialization and by reconnect()
.
MIT © Luke Edwards
FAQs
🧦 The cutest little WebSocket wrapper!
The npm package @archmaster/sockette receives a total of 1 weekly downloads. As such, @archmaster/sockette popularity was classified as not popular.
We found that @archmaster/sockette 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.
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.