You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
constcreateContext=({ req, res }: CreateExpressContextOptions)=>{constjwt=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.
I'll take this: expand the WebSocket docs with HTTP+WS together, nested routers, and the two-createContext pattern (CreateExpressContextOptions vs CreateWSSContextFnOptions, cookies from headers).
Activity
BeBoRE commentedon Jul 9, 2024
I've opted to use 2 seperate createContext functions since the logic is so different to retrieve the correct data.
You have to substract the cookies yourself from the headers when using WebSockets since Express handles that for you in the ExpressHandler.
YKDZ commentedon Dec 2, 2024
I opted to define 2 of createContext functions and used them to init trpc server:
Then follow the documentation:
For the router type issue, I take the following approach:
I'm not an expert on trpc, but the above at least works properly for me.
dewa3355 commentedon Sep 2, 2026
I'll take this: expand the WebSocket docs with HTTP+WS together, nested routers, and the two-
createContextpattern (CreateExpressContextOptionsvsCreateWSSContextFnOptions, cookies from headers).