{"id":26077579,"url":"https://github.com/trytriplex/websocks","last_synced_at":"2025-04-11T22:51:19.835Z","repository":{"id":277253060,"uuid":"925511838","full_name":"trytriplex/websocks","owner":"trytriplex","description":"🧦 An end-to-end typed websocket server for Node.js with clients for React and events.","archived":false,"fork":false,"pushed_at":"2025-04-01T03:18:34.000Z","size":300,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-01T04:24:55.453Z","etag":null,"topics":["javascript","nodejs","reactjs","typescript","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/trytriplex.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","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},"funding":{"github":["itsdouges"]}},"created_at":"2025-02-01T03:27:08.000Z","updated_at":"2025-04-01T03:18:37.000Z","dependencies_parsed_at":"2025-03-03T06:19:44.621Z","dependency_job_id":"5a6e2dab-511d-4841-a70f-0007ce5af05c","html_url":"https://github.com/trytriplex/websocks","commit_stats":null,"previous_names":["trytriplex/websocks"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trytriplex%2Fwebsocks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trytriplex%2Fwebsocks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trytriplex%2Fwebsocks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trytriplex%2Fwebsocks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trytriplex","download_url":"https://codeload.github.com/trytriplex/websocks/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248492986,"owners_count":21113161,"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","nodejs","reactjs","typescript","websocket"],"created_at":"2025-03-09T03:05:07.155Z","updated_at":"2025-04-11T22:51:19.818Z","avatar_url":"https://github.com/trytriplex.png","language":"TypeScript","funding_links":["https://github.com/sponsors/itsdouges"],"categories":[],"sub_categories":[],"readme":"# Websocks\n\n[![Discord](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fdiscord.com%2Fapi%2Finvites%2FnBzRBUEs4b%3Fwith_counts%3Dtrue\u0026query=%24.approximate_member_count\u0026style=flat\u0026colorA=000000\u0026colorB=000000\u0026label=discord\u0026logo=\u0026logoColor=000000)](https://discord.gg/nBzRBUEs4b) [![GitHub Sponsors](https://img.shields.io/github/sponsors/itsdouges?style=flat\u0026colorA=000000\u0026colorB=000000\u0026label=sponsor\u0026logo=\u0026logoColor=000000)](https://github.com/sponsors/itsdouges)\n\n## Installation\n\n```bash\nnpm i @triplex/websocks-server @triplex/websocks-client\n```\n\n## Usage\n\n### Create the server\n\n```ts\n// 1. Create the websocks server\nimport { createWSServer } from \"@triplex/websocks-server\";\n\nconst wss = createWSServer();\n\n// 2. Define routes\nconst routes = wss.collectTypes([\n  wss.route(\n    \"/rng/:max\",\n    ({ max }) =\u003e Math.round(Math.random() * Number(max)),\n    (push) =\u003e {\n      setInterval(() =\u003e {\n        // Every 1s push a new value to the client.\n        push();\n      }, 1000);\n    },\n  ),\n]);\n\n// 3. Define events\nconst events = wss.collectTypes([\n  tws.event\u003c\"ping\", { timestamp: number }\u003e(\"ping\", (send) =\u003e {\n    setInterval(() =\u003e {\n      send({ timestamp: Date.now() });\n    }, 1000);\n  }),\n]);\n\n// 4. Start listening\nwss.listen(3000);\n\n// 5. Export types to use with the client\nexport type Routes = typeof routes;\nexport type Events = typeof events;\n```\n\n### Create the client\n\n```tsx\n// 1. Import the server types\nimport { createWSEvents } from \"@triplex/websocks-client/events\";\nimport { createWSHooks } from \"@triplex/websocks-client/react\";\nimport { type Events, type Routes } from \"./server\";\n\n// 2. Declare the clients\nconst { preloadSubscription, useSubscription } = createWSHooks\u003cRoutes\u003e({\n  url: \"ws://localhost:3000\",\n});\n\nconst { on } = createWSEvents\u003cEvents\u003e({\n  url: \"ws://localhost:3000\",\n});\n\n// 3. Preload data\npreloadSubscription(\"/rng/:max\", { max: 100 });\n\n// 4. Subscribe to the data\nfunction Component() {\n  const value = useSubscription(\"/rng/:max\", { max: 100 });\n  return \u003cdiv\u003e{value}\u003c/div\u003e;\n}\n\non(\"ping\", ({ timestamp }) =\u003e {\n  console.log(timestamp);\n});\n```\n\n## API\n\n### `createWSServer()`\n\nCreates a typed websocket server.\n\n#### Returns\n\n| Name | Description |\n| --- | --- |\n| `close()` | Closes the server. |\n| `collectTypes(TEvents[] \\| TRoutes[])` | Collects types from `event()` and `route()`. |\n| `route(path: string, callback: Function, initialize: Function)` | Creates a route. |\n| `event(eventName: string, initialize: Function)` | Creates an event. |\n| `listen(port: number)` | Listens to the declared port. |\n\n### `createWSHooks(options: Options | () =\u003e Options)`\n\nCreates a routes client using types from the server that returns React hooks.\n\n#### Returns\n\n| Name | Description |\n| --- | --- |\n| `clearQuery(path: string, args: TArgs)` | Clears the query from the cache. |\n| `preloadSubscription(path: string, args: TArgs)` | Preloads the subscription. |\n| `useSubscription(path: string, args: TArgs)` | Returns the value of a preloaded subscription. |\n| `useLazySubscription(path: string, args: TArgs)` | Returns the value of a subscription. |\n\n### `createWSEvents(options: Options | () =\u003e Options)`\n\nCreates an events client using types from the server.\n\n#### Returns\n\n| Name                                        | Description         |\n| ------------------------------------------- | ------------------- |\n| `on(eventName: string, callback: Function)` | Listen to an event. |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrytriplex%2Fwebsocks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrytriplex%2Fwebsocks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrytriplex%2Fwebsocks/lists"}