WebSocket client, reconnecting and isomorphic. A simple implementation.
-
WebSocket API compatible.
It exposes all properties and methods. API documentation is still valid: MDN WebSocket API for web browser and Node.js WebSocket for node.js
-
Reconnecting, if connection drops
-
Configurable reconnecting timers
-
Configurable timeouts and reconnects when no message received
-
Configurable pings to keep connection alive
-
Allows changing URL and parameters between reconnections
Parameters:
All methods supported by WebSocket are supported, with unchanged behaviours and parameters. Exception are the methods below:
Connects the WebSocket.
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.
Calls WebSocket send(). Parameter data can be an object, if so it is stringify'ed before it is sent.
Calls Websocket close(). When event close is emitted, WebSocket is re-newed if reconnect option is active.
Calls Websocket close(). Reconnection is not attempted.
Calls Websocket terminate(). Reconnection is not attempted.
Some WebSocket implementations do not support
terminate(), in such caseclose()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 connected
It 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 connected
It is emitted when WebSocket is connected again.
WebSocket event
openis still received. Eventreconnectedis an additional event which provides extra information.
- lastRefreshMts - Millisecond timestamp when WebSocket connection was last refreshed, which is when connection was open or last message was received.
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.
Footnotes
-
Standard WebSocket options are supported, in addition options described here are implemented ↩
