{"id":14156245,"url":"https://github.com/romanzy313/trpc-uwebsockets","last_synced_at":"2025-08-06T02:32:27.105Z","repository":{"id":48129984,"uuid":"516392454","full_name":"romanzy313/trpc-uwebsockets","owner":"romanzy313","description":"tRPC adapter for uWebSockets.js server","archived":false,"fork":false,"pushed_at":"2024-09-07T08:10:18.000Z","size":335,"stargazers_count":58,"open_issues_count":0,"forks_count":5,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-11-30T08:39:58.187Z","etag":null,"topics":["trpc","uwebsocketsjs"],"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/romanzy313.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":"2022-07-21T13:55:20.000Z","updated_at":"2024-11-23T21:59:20.000Z","dependencies_parsed_at":"2024-01-25T18:47:46.728Z","dependency_job_id":"8ddc4c53-ad8e-4b53-bd1a-aeb2ebacea31","html_url":"https://github.com/romanzy313/trpc-uwebsockets","commit_stats":{"total_commits":59,"total_committers":7,"mean_commits":8.428571428571429,"dds":0.6271186440677966,"last_synced_commit":"9a3d49a75ad5a74ed5a05a72ff8714b045e4e46f"},"previous_names":["romanzy-1612/trpc-uwebsockets"],"tags_count":0,"template":false,"template_full_name":"romanzy313/modern-ts-lib-starter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/romanzy313%2Ftrpc-uwebsockets","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/romanzy313%2Ftrpc-uwebsockets/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/romanzy313%2Ftrpc-uwebsockets/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/romanzy313%2Ftrpc-uwebsockets/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/romanzy313","download_url":"https://codeload.github.com/romanzy313/trpc-uwebsockets/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228829102,"owners_count":17978152,"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":["trpc","uwebsocketsjs"],"created_at":"2024-08-17T08:05:19.247Z","updated_at":"2024-12-09T03:31:16.978Z","avatar_url":"https://github.com/romanzy313.png","language":"TypeScript","funding_links":[],"categories":["others"],"sub_categories":[],"readme":"# trpc-uwebsockets\n\n[uWebSockets.js](https://github.com/uNetworking/uWebSockets.js) adapter for [tRPC](https://trpc.io/)\n\n# Installation\n\nVersion 10\n\n```bash\nnpm install trpc-uwebsockets\n```\n\nVersion 11 beta (same as next branch of trpc)\n\n```bash\nnpm install trpc-uwebsockets@beta\n```\n\n# Usage\n\nImport needed packages\n\n```typescript\nimport { App } from 'uWebSockets.js';\nimport { inferAsyncReturnType, initTRPC } from '@trpc/server';\nimport { CreateContextOptions } from 'trpc-uwebsockets';\nimport z from 'zod';\n```\n\nDefine tRPC, context, and router\n\n```typescript\nconst t = initTRPC.context\u003cContext\u003e().create();\n\nconst createContext = ({ req, res }: CreateContextOptions) =\u003e {\n  const getUser = () =\u003e {\n    if (req.headers.authorization === 'meow') {\n      return {\n        name: 'KATT',\n      };\n    }\n    return null;\n  };\n  return {\n    req,\n    res,\n    user: getUser(),\n  };\n};\nexport type Context = inferAsyncReturnType\u003ctypeof createContext\u003e;\n\nconst router = t.router({\n  hello: t.procedure\n    .input(\n      z\n        .object({\n          who: z.string().nullish(),\n        })\n        .nullish()\n    )\n    .query(({ input, ctx }) =\u003e {\n      return {\n        text: `hello ${input?.who ?? ctx.user?.name ?? 'world'}`,\n      };\n    }),\n});\n```\n\nInitialize uWebsockets server and attach tRPC\n\n```typescript\nconst app = App();\n\n/* handle CORS as needed */\napp.options('/*', (res) =\u003e {\n  res.writeHeader('Access-Control-Allow-Origin', allowOrigin);\n  res.endWithoutBody();\n});\n\ncreateUWebSocketsHandler(app, '/trpc', {\n  router,\n  createContext,\n  // CORS part 2. See https://trpc.io/docs/server/caching for more information\n  responseMeta({ ctx, paths, type, errors }) {\n    return {\n      headers: {\n        'Access-Control-Allow-Origin': '*',\n      },\n    };\n  },\n});\n\n/* dont crash on unknown request */\napp.any('/*', (res) =\u003e {\n  res.writeStatus('404 NOT FOUND');\n  res.end();\n});\n\napp.listen('0.0.0.0', 8000, () =\u003e {\n  console.log('Server listening on http://localhost:8000');\n});\n```\n\n# API\n\nCreate context options\n\n```typescript\ntype CreateContextOptions = {\n  /* read-only request information */\n  req: {\n    headers: Record\u003cstring, string\u003e;\n    method: 'POST' | 'GET';\n    query: URLSearchParams;\n    path: string;\n  };\n  /* see https://unetworking.github.io/uWebSockets.js/generated/interfaces/HttpResponse.html */\n  res: {\n    writeStatus(status: RecognizedString): HttpResponse;\n    writeHeader(key: RecognizedString, value: RecognizedString): HttpResponse;\n  };\n};\n```\n\n# Enabling subscrptions\n\nSimple method: enable subscriptions when creating the main handler.\n\n```typescript\ncreateUWebSocketsHandler(app, '/trpc', {\n  router,\n  createContext,\n  enableSubscriptions: true,\n});\n```\n\nRecommended method: enable subscriptions after registering main request handler.\n\n\u003c!-- For example, cookies are not accessible inside WSHandler createContext, so in order to implement auth query string param with jwt needs to be implemented. --\u003e\n\n```typescript\nconst app = App();\n\ncreateUWebSocketsHandler(app, '/trpc', {\n  router,\n  createContext: ({ req, res }) =\u003e {},\n});\n\napplyWSHandler(app, '/trpc', {\n  router,\n  createContext: ({ req, res }) =\u003e {},\n});\n```\n\n## example of subscrption client\n\n```typescript\nimport {\n  createTRPCProxyClient,\n  createWSClient,\n  httpBatchLink,\n  splitLink,\n  wsLink,\n} from '@trpc/client';\n\nconst host = `localhost:8080/trpc`;\nconst wsClient = createWSClient({ url: `ws://${host}` });\nconst client = createTRPCProxyClient\u003cAppRouter\u003e({\n  links: [\n    splitLink({\n      condition(op) {\n        return op.type === 'subscription';\n      },\n      true: wsLink({ client: wsClient }),\n      false: httpBatchLink({\n        url: `http://${host}`,\n        headers: headers,\n      }),\n    }),\n  ],\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fromanzy313%2Ftrpc-uwebsockets","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fromanzy313%2Ftrpc-uwebsockets","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fromanzy313%2Ftrpc-uwebsockets/lists"}