export const SubscriptionComponent = () => { trpc.SubscriptionRouter.SubscriptionEndpoint.useSubscription( //Props here {}, { onData: (data) => { //Do the thing with the data }, })}
export const SubscriptionEndpoint = publicProcedure .subscription(async function* ({ _input, _ctx }) { const redisSubscriber = new Redis(...) const emitter = new EventEmitter() try { //Optional if you want to use redis await redisSubscriber.subscribe("some_channel_name") //Redis listening for messages redisSubscriber.on("message", async () => { //If you don't want to use Redis you can just use an event emitter alone emitter.emit("update", subData) }) //Async generator to keep the thread alive for await (const output of on(emitter, "update")) { //Yield the output to your frontend subscribers yield output[0] } } catch (error) { throw error } finally { redisSubscriber.unsubscribe("some_channel_name") redisSubscriber.quit() } })
.subscription