diff --git a/src/server/ably.ts b/src/server/ably.ts index 5e2c299..becab0f 100644 --- a/src/server/ably.ts +++ b/src/server/ably.ts @@ -2,13 +2,21 @@ import Ably from "ably"; import { env } from "~/env.mjs"; import type { EventType } from "../utils/types"; -const ably = new Ably.Realtime.Promise(env.ABLY_PRIVATE_KEY); +const globalForAbly = globalThis as unknown as { + ably: Ably.Types.RealtimePromise; +}; + +export const ably = + globalForAbly.ably || new Ably.Realtime.Promise(env.ABLY_PRIVATE_KEY); + +if (env.NODE_ENV !== "production") globalForAbly.ably = ably; export const publishToChannel = async ( + ablyInstance: Ably.Types.RealtimePromise, channel: string, event: EventType, message: string ) => { - const channelName = ably.channels.get(`${env.APP_ENV}-${channel}`); + const channelName = ablyInstance.channels.get(`${env.APP_ENV}-${channel}`); await channelName.publish(event, message); }; diff --git a/src/server/api/routers/room.ts b/src/server/api/routers/room.ts index f4778b3..6bb55d6 100644 --- a/src/server/api/routers/room.ts +++ b/src/server/api/routers/room.ts @@ -1,9 +1,11 @@ import { z } from "zod"; import { publishToChannel } from "~/server/ably"; +import Ably from "ably"; import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc"; import { fetchCache, invalidateCache, setCache } from "~/server/redis"; +import { env } from "~/env.mjs"; export const roomRouter = createTRPCRouter({ // Create @@ -15,6 +17,8 @@ export const roomRouter = createTRPCRouter({ ) .mutation(async ({ ctx, input }) => { if (ctx.session) { + const ably = new Ably.Realtime.Promise(env.ABLY_PRIVATE_KEY); + const room = await ctx.prisma.room.create({ data: { userId: ctx.session.user.id, @@ -29,6 +33,7 @@ export const roomRouter = createTRPCRouter({ await invalidateCache(`kv_roomlist_${ctx.session.user.id}`); await publishToChannel( + ably, `${ctx.session.user.id}`, "ROOM_LIST_UPDATE", "CREATE" @@ -120,6 +125,8 @@ export const roomRouter = createTRPCRouter({ }) ) .mutation(async ({ ctx, input }) => { + const ably = new Ably.Realtime.Promise(env.ABLY_PRIVATE_KEY); + if (input.reset) { if (input.log) { const oldRoom = await ctx.prisma.room.findUnique({ @@ -202,7 +209,7 @@ export const roomRouter = createTRPCRouter({ }); if (newRoom) { - await publishToChannel(`${newRoom.id}`, "ROOM_UPDATE", "UPDATE"); + await publishToChannel(ably, `${newRoom.id}`, "ROOM_UPDATE", "UPDATE"); } return !!newRoom; @@ -212,6 +219,8 @@ export const roomRouter = createTRPCRouter({ delete: protectedProcedure .input(z.object({ id: z.string() })) .mutation(async ({ ctx, input }) => { + const ably = new Ably.Realtime.Promise(env.ABLY_PRIVATE_KEY); + const deletedRoom = await ctx.prisma.room.delete({ where: { id: input.id, @@ -224,12 +233,18 @@ export const roomRouter = createTRPCRouter({ await invalidateCache(`kv_roomlist_${ctx.session.user.id}`); await publishToChannel( + ably, `${ctx.session.user.id}`, "ROOM_LIST_UPDATE", "DELETE" ); - await publishToChannel(`${deletedRoom.id}`, "ROOM_UPDATE", "DELETE"); + await publishToChannel( + ably, + `${deletedRoom.id}`, + "ROOM_UPDATE", + "DELETE" + ); } return !!deletedRoom; diff --git a/src/server/api/routers/vote.ts b/src/server/api/routers/vote.ts index 388d1f9..e97fae1 100644 --- a/src/server/api/routers/vote.ts +++ b/src/server/api/routers/vote.ts @@ -1,9 +1,11 @@ import { z } from "zod"; import { publishToChannel } from "~/server/ably"; +import Ably from "ably"; import type { Room } from "@prisma/client"; import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc"; import { fetchCache, invalidateCache, setCache } from "~/server/redis"; +import { env } from "~/env.mjs"; export const voteRouter = createTRPCRouter({ countAll: protectedProcedure.query(async ({ ctx }) => { @@ -66,6 +68,8 @@ export const voteRouter = createTRPCRouter({ set: protectedProcedure .input(z.object({ value: z.string(), roomId: z.string() })) .mutation(async ({ ctx, input }) => { + const ably = new Ably.Realtime.Promise(env.ABLY_PRIVATE_KEY); + const vote = await ctx.prisma.vote.upsert({ where: { userId_roomId: { @@ -100,7 +104,7 @@ export const voteRouter = createTRPCRouter({ await invalidateCache(`kv_votecount_admin`); await invalidateCache(`kv_votes_${input.roomId}`); - await publishToChannel(`${vote.roomId}`, "VOTE_UPDATE", "UPDATE"); + await publishToChannel(ably, `${vote.roomId}`, "VOTE_UPDATE", "UPDATE"); } return !!vote;