1.0.2 • Published 9 years ago
persistent-websocket v1.0.2
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:
debugboolean (defaultfalse):
Controls logging of verbose/debug outputinitialBackoffDelayMillisnumeric (default500):
Delay before first reconnection attempt (also acts as a minimum delay)maxBackoffDelayMillisnumeric (default120000):
Maximum delay between reconnection attemptspingSendFunctionfunction (no default):
An optional function that takes the PersistentWebsocket instance as its only parameter.
When left undefined, pings will not be performed.pingIntervalSecondsnumeric (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.pingTimeoutMillisnumeric (default2000):
How long to wait for a ping response before calling the connection deadconnectTimeoutMillisnumeric (default3000):
How long to wait for the websocket connection to reach aWebSocket.OPENready statereachabilityTestUrlstring (no default):
An optional url to use to test for internet connectivity (may be absolute or relative)
AHEADrequest 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.reachabilityTestTimeoutMillisnumeric (default2000):
How long to wait for a response from thereachabilityTestUrlreachabilityPollingIntervalMillisnumeric (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:
attemptNumbernumeric
The number of this attempt relative to the last disconnectwaitMillisnumeric
The timeout until the next connection attempt will be made (in milliseconds)
It also adds a couple fields to existing websocket events:
wasExpectedboolean
Added to thecloseeventwasReconnectboolean
Added to theopenevent
License
MIT