This is a cheat sheet repo for setting up tRPC with WebSocket Subscriptions.
Note: tRPC is a library for building typesafe APIs between server and client (tRPC main features work with TypeScript)
Web Sockets are a protocol for bi-directional, real-time communication between a client (such as a web browser) and a server. They are commonly used to build real-time applications, such as online games, chat applications, and collaborative editing tools.
Web Socket Subscriptions are a way to use Web Sockets to subscribe to data changes on the server.
Note: The installation will be the same as my trpc repo apart from
npm i ws
andnpm i -D @types/ws
command which installs the WebSocket library on the server
- clone repo and
cd
into it cd
intoserver
and runnpm install
cd
intoclient
and runnpm install
-
cd
intoserver
and runnpm run dev
-
cd
intoclient
and runnpm run dev
-
visit
http://localhost:5173/
to see the client
tRPC works well when you have a monorepo with a server
and client
folder on the same levelf
Note: you can setup a new tRPC project with create-t3-app
OR
- run
npm i @trpc/server cors express zod
- run
npm i ws
to install the WebSocket library - run
npm i -D @types/ws
to install types for the WebSocket library - run
npm i -D @types/cors @types/express @types/node
- nodemon ts-node typescript`
- setup
typescript
withtsc --init
- add
dev
script topackage.json
:
"dev": "nodemon SERVER_NAME.ts"
- start developing your server
Note: see this repo project for a working example
Note: You can use many frameworks as the client, but tRPC works best with t3, Next.js and React
- run
npm i @trpc/client
- run
npm i -D typescript
- setup
typescript
withtsc --init
- add
dev
script topackage.json
:
"dev": "YOUR CLIENT START COMMAND"
- start developing your client
Note: see this repo project for a working example