{"id":16222610,"url":"https://github.com/moishinetzer/partysocket","last_synced_at":"2025-10-28T11:39:56.127Z","repository":{"id":240219119,"uuid":"802004422","full_name":"moishinetzer/partysocket","owner":"moishinetzer","description":null,"archived":false,"fork":false,"pushed_at":"2024-05-17T10:53:13.000Z","size":63,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-04T20:43:13.866Z","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/moishinetzer.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","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":"2024-05-17T10:32:12.000Z","updated_at":"2024-05-17T10:53:17.000Z","dependencies_parsed_at":"2024-05-17T11:59:28.998Z","dependency_job_id":null,"html_url":"https://github.com/moishinetzer/partysocket","commit_stats":null,"previous_names":["moishinetzer/partysocket"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moishinetzer%2Fpartysocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moishinetzer%2Fpartysocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moishinetzer%2Fpartysocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moishinetzer%2Fpartysocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/moishinetzer","download_url":"https://codeload.github.com/moishinetzer/partysocket/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247761051,"owners_count":20991533,"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-10-10T12:14:41.574Z","updated_at":"2025-10-28T11:39:51.083Z","avatar_url":"https://github.com/moishinetzer.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PartySocket\n\n_(Forked from the wonderful [reconnecting-websocket](https://github.com/joewalnes/reconnecting-websocket/) project, updated with pending PRs and bugfixes)_\n\nA better WebSocket that Just Works™\n\n## Install\n\n```bash\nnpm install partysocket\n```\n\n## Features\n\n- WebSocket API compatible (same interface, Level0 and Level2 event model)\n- Reconnects when a connection drops\n- Buffers messages when not connected, and sends accumulated messages when open\n- Handle connection timeouts\n- Allows changing server URL between reconnections\n- Fully configurable\n- Multi-platform (Web, ServiceWorkers, Node.js, React Native, Cloudflare Workers, Deno, Bun)\n- Dependency free (does not depend on Window, DOM or any EventEmitter library)\n- Debug mode\n- Works everywhere, not just with PartyKit!\n\n## Usage\n\n### Compatible with WebSocket Browser API\n\n[MDN WebSocket API](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket).\n\n### Simple usage\n\n```javascript\nimport { WebSocket } from \"partysocket\";\n\nconst ws = new WebSocket(\"wss://my.site.com\");\n\nws.addEventListener(\"open\", () =\u003e {\n  ws.send(\"hello!\");\n});\n```\n\n### Usage with PartyKit\n\n```javascript\nimport PartySocket from \"partysocket\";\n\n// optional: only needed if creating using inside node.js. Run `npm install ws`, and then add:\n// import WS from \"ws\";\n\nconst ws = new PartySocket({\n  host: \"project.name.partykit.dev\", // or localhost:1999 in dev\n  room: \"my-room\",\n  // add an optional id to identify the client,\n  // if not provided, a random id will be generated\n  id: \"some-connection-id\"\n  // optional: if used from node.js, you need to pass the WebSocket polyfill imported from `ws`\n  // WebSocket: WS\n});\n\n// optionally, update the properties of the connection\n// (e.g. to change the host or room)\nws.updateProperties({\n  host: \"another-project.username.partykit.dev\",\n  room: \"my-new-room\"\n});\n\nws.reconnect(); // make sure to call reconnect() after updating the properties\n```\n\n### Update URL\n\nThe `url` parameter will be resolved before connecting, with possible types:\n\n- `string`\n- `() =\u003e string`\n- `() =\u003e Promise\u003cstring\u003e`\n\n```javascript\nimport { WebSocket } from \"partysocket\";\n\nconst urls = [\n  \"wss://my.site.com\",\n  \"wss://your.site.com\",\n  \"wss://their.site.com\"\n];\nlet urlIndex = 0;\n\n// round robin url provider\nconst urlProvider = () =\u003e urls[urlIndex++ % urls.length];\n\nconst ws = new WebSocket(urlProvider);\n```\n\n```javascript\nimport { WebSocket } from \"partysocket\";\n\n// async url provider\nconst urlProvider = async () =\u003e {\n  const token = await getSessionToken();\n  return `wss://my.site.com/${token}`;\n};\n\nconst ws = new WebSocket(urlProvider);\n```\n\n### Update Protocols\n\nThe `protocols` parameter will be resolved before connecting, possible types:\n\n- `null`\n- `string`\n- `string[]`\n- `() =\u003e string | string[] | null`\n- `() =\u003e Promise\u003cstring | string[] | null\u003e`\n\n```javascript\nimport { WebSocket } from \"partysocket\";\n\nconst ws = new WebSocket(\"wss://your.site.com\", \"your protocol\");\n```\n\n```javascript\nimport WebSocket from 'partysocket`;\n\nconst protocols = ['p1', 'p2', ['p3.1', 'p3.2']];\nlet protocolsIndex = 0;\n\n// round robin protocols provider\nconst protocolsProvider = () =\u003e protocols[protocolsIndex++ % protocols.length];\n\nconst ws = new WebSocket('wss://your.site.com', protocolsProvider);\n```\n\n### Options\n\n#### Sample with custom options\n\n```javascript\nimport { WebSocket } from \"partysocket\";\nimport WS from \"ws\";\n\nconst options = {\n  WebSocket: WS, // custom WebSocket constructor\n  connectionTimeout: 1000,\n  maxRetries: 10\n};\nconst ws = new WebSocket(\"wss://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?: ProtocolsProvider, 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## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoishinetzer%2Fpartysocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmoishinetzer%2Fpartysocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoishinetzer%2Fpartysocket/lists"}