persistent-websocket
v1.0.2
Published
An automatically-reconnecting websocket wrapper that respects server reachability and good backoff practices
Downloads
226
Readme
Persistent Websocket
An automatically-reconnecting websocket wrapper that respects server reachability and good backoff practices
Features
- Optionally ping the backend (using a custom function) to make sure you're not on a zombie connection
- Optionally check for basic internet connectivity before trying to reconnect (via a configured url endpoint)
- "Decorrelated jitter" backoff
- umd / universal library
- Configurable timeouts, ping intervals, and backoff delay limits
Usage
import {PersistentWebsocket} from "persistent-websocket";
const pws = new PersistentWebsocket("ws://mysite.com/", {
pingSendFunction: (pws) => pws.send("ping"),
reachabilityTestUrl: "/favicon.ico"
});
pws.onmessage = (e) => console.log(e);
pws.onopen = (e) => console.log(e);
pws.onclose = (e) => console.log(e);
pws.onerror = (e) => console.log(e);
pws.open(); // Must be explicitly opened, unlike regular WebSocket
// Now you can use the PersistentWebsocket instance like a plain WebSocket instance,
// except this instance will automatically try to reconnect if the connection dies
Options
The full list of available options is:
debug
boolean (defaultfalse
):
Controls logging of verbose/debug outputinitialBackoffDelayMillis
numeric (default500
):
Delay before first reconnection attempt (also acts as a minimum delay)maxBackoffDelayMillis
numeric (default120000
):
Maximum delay between reconnection attemptspingSendFunction
function (no default):
An optional function that takes the PersistentWebsocket instance as its only parameter.
When left undefined, pings will not be performed.pingIntervalSeconds
numeric (default30
):
If the remote end of the websocket connection hasn't sent anything after this number of seconds, a ping will be sent (viapingSendFunction
) to probe the connection.
Note that pings aren't sent at regular intervals. They're only sent when the line is otherwise quiet.pingTimeoutMillis
numeric (default2000
):
How long to wait for a ping response before calling the connection deadconnectTimeoutMillis
numeric (default3000
):
How long to wait for the websocket connection to reach aWebSocket.OPEN
ready statereachabilityTestUrl
string (no default):
An optional url to use to test for internet connectivity (may be absolute or relative)
AHEAD
request will be sent to this url before trying to reconnect, and if it fails, the library will reset the backoff timing to its initial value and poll that url until it successfully responds.
If left undefined, no reachability/internet connectivity check will be performed.reachabilityTestTimeoutMillis
numeric (default2000
):
How long to wait for a response from thereachabilityTestUrl
reachabilityPollingIntervalMillis
numeric (default3000
):
How long to between a failed reachability test and the next request to thereachabilityTestUrl
Events
In addition to the standard websocket events, the library also provides a "beforereconnect" event. This event is fired when a reconnection is scheduled (as opposed to attempted), and has the following properties:
attemptNumber
numeric
The number of this attempt relative to the last disconnectwaitMillis
numeric
The timeout until the next connection attempt will be made (in milliseconds)
It also adds a couple fields to existing websocket events:
wasExpected
boolean
Added to theclose
eventwasReconnect
boolean
Added to theopen
event
License
MIT