Subscriptions / WebSockets
Using Subscriptions
tip
- For a full-stack example have a look at /examples/next-prisma-starter-websockets.
- For a bare-minimum Node.js example see /examples/standalone-server.
Adding a subscription procedure
server/router.tstsx
server/router.tstsx
Creating a WebSocket-server
bash
bash
server/wsServer.tsts
server/wsServer.tsts
Setting TRPCClient to use WebSockets
tip
You can use Links to route queries and/or mutations to HTTP transport and subscriptions over WebSockets.
client.tstsx
client.tstsx
Using React
See /examples/next-prisma-starter-websockets.
WebSockets RPC Specification
You can read more details by drilling into the TypeScript definitions:
query / mutation
Request
ts
ts
Response
... below, or an error.
ts
ts
subscription / subscription.stop
Start a subscription
ts
ts
To cancel a subscription, call subscription.stop
ts
ts
Subscription response shape
... below, or an error.
ts
ts
Errors
See https://www.jsonrpc.org/specification#error_object or Error Formatting.
Notifications from Server to Client
{ id: null, type: 'reconnect' }
Tells clients to reconnect before shutting down the server. Invoked by wssHandler.broadcastReconnectNotification().