Help for subscription with redis
My subscription is currently as in the function below. It works, but it doesn't make sense to check the response from redis once every second. How can I make the subscription more performant? I couldn't find an example using redis or rabbitmq.
7 Replies
Why are you doing the timeout and the weird while loop?
If you take a look at https://trpc.io/docs/server/subscriptions
You should probably be able to turn you subscriber into an iterable using
import { on } from "node:events"
then you can for await
that result instead of the message queue stuff you are doing.Subscriptions | tRPC
Introduction
@BeBoRE
I had looked at the example in the document but it had not occurred to me that the on function in node:events could work with redis. I have never used eventemitter before.
Do you think the following setup is correct?
Try it out, the Redis client implements or extends the EventEmitter, so it should work in theory.
This was the exact issue I ran into today. We have a redis client too and I noticed the depreciation notice on our subscriptions that now just return a resolver directly. In the websocket examples I got lost between the deprecated warning and the examples because they looked identical. Then I figured that we only use 1 way messaging so websockets are overkill. I would love to see an example for redis with event emitter (or maybe the one above is that example i missed) For some reasons our redis client is not in the ctx. Is there a pattern or reason for being there?
This is just a pattern they are using, where they pass the client to the subscription handler by context. https://trpc.io/docs/server/context. There is no one way of getting the client to the handler, just make sure you aren’t initializing a new client with every request.
Good call... Have to check that indeed!
Gave it a shot, did not get it to work with ioredis. 😦 have to do some more digging. The examples on trpc website are great but only consider there is one instance of node server running, so if anyone has an example of using redis pubsub with web sockets/SSE that would be fanatastic to add.
This seems to work for me, no idea if it is best practice
unfortunately you cannot provide trpc.context.someMeta used in subscription link so i could opt for Websocket vs SSE
workaround: use op.input and have some check for a key in an object