{"id":15048053,"url":"https://github.com/github/stable-socket","last_synced_at":"2025-04-09T08:05:13.597Z","repository":{"id":37541549,"uuid":"278751958","full_name":"github/stable-socket","owner":"github","description":"A web socket that reconnects.","archived":false,"fork":false,"pushed_at":"2024-10-14T18:50:18.000Z","size":196,"stargazers_count":125,"open_issues_count":1,"forks_count":17,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-04-02T06:16:55.199Z","etag":null,"topics":["keep","websocket"],"latest_commit_sha":null,"homepage":"","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/github.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-07-10T23:25:22.000Z","updated_at":"2025-01-09T19:58:33.000Z","dependencies_parsed_at":"2024-06-20T05:23:39.224Z","dependency_job_id":"81da347c-ef46-4721-91b9-62f3d25f9775","html_url":"https://github.com/github/stable-socket","commit_stats":{"total_commits":42,"total_committers":10,"mean_commits":4.2,"dds":0.6904761904761905,"last_synced_commit":"37641a2f9867ed017c4ed542f6c733e3f7265c46"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/github%2Fstable-socket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/github%2Fstable-socket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/github%2Fstable-socket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/github%2Fstable-socket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/github","download_url":"https://codeload.github.com/github/stable-socket/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247999859,"owners_count":21031046,"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":["keep","websocket"],"created_at":"2024-09-24T21:07:32.787Z","updated_at":"2025-04-09T08:05:13.574Z","avatar_url":"https://github.com/github.png","language":"TypeScript","readme":"# StableSocket\n\nA web socket that reconnects.\n\n## Installation\n\n```\n$ npm install @github/stable-socket\n```\n\n## Usage\n\n```ts\nimport {StableSocket} from '@github/stable-socket'\n\nconst delegate = {\n  socketDidOpen(socket: Socket) {\n    // Socket is ready to write.\n    socket.send('Hello')\n  },\n  socketDidClose(socket: Socket, code?: number, reason?: string) {\n    // Socket closed and will retry the connection.\n  },\n  socketDidFinish(socket: Socket) {\n    // Socket closed for good and will not retry.\n  },\n  socketDidReceiveMessage(socket: Socket, message: string) {\n    // Socket read data from the connection.\n  },\n  socketShouldRetry(socket: Socket, code: number): boolean {\n    // Socket reconnects unless server returns the policy violation code.\n    return code !== 1008\n  }\n}\n\nconst policy = {\n  timeout: 4000,\n  attempts: Infinity,\n  maxDelay: 60000\n}\n\nconst url = 'wss://live.example.com'\nconst socket = new StableSocket(url , delegate, policy)\nsocket.open()\n```\n\n### BufferedSocket\n\nWriting to a StableSocket while it is in the opening or closed states\ndiscards the message data. Use a BufferedSocket to buffer writes to be\nsent when it opens.\n\n```ts\nimport {BufferedSocket, StableSocket} from '@github/stable-socket'\nconst socket = new BufferedSocket(new StableSocket(url, delegate, policy))\nsocket.open()\nsocket.send('hello') // Will be sent when the socket is open.\n```\n\n### Asynchronous connections\n\nStableSocket and BufferedSocket are abstractions over a WebSocket that\nmaintain an internal state machine, managing reconnects and preventing writes\nto closed sockets. However, sometimes we need direct access to an open WebSocket\nin async functions.\n\n#### connect\n\nAsynchronously connects to a web socket port or fails after a timeout. The\nsocket is open, and writable with `send`, when its promise is fulfilled.\nReturns a Promise fulfilled with an open WebSocket or rejected with a\nconnection failure.\n\n```ts\nimport {connect} from '@github/stable-socket'\n\ntry {\n  const socket = await connect('wss://live.example.com', 100)\n  socket.send('hi')\n} catch (e) {\n  console.log('Socket connection failed', e)\n}\n```\n\n#### connectWithRetry\n\nAsynchronously connects to a web socket port, retrying failed connections\nwith exponential backoff. Returns a Promise fulfilled with an open WebSocket\nor rejected with a connection failure.\n\n```ts\nimport {connectWithRetry} from '@github/stable-socket'\n\ntry {\n  const policy = {timeout: 100, attempts: Infinity, maxDelay: 60000}\n  const socket = await connectWithRetry('wss://live.example.com', policy)\n  socket.send('hi')\n} catch (e) {\n  console.log('Socket connection failed', e)\n}\n```\n\n## Development\n\n```\nnpm install\nnpm test\n```\n\n## License\n\nDistributed under the MIT license. See LICENSE for details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgithub%2Fstable-socket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgithub%2Fstable-socket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgithub%2Fstable-socket/lists"}