{"id":2130734,"url":"https://github.com/bcomnes/universal-reconnecting-websocket","last_synced_at":"2025-04-22T21:44:43.767Z","repository":{"id":57386603,"uuid":"158034503","full_name":"bcomnes/universal-reconnecting-websocket","owner":"bcomnes","description":"A universal, reconnecting, event based WebSocket abstraction with configurable backoff.","archived":false,"fork":false,"pushed_at":"2018-11-25T17:07:06.000Z","size":82,"stargazers_count":11,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-12T19:41:43.424Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/universal-reconnecting-websocket","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bcomnes.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-17T23:42:47.000Z","updated_at":"2023-12-20T20:39:19.000Z","dependencies_parsed_at":"2022-09-26T16:51:01.640Z","dependency_job_id":null,"html_url":"https://github.com/bcomnes/universal-reconnecting-websocket","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Funiversal-reconnecting-websocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Funiversal-reconnecting-websocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Funiversal-reconnecting-websocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Funiversal-reconnecting-websocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bcomnes","download_url":"https://codeload.github.com/bcomnes/universal-reconnecting-websocket/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250330465,"owners_count":21412985,"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":[],"created_at":"2024-01-21T23:26:01.459Z","updated_at":"2025-04-22T21:44:43.739Z","avatar_url":"https://github.com/bcomnes.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# universal-reconnecting-websocket\n[![npm version][npmimg]][npm] [![build status][travisimg]][travis] [![coverage][coverallsimg]][coveralls]\n[![downloads][downloadsimg]][downloads] [![js-standard-style][standardimg]][standard]\n\nA universal, reconnecting, event based WebSocket abstraction with configurable [backoff][backoff].\n\n## Installation\n\n```console\n$ npm install universal-reconnecting-websocket\n```\n\n## Usage\n\n```js\nconst URWS = require(\"universal-reconnecting-websocket\")\n\nconst ws = new URWS('wss://example.com')\n\nws.on('state', console.log) // handle connection state\nws.on('error', console.error) // handle / display errors\nws.on('info', console.debug) // handle non-error status events\n\nws.on('connect', () =\u003e { // Handle the connect event maybe\n  ws.send({ // default serializer is JSON\n    type: 'handshake',\n    foo: 'bar'\n  })\n})\n\nws.on('reconnect', () =\u003e { // Reconnection handlers can be the same function or a separate function\n  ws.send({\n    type: 'handshakeWithResume',\n    foo: 'bar'\n  })\n})\n\nws.on('disconnect', () =\u003e {\n  console.log('ws fully disconnected')\n})\n\nws.on('message', (ev) =\u003e {\n  // ev is the raw message event from the websocket\n  // ev.data is the raw data payload\n  console.log('Received message: ')\n  console.log(ev.body.foo.bar) // default de-serializer is JSON\n  // deserialized object is found on ev.body\n  console.log(ev.data) // raw message data\n})\n\nws.start()\n\nsetTimeout(() =\u003e {\n  ws.stop() // turn off the websocket after a while\n}, 10e10)\n```\n\n## API\n\n### `URWS = require(\"universal-reconnecting-websocket\")`\n\nImport the URWS class.\n\n### `ws = new URWS(url, [opts])`\n\nCreate a URWS instance that will connect to a `ws` or `wss` `url`.  (e.g. [`wss://example.com`](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/WebSocket)) and an `opts` object.\n\nOptional `opts` include:\n\n```js\n{\n  serializer: JSON.stringify, // Default message serializer\n  deserializer: JSON.parse, // Default deserializer\n  strategy: 'fibonacci', // default backoff strategy\n  protocols: null, // https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/WebSocket\n  binaryType: null, // https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/binaryType\n  strategyOpts: { // backoff options argument\n    randomisationFactor: 0.2, // No deep merging is performed.\n    initialDelay: 1000, // If you need to change these, you have to repass the whole object\n    maxDelay: 20000 // https://github.com/MathieuTurcotte/node-backoff\n  },\n  transport, // native WebSockets, https://github.com/websockets/ws, or pass in your own\n  nodeOpts, // opts for ws in node https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketaddress-protocols-options\n  failAfter: null, // fail after n failed connection attempts\n  name: 'urws-' + Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1) // name timing events\n}\n```\n\n### `ws.state`\n\nA getter that returns the current state of the URWS instance.  One of:\n\n- `disconnected`: is disconnected and idle.\n- `waiting`: waiting to reconnect within the backoff timing\n- `error`: websocket is disconnected and idle due to an unrecoverable error\n- `reconnecting`: websocket is attempting to reconnect after being disconnected and `waiting`.\n- `connecting`: websocket is attempting to connect for the first time.\n- `connected`: websocket is connected and open.\n\n### `ws.binaryType`\n\nA getter/setter that returns the configured `binaryType` option.  If unset, it defaults to whatever the default `binaryType` the WebSocket `transport` defaults to.  If that is unknown, it will return undefined.\n\nWhen using native WebSockets it will be or can be set to one of:\n\n- 'blob',\n- 'arraybuffer'\n\n### `ws.start()`\n\nStart the websocket connection until `ws.stop()` is called, or `opts.failAfter` consecutive reconnection attempts fail.\n\n### `ws.stop()`\n\nDisconnect any active websocket connections, cancel any outstanding reconnection attempts and remove all underlying event listeners from existing WebSockets.\n\n### `ws.send(msg)`\n\nSend a `msg` over a WebSocket.  `opts.serializer` is automatically applied to `msg` and the result is [sent](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send) to the WebSocket server.  If you try to send when not in a `connected` state, the `msg` is discarded and an `error` event is fired.  Any errors while running `opts.serializer` cause the `msg` to be discarded and an `error` event is fired.  In both cases, the `error` will contain the original message as `err.msg`.  Setting `opts.serializer` to `null` or `undefined` disables any automatic attempt to serialize your message.\n\n### Events\n\nThe `ws` instance emits the following node style events.  They should all be handled.\n\n#### `ws.on('state', (stateString) =\u003e {})`\n\nListen to the `state` event to get `state` string updates as they are generated.  If `stateString` === `error`, the URWS encountered an unrecoverable error and is not attempting to reconnect any further.\n\n#### `ws.on('error', (err) =\u003e {})`\n\nListen to `error` events to receive various errors that are encountered.  You may wish to display or log these somewhere to provide better visibility of connection state, but some of these may easily be ignored.  If the error originated from an underlying WebSocket event, the event will be available on `err.ev`.\n\n#### `ws.on('info', (ev) =\u003e {})`\n\nListen to the `info` event to get information about clean connection `close` events.  Sometimes websocket servers limit connection time, or need to restart etc.  The `ev` will be an instance of [CloseEvent](https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent).\n\n#### `ws.on('connect', (ev) =\u003e {})`\n\nFired on the first successful connection after calling `ws.start()`.  See more on the [`onopen`](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/onopen) event.\n\n#### `ws.on('reconnect', (ev) =\u003e {})`\n\nFired on the second and any subsequent successful connection attempts.  Useful for handling resume logic or other mid-session handshake actions. See more on the [`onopen`](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/onopen) event.\n\n#### `ws.on('disconnect' () =\u003e {})`\n\nFired after all event listeners have been removed and the websocket has been successfully closed.\n\n#### `ws.on('message', (data) =\u003e {})`\n\nFired whenever the client receives a message from the WebSocket server.  Data will be the contents of the received message.\n\nIf the `opts.deserializer` option is set, the `ev.data` field will be deserialized into `ev.body`.  Any errors during that process will prevent the `message` event from firing, and an `error` event will be fired instead, with the original `ev` attached as `err.\n\nWith native websockets, this event receives an `ev` will be of type [MessageEvent](https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent), but only the data of the message is passed to the event listener.  See more on the [onmessage](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/onmessage) event.  If needed we can expose this, but for now its abstracted away in order to pairty `ws` in the browser.\n\nSetting the `opts.deserializer` option to `null` or `undefined` prevents any automatic attempts at deserializing and the `ev.body` property will be `undefined`.\n\n## License\n[MIT](https://tldrlegal.com/license/mit-license)\n\n[stabilityimg]: https://img.shields.io/badge/stability-experimental-orange.svg\n[stability]: https://nodejs.org/api/documentation.html#documentation_stability_index\n[npmimg]: https://img.shields.io/npm/v/universal-reconnecting-websocket.svg\n[npm]: https://npmjs.org/package/universal-reconnecting-websocket\n[travisimg]: https://img.shields.io/travis/bcomnes/universal-reconnecting-websocket/master.svg\n[travis]: https://travis-ci.org/bcomnes/universal-reconnecting-websocket\n[downloadsimg]: https://img.shields.io/npm/dm/universal-reconnecting-websocket.svg\n[downloads]: https://npmjs.org/package/universal-reconnecting-websocket\n[standardimg]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg\n[standard]: https://github.com/feross/standard\n[coverallsimg]: https://img.shields.io/coveralls/bcomnes/universal-reconnecting-websocket/master.svg\n[coveralls]: https://coveralls.io/github/bcomnes/universal-reconnecting-websocket\n\n[backoff]: https://github.com/MathieuTurcotte/node-backoff#readme\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbcomnes%2Funiversal-reconnecting-websocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbcomnes%2Funiversal-reconnecting-websocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbcomnes%2Funiversal-reconnecting-websocket/lists"}