pjnicolas
pjnicolas
TtRPC
Created by pjnicolas on 7/25/2023 in #❓-help
"Fast Refresh" messing with my WebSocketServer in Next.js development
I got my websockets up and running in tRPC + Next.js with this code:
import { WebSocketServer } from 'ws'
import { applyWSSHandler } from '@trpc/server/adapters/ws'

// [...]

const wss = new WebSocketServer({
port: 3001,
})

const handler = applyWSSHandler({
wss,
router: appRouter,
createContext,
})
import { WebSocketServer } from 'ws'
import { applyWSSHandler } from '@trpc/server/adapters/ws'

// [...]

const wss = new WebSocketServer({
port: 3001,
})

const handler = applyWSSHandler({
wss,
router: appRouter,
createContext,
})
...and it's working like a charm. But when I change something in any server file and reload my webapp, I get this error in my server console:
WebSocket Server error Error: listen EADDRINUSE: address already in use :::3001
[...]
WebSocket Server error Error: listen EADDRINUSE: address already in use :::3001
[...]
So I guess Next.js is re-running the files and trying to re-create the server. Is there any way I can prevent Next.js of doing that? Or is there any way to close de server just before the hot-reload? Am I doing something wrong, or is there any other possible solution? Thanks!
6 replies
TtRPC
Created by pjnicolas on 7/21/2023 in #❓-help
How would you implement basic auth session tokens with tRPC?
I'm building a hobby app which requires basic auth. I'm used to use express and express-session along a SQL database (for the user data) and Redis (for the session data) to achieve that very easily, but I'm interested in learning tRPC. My app is built in Next.js as a monorepo, and I would like to NOT implement JWT for authentication, just plain old session tokens. I'm thinking in implementing my own version on express-session for tRPC, but that sounds dangerous as I'm not that familiar with the technology or with backend code in general. What would you suggest to keep it simple? I don't want to rely on external services like auth0 or firebase, but keep everything in my own server. This is a hobby app and it's not going to have a lot of active users. Thanks!
10 replies