{"id":24290544,"url":"https://github.com/dutu/forever-websocket","last_synced_at":"2025-09-25T13:31:26.688Z","repository":{"id":74504567,"uuid":"597521496","full_name":"dutu/forever-websocket","owner":"dutu","description":"WebSocket client, reconnecting and isomorphic. A simple implementation","archived":false,"fork":false,"pushed_at":"2024-07-15T13:01:01.000Z","size":29104,"stargazers_count":1,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-14T19:03:45.742Z","etag":null,"topics":["backoff","backoff-strategy","isomorphic","reconnect","reconnection","timeout","websocket","websocket-client"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/forever-websocket","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dutu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-02-04T19:59:52.000Z","updated_at":"2024-07-15T13:01:04.000Z","dependencies_parsed_at":null,"dependency_job_id":"8eade27d-a381-4b0c-8ac7-6c1a71133b6a","html_url":"https://github.com/dutu/forever-websocket","commit_stats":{"total_commits":25,"total_committers":1,"mean_commits":25.0,"dds":0.0,"last_synced_commit":"f8f24709649effb342147d50aeeda38712aea0da"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dutu%2Fforever-websocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dutu%2Fforever-websocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dutu%2Fforever-websocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dutu%2Fforever-websocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dutu","download_url":"https://codeload.github.com/dutu/forever-websocket/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234195784,"owners_count":18794356,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["backoff","backoff-strategy","isomorphic","reconnect","reconnection","timeout","websocket","websocket-client"],"created_at":"2025-01-16T11:53:14.733Z","updated_at":"2025-09-25T13:31:20.815Z","avatar_url":"https://github.com/dutu.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# forever-websocket\nWebSocket client, reconnecting and isomorphic. A simple implementation.\n\n## Features\n* WebSocket API compatible.\n  \n  It exposes all properties and methods. API documentation is still valid: [MDN WebSocket API](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) for web browser and [Node.js WebSocket](https://github.com/websockets/ws/blob/master/doc/ws.md) for node.js\n* Reconnecting, if connection drops \n* Configurable reconnecting timers \n* Configurable timeouts and reconnects when no message received\n* Configurable pings to keep connection alive\n* Allows changing URL and parameters between reconnections\n\n\n## Constructor\n\n### `new ForeverWebSocket(address[, protocol][, options])`\nParameters:\n\n| Name                               | \tType      | \tAttributes  | \tDefault        |\tDescription|\n|------------------------------------|---------------|--------------|-----------------|-------------|\n| `address`                          | string        |              |                 |  The URL to which to connect|\n| `protocol`                         | string \\| string[]      | \\\u003coptional\\\u003e |                 | The list of subprotocols|\n| `options`                          | object        | \\\u003coptional\\\u003e |                 | Options[^1]|\n| `options.automaticOpen`            | boolean       | \\\u003coptional\\\u003e | `true`         | Controls if WebSocket should be created and connected automatically to the server. See also [`connect()`](#method-connect)|\n| `options.reconnect`                | object \\| `null`        | \\\u003coptional\\\u003e | `{}`            | Parameters for reconnecting. If `null`, no reconnection will reoccur |\n| `options.reconnect.strategy`       | `'fibonacci'` \\| `'exponential'` | \\\u003coptional\\\u003e | `'fibonacci'`   | Backoff strategy|\n| `options.reconnect.initialDelay`   | number        | \\\u003coptional\\\u003e | `50`            | Initial delay in milliseconds|\n| `options.reconnect.factor`         | number        | \\\u003coptional\\\u003e | `1.5`           | Multiplicative factor for `'exponential'` backoff strategy|\n| `options.reconnect.maxDelay`       | number        | \\\u003coptional\\\u003e | `10000`         | Maximum delay in milliseconds|\n| `options.reconnect.randomizeDelay` | number        | \\\u003coptional\\\u003e | `0`             | Range of randomness and must be between `0` and `1`|\n|  `options.timeout`                 | number        | \\\u003coptional\\\u003e | no timeout      | Timeout in milliseconds after which the websockets reconnects when no messages are received|\n| `options.ping`                     | object        | \\\u003coptional\\\u003e | no ping         | Controls how ping are sent to websocket server|\n| `options.ping.interval`            | number        | \\\u003coptional\\\u003e |                 | Ping interval value in milliseconds|\n| `options.ping.data`                | array \\| number \\| object \\| string \\| ArrayBuffer \\| buffer | \\\u003coptional\\\u003e |                 | The data to send in the ping frame|\n| `options.ping.pingFrame`           | boolean       | \\\u003coptional\\\u003e | `false`         | Specifies whether ping should be sent as a ping frame|\n| `options.ping.mask`                | boolean       | \\\u003coptional\\\u003e |  | Specifies whether `data` should be masked or not|\n| `options.newWebSocket`             | function      | \\\u003coptional\\\u003e |                 | 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)|\n\n\n[^1]: Standard WebSocket options are supported, in addition options described here are implemented\n\n\n## Methods\n\nAll methods supported by WebSocket are supported, with unchanged behaviours and parameters.\nException are the methods below:\n\n\n### Method: `connect()`\nConnects the WebSocket. \n\nWhen `ForeverWebsocket` is created with `automaticOpen = false` in the constructor, underlying WebSocket objects is not created.\nIn this case, method `connect()` needs to be used to create the WebSocket and connect it to the server.\n\n\u003eThe method has no effect when `automaticOpen = true`, or when it is called the second time.    \n\n\u003cbr\u003e\n\n### Method: `send(data)`\nCalls WebSocket `send()`. Parameter `data` can be an object, if so it is `stringify`'ed before it is sent. \n\n\n\u003cbr\u003e\n\n### Method: `refresh(code, reason)`\nCalls Websocket `close()`. When event `close` is emitted, WebSocket is re-newed if `reconnect` option is active.  \n\n\n\u003cbr\u003e\n\n### Method: `close(code, reason)`\nCalls Websocket `close()`. Reconnection is not attempted.\n\n\n\u003cbr\u003e\n\n### Method: `terminate()`\nCalls Websocket `terminate()`. Reconnection is not attempted.\n\u003e Some WebSocket implementations do not support `terminate()`, in such case `close()` is called instead.\n\n\n\u003cbr\u003e\n\n## Events\n\nAll events normally emitted by WebSocket are emitted, with unchanged behaviour and parameters.\n\n**In addition**, the following events are emitted:\n\n### Event: `connecting`\n* `retryNumber` - The retry number\n* `lastConnectedMts` - Millisecond timestamp on when WebSocket was last connected\n\nIt is emitted just before WebSocket tries to reconnect again.\n\n\n\u003cbr\u003e\n\n### Event: `delay`\n* `retryNumber` - The retry number that will be attempted next\n* `delay` - Period of delay in milliseconds until the next connection attempt \n\nIt is emitted when a connection attempt has failed and there needs to be a delay until the next retry.  \n\n\n\u003cbr\u003e\n\n### Event: `reconnected`\n* `retryNumber` - The number of retries needed to reconnect\n* `lastConnectedMts` - Millisecond timestamp on when WebSocket was last connected\n\nIt is emitted when WebSocket is connected again.\n \u003e WebSocket event  `open` is still received. Event `reconnected` is an additional event which provides extra information.  \n\n\n\u003cbr\u003e\n\n### Event: `timeout`\n* lastRefreshMts - Millisecond timestamp when WebSocket connection was last refreshed, which is when connection was open or last message was received.\n\nIt 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. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdutu%2Fforever-websocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdutu%2Fforever-websocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdutu%2Fforever-websocket/lists"}