altA
tRPC8mo ago
6 replies
alt

event emitter doesnt trigger subscription from webhook

I'm using subscription in nextjs using SSE, im trying to trigger subscription when webhook receives data, webhook is seprated page api/webhook/route.ts and subscription is in root.ts, I even tested subscription using little mock of mutation and subsciption procedures and it was working fine,

here is the test which works ✅
 test: createTRPCRouter({
    emit: protectedProcedure
      .input(z.object({ message: z.string() }))
      .mutation(async ({ input }) => {
        mediaEvents.emit("test-event", {
          ...input,
        });
      }),
    on: protectedProcedure
      .subscription(async function* ({ signal }) {
        const eventKey = `test-event`;

        try {
          for await (const [data] of on(mediaEvents, eventKey, { signal })) {
            yield data;
          }
        } catch (error) {
          console.error(error);
        }
      }),
  }),
Was this page helpful?