{"id":13447628,"url":"https://github.com/pladaria/reconnecting-websocket","last_synced_at":"2025-05-14T10:06:34.664Z","repository":{"id":38815039,"uuid":"62344439","full_name":"pladaria/reconnecting-websocket","owner":"pladaria","description":"Reconnecting WebSocket. For Web, React Native, cli (Node.js)","archived":false,"fork":false,"pushed_at":"2023-06-15T17:25:20.000Z","size":796,"stargazers_count":1276,"open_issues_count":66,"forks_count":204,"subscribers_count":18,"default_branch":"master","last_synced_at":"2025-05-11T06:17:12.435Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/pladaria.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-06-30T21:50:41.000Z","updated_at":"2025-05-08T21:50:52.000Z","dependencies_parsed_at":"2024-01-14T04:44:15.397Z","dependency_job_id":"d84b9f68-5d8a-4dcd-b3ac-57610abe7565","html_url":"https://github.com/pladaria/reconnecting-websocket","commit_stats":{"total_commits":215,"total_committers":23,"mean_commits":9.347826086956522,"dds":"0.40930232558139534","last_synced_commit":"05a2f7cb0e31f15dff5ff35ad53d07b1bec5e197"},"previous_names":[],"tags_count":39,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pladaria%2Freconnecting-websocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pladaria%2Freconnecting-websocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pladaria%2Freconnecting-websocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pladaria%2Freconnecting-websocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pladaria","download_url":"https://codeload.github.com/pladaria/reconnecting-websocket/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254119473,"owners_count":22017951,"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.848Z","updated_at":"2025-05-14T10:06:34.605Z","avatar_url":"https://github.com/pladaria.png","language":"TypeScript","funding_links":[],"categories":["JavaScript","TypeScript"],"sub_categories":[],"readme":"# Reconnecting WebSocket\n\n[![Build Status](https://travis-ci.org/pladaria/reconnecting-websocket.svg?branch=master\u0026v=1)](https://travis-ci.org/pladaria/reconnecting-websocket)\n[![Coverage Status](https://coveralls.io/repos/github/pladaria/reconnecting-websocket/badge.svg?branch=master\u0026v=3)](https://coveralls.io/github/pladaria/reconnecting-websocket?branch=master)\n\nWebSocket that will automatically reconnect if the connection is closed.\n\n## Features\n\n-   WebSocket API compatible (same interface, Level0 and Level2 event model)\n-   Fully configurable\n-   Multi-platform (Web, ServiceWorkers, Node.js, React Native)\n-   Dependency free (does not depend on Window, DOM or any EventEmitter library)\n-   Handle connection timeouts\n-   Allows changing server URL between reconnections\n-   Buffering. Will send accumulated messages on open\n-   Multiple builds available (see dist folder)\n-   Debug mode\n\n## Install\n\n```bash\nnpm install --save reconnecting-websocket\n```\n\n## Usage\n\n### Compatible with WebSocket Browser API\n\nSo this documentation should be valid:\n[MDN WebSocket API](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket).\n\nPing me if you find any problems. Or, even better, write a test for your case and make a pull\nrequest :)\n\n### Simple usage\n\n```javascript\nimport ReconnectingWebSocket from 'reconnecting-websocket';\n\nconst rws = new ReconnectingWebSocket('ws://my.site.com');\n\nrws.addEventListener('open', () =\u003e {\n    rws.send('hello!');\n});\n```\n\n### Update URL\n\nThe `url` parameter will be resolved before connecting, possible types:\n\n-   `string`\n-   `() =\u003e string`\n-   `() =\u003e Promise\u003cstring\u003e`\n\n```javascript\nimport ReconnectingWebSocket from 'reconnecting-websocket';\n\nconst urls = ['ws://my.site.com', 'ws://your.site.com', 'ws://their.site.com'];\nlet urlIndex = 0;\n\n// round robin url provider\nconst urlProvider = () =\u003e urls[urlIndex++ % urls.length];\n\nconst rws = new ReconnectingWebSocket(urlProvider);\n```\n\n```javascript\nimport ReconnectingWebSocket from 'reconnecting-websocket';\n\n// async url provider\nconst urlProvider = async () =\u003e {\n    const token = await getSessionToken();\n    return `wss://my.site.com/${token}`;\n};\n\nconst rws = new ReconnectingWebSocket(urlProvider);\n```\n\n### Options\n\n#### Sample with custom options\n\n```javascript\nimport ReconnectingWebSocket from 'reconnecting-websocket';\nimport WS from 'ws';\n\nconst options = {\n    WebSocket: WS, // custom WebSocket constructor\n    connectionTimeout: 1000,\n    maxRetries: 10,\n};\nconst rws = new ReconnectingWebSocket('ws://my.site.com', [], options);\n```\n\n#### Available options\n\n```typescript\ntype Options = {\n    WebSocket?: any; // WebSocket constructor, if none provided, defaults to global WebSocket\n    maxReconnectionDelay?: number; // max delay in ms between reconnections\n    minReconnectionDelay?: number; // min delay in ms between reconnections\n    reconnectionDelayGrowFactor?: number; // how fast the reconnection delay grows\n    minUptime?: number; // min time in ms to consider connection as stable\n    connectionTimeout?: number; // retry connect if not connected after this time, in ms\n    maxRetries?: number; // maximum number of retries\n    maxEnqueuedMessages?: number; // maximum number of messages to buffer until reconnection\n    startClosed?: boolean; // start websocket in CLOSED state, call `.reconnect()` to connect\n    debug?: boolean; // enables debug output\n};\n```\n\n#### Default values\n\n```javascript\nWebSocket: undefined,\nmaxReconnectionDelay: 10000,\nminReconnectionDelay: 1000 + Math.random() * 4000,\nreconnectionDelayGrowFactor: 1.3,\nminUptime: 5000,\nconnectionTimeout: 4000,\nmaxRetries: Infinity,\nmaxEnqueuedMessages: Infinity,\nstartClosed: false,\ndebug: false,\n```\n\n## API\n\n### Methods\n\n```typescript\nconstructor(url: UrlProvider, protocols?: string | string[], options?: Options)\n\nclose(code?: number, reason?: string)\nreconnect(code?: number, reason?: string)\n\nsend(data: string | ArrayBuffer | Blob | ArrayBufferView)\n\naddEventListener(type: 'open' | 'close' | 'message' | 'error', listener: EventListener)\nremoveEventListener(type:  'open' | 'close' | 'message' | 'error', listener: EventListener)\n```\n\n### Attributes\n\n[More info](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket)\n\n```typescript\nbinaryType: string;\nbufferedAmount: number;\nextensions: string;\nonclose: EventListener;\nonerror: EventListener;\nonmessage: EventListener;\nonopen: EventListener;\nprotocol: string;\nreadyState: number;\nurl: string;\nretryCount: number;\n```\n\n### Constants\n\n```text\nCONNECTING 0 The connection is not yet open.\nOPEN       1 The connection is open and ready to communicate.\nCLOSING    2 The connection is in the process of closing.\nCLOSED     3 The connection is closed or couldn't be opened.\n```\n\n## Contributing\n\n[Read here](./CONTRIBUTING.md)\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpladaria%2Freconnecting-websocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpladaria%2Freconnecting-websocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpladaria%2Freconnecting-websocket/lists"}