{"id":13447627,"url":"https://github.com/joewalnes/reconnecting-websocket","last_synced_at":"2025-05-14T00:06:01.456Z","repository":{"id":2325098,"uuid":"3285927","full_name":"joewalnes/reconnecting-websocket","owner":"joewalnes","description":"A small decorator for the JavaScript WebSocket API that automatically reconnects","archived":false,"fork":false,"pushed_at":"2022-07-26T18:35:34.000Z","size":94,"stargazers_count":4271,"open_issues_count":79,"forks_count":968,"subscribers_count":128,"default_branch":"master","last_synced_at":"2025-04-10T02:16:07.914Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/joewalnes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-01-27T20:28:51.000Z","updated_at":"2025-04-08T18:48:37.000Z","dependencies_parsed_at":"2022-06-27T01:31:31.057Z","dependency_job_id":null,"html_url":"https://github.com/joewalnes/reconnecting-websocket","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joewalnes%2Freconnecting-websocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joewalnes%2Freconnecting-websocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joewalnes%2Freconnecting-websocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joewalnes%2Freconnecting-websocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joewalnes","download_url":"https://codeload.github.com/joewalnes/reconnecting-websocket/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248142906,"owners_count":21054671,"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-07-31T05:01:22.826Z","updated_at":"2025-04-10T02:16:13.998Z","avatar_url":"https://github.com/joewalnes.png","language":"JavaScript","readme":"ReconnectingWebSocket\n=====================\n\nA small JavaScript library that decorates the WebSocket API to provide a WebSocket connection that will automatically reconnect if the connection is dropped.\n\nIt is API compatible, so when you have:\n\n```javascript\nvar ws = new WebSocket('ws://....');\n```\n\nyou can replace with:\n\n```javascript\nvar ws = new ReconnectingWebSocket('ws://....');\n```\n\nMinified library with gzip compression is less than 600 bytes.\n\nHow reconnections occur\n-----------------------\n\nWith the standard `WebSocket` API, the events you receive from the WebSocket instance are typically:\n\n    onopen\n    onmessage\n    onmessage\n    onmessage\n    onclose // At this point the WebSocket instance is dead.\n\nWith a `ReconnectingWebSocket`, after an `onclose` event is called it will automatically attempt to reconnect. In addition, a connection is attempted repeatedly (with a small pause) until it succeeds. So the events you receive may look something more like:\n\n    onopen\n    onmessage\n    onmessage\n    onmessage\n    onclose\n    // ReconnectingWebSocket attempts to reconnect\n    onopen\n    onmessage\n    onmessage\n    onmessage\n    onclose\n    // ReconnectingWebSocket attempts to reconnect\n    onopen\n    onmessage\n    onmessage\n    onmessage\n    onclose\n\nThis is all handled automatically for you by the library.\n\n## Parameters\n\n```javascript\nvar socket = new ReconnectingWebSocket(url, protocols, options);\n```\n\n#### `url`\n- The URL you are connecting to.\n- https://html.spec.whatwg.org/multipage/comms.html#network\n\n#### `protocols`\n- Optional string or array of protocols per the WebSocket spec.\n- https://tools.ietf.org/html/rfc6455\n\n#### `options`\n- Options (see below)\n\n## Options\n\nOptions can either be passed as the 3rd parameter upon instantiation or set directly on the object after instantiation:\n\n```javascript\nvar socket = new ReconnectingWebSocket(url, null, {debug: true, reconnectInterval: 3000});\n```\n\nor\n\n```javascript\nvar socket = new ReconnectingWebSocket(url);\nsocket.debug = true;\nsocket.timeoutInterval = 5400;\n```\n\n#### `debug`\n- Whether this instance should log debug messages or not. Debug messages are printed to `console.debug()`.\n- Accepts `true` or `false`\n- Default value: `false`\n\n#### `automaticOpen`\n- Whether or not the websocket should attempt to connect immediately upon instantiation. The socket can be manually opened or closed at any time using ws.open() and ws.close().\n- Accepts `true` or `false`\n- Default value: `true`\n\n#### `reconnectInterval`\n- The number of milliseconds to delay before attempting to reconnect.\n- Accepts `integer`\n- Default: `1000`\n\n#### `maxReconnectInterval`\n- The maximum number of milliseconds to delay a reconnection attempt.\n- Accepts `integer`\n- Default: `30000`\n\n####`reconnectDecay`\n- The rate of increase of the reconnect delay. Allows reconnect attempts to back off when problems persist.\n- Accepts `integer` or `float`\n- Default: `1.5`\n\n#### `timeoutInterval`\n- The maximum time in milliseconds to wait for a connection to succeed before closing and retrying.\n- Accepts `integer`\n- Default: `2000`\n\n#### `maxReconnectAttempts`\n- The maximum number of reconnection attempts that will be made before giving up. If null, reconnection attempts will be continue to be made forever.\n- Accepts `integer` or `null`.\n- Default: `null`\n\n#### `binaryType`\n- The binary type is required by some applications.\n- Accepts strings `'blob'` or `'arraybuffer'`.\n- Default: `'blob'`\n\n---\n\n## Methods\n\n#### `ws.open()`\n- Open the Reconnecting Websocket\n\n#### `ws.close(code, reason)`\n- Closes the WebSocket connection or connection attempt, if any. If the connection is already CLOSED, this method does nothing.\n- `code` is optional the closing code (default value 1000). [https://tools.ietf.org/html/rfc6455#section-7.4.1](https://tools.ietf.org/html/rfc6455#section-7.4.1)\n- `reason` is the optional reason that the socket is being closed. [https://tools.ietf.org/html/rfc6455#section-7.1.6](https://tools.ietf.org/html/rfc6455#section-7.1.6)\n\n#### `ws.refresh()`\n- Refresh the connection if still open (close and then re-open it).\n\n#### `ws.send(data)`\n- Transmits data to the server over the WebSocket connection.\n- Accepts @param data a text string, ArrayBuffer or Blob\n\nLike this? Check out [websocketd](https://github.com/joewalnes/websocketd) for the simplest way to create WebSocket backends from any programming language.\n\n[Follow @joewalnes](https://twitter.com/joewalnes)\n\n[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/joewalnes/reconnecting-websocket/trend.png)](https://bitdeli.com/free \"Bitdeli Badge\")\n","funding_links":[],"categories":["JavaScript","others"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoewalnes%2Freconnecting-websocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoewalnes%2Freconnecting-websocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoewalnes%2Freconnecting-websocket/lists"}