1

I am building an app with Next.JS and tRPC. As I need real-time communication inside my app, I want to use Websockets, which are nicely integrated into rRPC as subscriptions. (See https://trpc.io/docs/subscriptions)

In the documentation of tRPC they are creating a ws Server, however it is not documented for Next.

My questions is: How can I integrate Websocktes in this use case into Next, as I cannot find a way to run the ws Server with Next. I tried to import the file wsServer.ts in _app.tsx or in api/trpc/[trpc].ts without getting it to work. When doing so I always get errors of some node libraries like fs are not found even when ensuring that the code is run on the server side.

Lukas Thiersch
  • 155
  • 2
  • 15

1 Answers1

2

You have to create a custom server to wrap your Next.js app in order to use it.

We have a full example that is ready to deploy here: https://github.com/trpc/examples-next-prisma-starter-websockets

Beware that you can't host WebSocket servers on Vercel.

Alex
  • 1,689
  • 18
  • 27