Is it possible to run websocket (trpc subscriptions) server in the same process as nextjs app?
Official TRPC example on using websockets with next (https://github.com/trpc/examples-next-prisma-websockets-starter) seem to run them separately, but it doesn't really look right to me, as i'm not ever planning to run my app on edge or serverless, and just want this stupid-simple monolith.
GitHub
GitHub - trpc/examples-next-prisma-websockets-starter: mirror of ht...
mirror of https://github.com/trpc/trpc/tree/main/examples/next-prisma-starter-websockets - GitHub - trpc/examples-next-prisma-websockets-starter: mirror of https://github.com/trpc/trpc/tree/main/ex...
5 Replies
Absolutely
But websockets require that the client always connects to the same server. Which makes a serverless deployment impossible
You could use aws ecs / kubernetes with a sticky load balancer
Or simply run one server if elastic scaling is not needed
That example does run one process. It uses NextJS custom server.
src/server/prodServer.ts
is what programmatically starts nextjsSo is it just using separate processes in dev? I'm currently trying to set it up in my existing app created with
create-t3-app
. It already uses some utility stuff like ramda in it's trpc routers, so i've got into problems with running tsx watch src/server/trpc/wssDevServer.ts
which I saw in this templates package.json
.yes in dev is uses 2 processes, I would imagine you can do it on a single process but there is probably a reason why two separate ones have been used in dev. probably something todo with hmr if I had to guess.
Yeah problems with hmr is what I've just thought about. Would try to set up this
tsx
thing properly then, or replace it with something...