{"id":19670364,"url":"https://github.com/mat-sz/typesocket","last_synced_at":"2025-07-19T06:37:09.736Z","repository":{"id":57383997,"uuid":"233905600","full_name":"mat-sz/typesocket","owner":"mat-sz","description":"🌐 TypeScript WebSockets library.","archived":false,"fork":false,"pushed_at":"2023-11-15T18:44:33.000Z","size":181,"stargazers_count":26,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-06-28T09:17:26.230Z","etag":null,"topics":["javascript","typescript","typescript-library","websocket","websocket-library","websockets"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/typesocket","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause-clear","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mat-sz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-01-14T18:13:01.000Z","updated_at":"2024-10-24T07:31:41.000Z","dependencies_parsed_at":"2023-11-15T19:48:21.805Z","dependency_job_id":null,"html_url":"https://github.com/mat-sz/typesocket","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/mat-sz/typesocket","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mat-sz%2Ftypesocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mat-sz%2Ftypesocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mat-sz%2Ftypesocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mat-sz%2Ftypesocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mat-sz","download_url":"https://codeload.github.com/mat-sz/typesocket/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mat-sz%2Ftypesocket/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265898327,"owners_count":23845774,"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":["javascript","typescript","typescript-library","websocket","websocket-library","websockets"],"created_at":"2024-11-11T17:06:02.778Z","updated_at":"2025-07-19T06:37:09.713Z","avatar_url":"https://github.com/mat-sz.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003eTypeSocket\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\nTypeScript WebSocket wrapper with disconnection handling and JSON parsing.\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\u003cimg alt=\"workflow\" src=\"https://img.shields.io/github/actions/workflow/status/mat-sz/typesocket/node.js.yml?branch=master\"\u003e\n\u003ca href=\"https://npmjs.com/package/typesocket\"\u003e\n\u003cimg alt=\"npm\" src=\"https://img.shields.io/npm/v/typesocket\"\u003e\n\u003cimg alt=\"npm\" src=\"https://img.shields.io/npm/dw/typesocket\"\u003e\n\u003cimg alt=\"NPM\" src=\"https://img.shields.io/npm/l/typesocket\"\u003e\n\u003c/a\u003e\n\u003c/p\u003e\n\n\u003e **Are you a React.js user?** You might be interested in the [useTypeSocket](https://github.com/mat-sz/use-typesocket) React hook.\n\nUsed in [filedrop-web](https://github.com/mat-sz/filedrop-web) and [whiteboard-web](https://github.com/mat-sz/whiteboard-web).\n\nAPI is **mostly** stable. It may change in the future, but the changes shouldn't be breaking.\n\n## Installation\n\nTypeSocket is available on [npm](https://www.npmjs.com/package/typesocket), you can install it with either npm or yarn:\n\n```sh\nnpm install typesocket\n# or:\nyarn install typesocket\n```\n\n## Example usage\n\n```ts\nconst socket = new TypeSocket\u003cMessageModel\u003e(url, {\n  maxRetries: 10,\n});\n\nsocket.on('connected', () =\u003e {\n  console.log('connected!');\n\n  socket.send({\n    type: 'ping',\n  });\n});\n\nsocket.on('message', (message: MessageModel) =\u003e {\n  console.log(message.type);\n});\n\nsocket.on('disconnected', () =\u003e {\n  console.log('disconnected');\n});\n\nsocket.connect();\n```\n\n### With Redux:\n\nI use TypeSocket with Redux and Redux-Saga like this:\n\n```ts\nexport const socketMiddleware = (url: string) =\u003e {\n  return (store: MiddlewareAPI\u003cany, any\u003e) =\u003e {\n    const socket = new TypeSocket\u003cMessageModel\u003e(url);\n\n    socket.on('connected', () =\u003e\n      store.dispatch({ type: ActionType.WS_CONNECTED }),\n    );\n    socket.on('disconnected', () =\u003e\n      store.dispatch({ type: ActionType.WS_DISCONNECTED }),\n    );\n    socket.on('message', message =\u003e\n      store.dispatch({ type: ActionType.WS_MESSAGE, value: message }),\n    );\n    socket.connect();\n\n    return (next: (action: any) =\u003e void) =\u003e (action: any) =\u003e {\n      if (\n        action.type \u0026\u0026\n        action.type === ActionType.WS_SEND_MESSAGE \u0026\u0026\n        socket.readyState === 1\n      ) {\n        socket.send(action.value);\n      }\n\n      return next(action);\n    };\n  };\n};\n```\n\n## Events\n\nYou can attach event listeners to an instance of `TypeSocket` with `.on`:\n\n```ts\nsocket.on('message', message =\u003e {\n  console.log(message);\n});\n```\n\n### connected\n\nEmitted when a connection gets established.\n\n### disconnected\n\nEmitted when the socket is disconnected.\n\n### permanentlyDisconnected\n\nEmitted when the socket is permanently disconnected, for example:\n\n- Server gracefully closes the connection.\n- Client gracefully closes the connection.\n- `disconnect` is called.\n- Retry amount has been exceeded.\n\n### message\n\nEmitted when a **valid** message is received.\n\nThe only argument contains an object of type `T` with a deserialized message.\n\n### invalidMessage\n\nEmitted when an **invalid** message is received.\n\nThe only argument contains an object of type `string | ArrayBuffer` with a raw message.\n\n### rawMessage\n\nEmitted when **any** message is received.\n\nThe only argument contains an object of type `string | ArrayBuffer` with a raw message.\n\n### binaryMessage\n\nEmitted when a binary message (with an ArrayBuffer) is received.\n\nThe only argument contains an object of type `ArrayBuffer`.\n\n## API\n\n```\non(eventType: 'message', listener: (message: T) =\u003e void);\non(eventType: 'rawMessage' | 'invalidMessage', listener: (message: string | ArrayBuffer) =\u003e void);\non(eventType: 'binaryMessage', listener: (message: ArrayBuffer) =\u003e void);\non(eventType: 'connected' | 'disconnected' | 'permanentlyDisconnected', listener: () =\u003e void);\n\noff(eventType: 'message', listener: (message: T) =\u003e void);\noff(eventType: 'rawMessage' | 'invalidMessage', listener: (message: string | ArrayBuffer) =\u003e void);\non(eventType: 'binaryMessage', listener: (message: ArrayBuffer) =\u003e void);\noff(eventType: 'connected' | 'disconnected' | 'permanentlyDisconnected', listener: () =\u003e void);\n\nconstructor(private url: string, options?: TypeSocketOptions);\nconnect();\ndisconnect();\nsend(data: T);\nsendRaw(data: string | ArrayBuffer | Blob | ArrayBufferView);\nget readyState();\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmat-sz%2Ftypesocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmat-sz%2Ftypesocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmat-sz%2Ftypesocket/lists"}