WebSocket client, reconnecting and isomorphic, a simple implementation
npm install forever-websocket| Name | Type | Attributes | Default | Description|
|------------------------------------|---------------|--------------|-----------------|-------------|
| address | string | | | The URL to which to connect|
| protocol | string \| string[] | \
| options | object | \
| options.automaticOpen | boolean | \true | Controls if WebSocket should be created and connected automatically to the server. See also connect()|
| options.reconnect | object \| null | \{} | Parameters for reconnecting. If null, no reconnection will reoccur |
| options.reconnect.strategy | 'fibonacci' \| 'exponential' | \'fibonacci' | Backoff strategy|
| options.reconnect.initialDelay | number | \50 | Initial delay in milliseconds|
| options.reconnect.factor | number | \1.5 | Multiplicative factor for 'exponential' backoff strategy|
| options.reconnect.maxDelay | number | \10000 | Maximum delay in milliseconds|
| options.reconnect.randomizeDelay | number | \0 | Range of randomness and must be between 0 and 1|
| options.timeout | number | \
| options.ping | object | \
| options.ping.interval | number | \
| options.ping.data | array \| number \| object \| string \| ArrayBuffer \| buffer | \
| options.ping.pingFrame | boolean | \false | Specifies whether ping should be sent as a ping frame|
| options.ping.mask | boolean | \data should be masked or not|
| options.newWebSocket | function | \
[^1]: Standard WebSocket options are supported, in addition options described here are implemented
All methods supported by WebSocket are supported, with unchanged behaviours and parameters.
Exception are the methods below:
When ForeverWebsocket is created with automaticOpen = false in the constructor, underlying WebSocket objects is not created.
In this case, method connect() needs to be used to create the WebSocket and connect it to the server.
>The method has no effect when automaticOpen = true, or when it is called the second time.
send(). Parameter data can be an object, if so it is stringify'ed before it is sent. close(). When event close is emitted, WebSocket is re-newed if reconnect option is active. close(). Reconnection is not attempted.terminate(). Reconnection is not attempted.terminate(), in such case close() is called instead.All events normally emitted by WebSocket are emitted, with unchanged behaviour and parameters.
In addition, the following events are emitted:
retryNumber - The retry numberlastConnectedMts - Millisecond timestamp on when WebSocket was last connectedIt is emitted just before WebSocket tries to reconnect again.
retryNumber - The retry number that will be attempted nextdelay - Period of delay in milliseconds until the next connection attempt It is emitted when a connection attempt has failed and there needs to be a delay until the next retry.
retryNumber - The number of retries needed to reconnectlastConnectedMts - Millisecond timestamp on when WebSocket was last connectedIt is emitted when WebSocket is connected again.
> WebSocket event open is still received. Event reconnected is an additional event which provides extra information.
It is emitted when timout occurs. After the event is emitted the WebSocket connection is closed and a reconnect will be attempted if reconnection is configured.