{"id":18863768,"url":"https://github.com/alexzhang1030/wshe","last_synced_at":"2025-04-14T13:06:56.618Z","repository":{"id":231324758,"uuid":"781519542","full_name":"alexzhang1030/wshe","owner":"alexzhang1030","description":"A simple yet modern websocket client.","archived":false,"fork":false,"pushed_at":"2025-04-07T03:32:57.000Z","size":674,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-07T04:28:36.634Z","etag":null,"topics":["typescript","websocket","websocket-client"],"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/alexzhang1030.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":"2024-04-03T14:37:08.000Z","updated_at":"2025-04-07T03:32:50.000Z","dependencies_parsed_at":"2024-05-20T02:30:37.724Z","dependency_job_id":"34ec3e84-645d-4995-91db-84e7e79cb0c5","html_url":"https://github.com/alexzhang1030/wshe","commit_stats":{"total_commits":78,"total_committers":4,"mean_commits":19.5,"dds":0.5,"last_synced_commit":"98576cd150cb57e8aa279564c04ea42147bf4997"},"previous_names":["alexzhang1030/wshe"],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexzhang1030%2Fwshe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexzhang1030%2Fwshe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexzhang1030%2Fwshe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexzhang1030%2Fwshe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexzhang1030","download_url":"https://codeload.github.com/alexzhang1030/wshe/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248886314,"owners_count":21177643,"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":["typescript","websocket","websocket-client"],"created_at":"2024-11-08T04:38:27.118Z","updated_at":"2025-04-14T13:06:56.079Z","avatar_url":"https://github.com/alexzhang1030.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# wshe\n\n**WS He**lper\n\n\u003ca href=\"https://www.npmjs.com/package/wshe\" target=\"_blank\" rel=\"noopener noreferrer\"\u003e\u003cimg src=\"https://badgen.net/npm/v/wshe\" alt=\"NPM Version\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://www.npmjs.com/package/wshe\" target=\"_blank\" rel=\"noopener noreferrer\"\u003e\u003cimg src=\"https://badgen.net/npm/dt/wshe\" alt=\"NPM Downloads\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/alexzhang1030/wshe/blob/main/LICENSE\" target=\"_blank\" rel=\"noopener noreferrer\"\u003e\u003cimg src=\"https://badgen.net/github/license/alexzhang1030/wshe\" alt=\"License\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://codecov.io/gh/alexzhang1030/wshe\" \u003e\u003cimg src=\"https://codecov.io/gh/alexzhang1030/wshe/graph/badge.svg?token=I4FQDYAOMN\"/\u003e\u003c/a\u003e\n\nA simple yet modern websocket client.\n\n## Installation\n\n```bash\npnpm i wshe\n```\n\n## Usage\n\n```ts\nimport { createWSHE } from 'wshe'\n\nconst wshe = createWSHE('wss://echo.websocket.org', {\n  // Pass this to automatically connect on creation\n  immediate: true\n})\n\n// You can also connect manually\nwshe.connect()\n\n// You can send something\nwshe.send('\u003ceventName\u003e', '\u003ceventPayload\u003e')\n\n// or send raw data\nwshe.sendRaw('\u003crawData\u003e')\nwshe.subscribeRaw((data) =\u003e {})\n\n// You can listen to messages\nconst unsubscribe = wshe.subscribe('\u003ceventName\u003e', (payload) =\u003e {})\n```\n\n## Server implementation\n\nYou should implement a server to transfer messages between clients.\n\nAnd do this on `onmessage` to handle heartbeat requests:\n\n```ts\nimport { getHeartbeatResponse, isHeartbeatRequest } from 'wshe/server'\n\nws.onmessage = (message) =\u003e {\n  if (isHeartbeatRequest(message)) {\n    ws.send(getHeartbeatResponse(message))\n    return\n  }\n  // do your own logic here..., e.g.\n  JSON.parse(message)\n}\n```\n\n## Options\n\n```ts\ninterface WSHEConfig {\n  /**\n   * onConnected event callback\n   * @default\n   * () =\u003e {}\n   */\n  onConnected?: (ws: WebSocket, event: Event) =\u003e void\n  /**\n   * onDisconnected event callback\n   * @default\n   * () =\u003e {}\n   */\n  onDisconnected?: (ws: WebSocket, event: CloseEvent) =\u003e void\n  /**\n   * onMessage event callback\n   * @default\n   * () =\u003e {}\n   */\n  onError?: (ws: WebSocket, event: Event) =\u003e void\n\n  /**\n   * Debugging log\n   * @default false\n   */\n  debugging?: boolean\n  /**\n   * Connection ws immediately\n   * @default false\n   */\n  immediate?: boolean\n  /**\n   * Heartbeat configuration\n   * @default\n   */\n  heartbeat?: WSHEHeartbeatConfig\n\n  /**\n   * Auto reconnect\n   * @default false\n   */\n  autoReconnect?: boolean\n}\n\ninterface WSHEHeartbeatConfig {\n  /**\n   * @default 5000\n   */\n  interval?: number\n  /**\n   * @default \"ping\"\n   */\n  pingMessage?: string\n  /**\n   * @default \"pong\"\n   */\n  pongMessage?: string\n  /**\n   * @default 10000\n   */\n  timeout?: number\n}\n```\n\n## TypeScript Support\n\nThis library is written in TypeScript and provides good support for it.\n\nSet message type will be like this:\n\n```ts\nconst wshe = createWSHE\u003c{\n  foo: {\n    bar: string\n  }\n  baz: {\n    qux: number\n  }\n}\u003e('...')\n\nwshe.subscribe('foo', (payload) =\u003e {\n  //                   ^? { bar: string }\n})\n\nwshe.subscribe('baz', (payload) =\u003e {\n  //                   ^? { qux: number }\n})\n```\n\n## Credits\n\nThis project is highly inspired by [wsgo](https://github.com/melishev/wsgo).\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexzhang1030%2Fwshe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexzhang1030%2Fwshe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexzhang1030%2Fwshe/lists"}