const listSubscription= profileProcedure .input( z.object({ matchId: z.string(), }), ) .subscription(async function* ({ signal, input, ctx }) { console.log("connection opened"); await ctx.eventSub.subscribe(input.matchId); //eventsub is a redis client const messageQueue: any[] = []; const messageHandler = (channel: string, message: string) => { const post = JSON.parse(message); messageQueue.push(post); }; ctx.eventSub.on("message", messageHandler); try { while (!signal?.aborted) { if (messageQueue.length > 0) { yield messageQueue.shift(); } await new Promise((resolve) => setTimeout(resolve, 1000)); } } finally { console.log("connection closed"); ctx.eventSub.off("message", messageHandler); ctx.eventSub.unsubscribe(input.matchId); } })