Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

forever-websocket

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

forever-websocket

WebSocket client, reconnecting and isomorphic, a simple implementation

  • 0.4.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

forever-websocket

WebSocket client, reconnecting and isomorphic. A simple implementation.

Features

  • 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

Constructor

new ForeverWebSocket(address[, protocol][, options])

Parameters:

NameTypeAttributesDefaultDescription
addressstringThe URL to which to connect
protocolstring | string[]<optional>The list of subprotocols
optionsobject<optional>Options1
options.automaticOpenboolean<optional>trueControls if WebSocket should be created and connected automatically to the server. See also connect()
options.reconnectobject | null<optional>{}Parameters for reconnecting. If null, no reconnection will reoccur
options.reconnect.strategy'fibonacci' | 'exponential'<optional>'fibonacci'Backoff strategy
options.reconnect.initialDelaynumber<optional>50Initial delay in milliseconds
options.reconnect.factornumber<optional>1.5Multiplicative factor for 'exponential' backoff strategy
options.reconnect.maxDelaynumber<optional>10000Maximum delay in milliseconds
options.reconnect.randomizeDelaynumber<optional>0Range of randomness and must be between 0 and 1
options.timeoutnumber<optional>no timeoutTimeout in milliseconds after which the websockets reconnects when no messages are received
options.pingobject<optional>no pingControls how ping are sent to websocket server
options.ping.intervalnumber<optional>Ping interval value in milliseconds
options.ping.dataarray | number | object | string | ArrayBuffer | buffer<optional>The data to send in the ping frame
options.ping.pingFrameboolean<optional>falseSpecifies whether ping should be sent as a ping frame
options.ping.maskboolean<optional>Specifies whether data should be masked or not
options.newWebSocketfunction<optional>Functions which returns a WebSocket instance. If present it will be called when a new WebSocket is needed when reconnecting. The function could be useful in situations when the new WebSocket connection needs to be created with different parameters when reconnecting (e.g. a timestamp in the headers, or different URL)

Methods

All methods supported by WebSocket are supported, with unchanged behaviours and parameters. Exception are the methods below:

Method: connect()

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.


Method: send(data)

Calls WebSocket send(). Parameter data can be an object, if so it is stringify'ed before it is sent.


Method: refresh(code, reason)

Calls Websocket close(). When event close is emitted, WebSocket is re-newed if reconnect option is active.


Method: close(code, reason)

Calls Websocket close(). Reconnection is not attempted.


Method: terminate()

Calls Websocket terminate(). Reconnection is not attempted.

Some WebSocket implementations do not support terminate(), in such case close() is called instead.


Events

All events normally emitted by WebSocket are emitted, with unchanged behaviour and parameters.

In addition, the following events are emitted:

Event: connecting

  • retryNumber - The retry number
  • lastConnectedMts - Millisecond timestamp on when WebSocket was last connected

It is emitted just before WebSocket tries to reconnect again.


Event: delay

  • retryNumber - The retry number that will be attempted next
  • delay - 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.


Event: reconnected

  • retryNumber - The number of retries needed to reconnect
  • lastConnectedMts - Millisecond timestamp on when WebSocket was last connected

It is emitted when WebSocket is connected again.

WebSocket event open is still received. Event reconnected is an additional event which provides extra information.


Event: timeout

  • 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

  1. Standard WebSocket options are supported, in addition options described here are implemented

Keywords

FAQs

Package last updated on 13 Jul 2023

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc