tRPC + WebSocket Client

I have a WebSocket between my backend and an external server. I'm trying to get to this:
WebSocket Server → Next.js backend → users
WebSocket Server → Next.js backend → users
I have gone through tRPC's documentation on subscriptions (https://trpc.io/docs/subscriptions) and a few examples, but none seemed to work the way I need it. I currently have it as a query using WebSocket messages cached in a Map:
const cache = Map<number, Status>()
socket.onmessage = ({data}) => data.forEach(d => cache.set(d.id, d))

// then
const { data } = useQuery(undefined, { refetchInterval: 5_000 })
const cache = Map<number, Status>()
socket.onmessage = ({data}) => data.forEach(d => cache.set(d.id, d))

// then
const { data } = useQuery(undefined, { refetchInterval: 5_000 })
8 Replies
Keef
Keef2y ago
I haven’t heard the best things about trpc subscriptions so I haven’t used them. I instead use soketi and pusher js
Keef
Keef2y ago
soketi
soketi
Soketi is your simple, fast, and resilient open-source WebSockets server. 📣
Keef
Keef2y ago
Theo has an example up with incorporating it here https://github.com/pingdotgg/zapdos
GitHub
GitHub - pingdotgg/zapdos
Contribute to pingdotgg/zapdos development by creating an account on GitHub.
Keef
Keef2y ago
Soketi has kinda loose documentation but their discord is pretty helpful if you need help
Mocha
MochaOP2y ago
Thanks! Are you suggesting I let my backend be a WebSocket Client (to get the data from Server A) and also Server to the users?
Keef
Keef2y ago
I use soketi to proxy from backend to front end so my backend triggers an event and then my front end consumes it Its how you would use any managed web socket instance that zapdos repo is a good example So what i'm saying is don't use trpc for this, just use the pusher client
Mocha
MochaOP2y ago
Thanks again! Yes, it’s pretty clear now and the documentation is good I didn’t need true real-time communication (a 1-min delay is fine) but this should be the cleanest way so I don’t end up with a million useless requests
Keef
Keef2y ago
np np!!
Want results from more Discord servers?
Add your server