#5883·trpc

docs: WebSocket docs improvements

Author: kzuraniewskiCreated Jul 9, 2024Updated Sep 12, 2026
Labels📚 documentation / examples✅ accepted-PRs-welcome

Area of Improvement

When following the WebSocket configuration guide, I found it lacking of some information that I think would be a good addition:

  • How to setup WebSocket client along the standard HTTP client
  • How to setup WebSocket server for a nested router

Another thing is, when defining a WebSocket handler for an already existing router in Express.js, I could not reuse the following createContext function as the types mismatch:

typescript
const createContext = ({ req, res }: CreateExpressContextOptions) => {
    const jwt = validateToken(req.cookies['token']);

    return {
        jwt,
    };
};

In some examples I've noticed the type CreateExpressContextOptions | CreateWSSContextFnOptions being applied on the argument. The problem is, I am accessing res.cookies property that is omitted in the union. What confuses me is whether this is a TypeScript problem or an implementation problem. Should I type the createContext properly? Should I adjust it to handle both the Express middleware and the WebSocket handler? Or should I create a separate one for both? Some coverage on this topic would also be welcome.

Link to related docs

https://trpc.io/docs/websockets

Additional information

No response

‍‍ Contributing

  • ‍♂️ Yes, I'd be down to file a PR implementing the suggested changes!