feat(core-realtime): realtime-ping proof-of-life channel pair
This commit is contained in:
@@ -13,3 +13,10 @@ export { SocketIORealtimeBroadcaster } from "./socket-io-realtime-broadcaster";
|
|||||||
export { SocketIORealtimeServer } from "./socket-io-realtime-server";
|
export { SocketIORealtimeServer } from "./socket-io-realtime-server";
|
||||||
export { authorize } from "./authorize";
|
export { authorize } from "./authorize";
|
||||||
export { matchChannelTemplate } from "./channel-template";
|
export { matchChannelTemplate } from "./channel-template";
|
||||||
|
export {
|
||||||
|
realtimePingChannel,
|
||||||
|
realtimePongChannel,
|
||||||
|
realtimePingInboundDescriptor,
|
||||||
|
type PingPayload,
|
||||||
|
type PongPayload,
|
||||||
|
} from "./realtime-ping";
|
||||||
|
|||||||
36
packages/core-realtime/src/realtime-ping.ts
Normal file
36
packages/core-realtime/src/realtime-ping.ts
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import { z } from "zod";
|
||||||
|
import { defineRealtimeChannel } from "./realtime-channel";
|
||||||
|
import type { IRealtimeBroadcaster } from "./realtime-broadcaster.interface";
|
||||||
|
import type { IInboundDescriptor, RealtimeContext } from "./realtime-handler.interface";
|
||||||
|
|
||||||
|
const pingSchema = z.object({ at: z.string().datetime() }).strict();
|
||||||
|
const pongSchema = z.object({ at: z.string().datetime(), echo: z.string() }).strict();
|
||||||
|
|
||||||
|
export type PingPayload = z.infer<typeof pingSchema>;
|
||||||
|
export type PongPayload = z.infer<typeof pongSchema>;
|
||||||
|
|
||||||
|
export const realtimePingChannel = defineRealtimeChannel(
|
||||||
|
"realtime.ping",
|
||||||
|
pingSchema,
|
||||||
|
{ scope: "authenticated" },
|
||||||
|
);
|
||||||
|
|
||||||
|
export const realtimePongChannel = defineRealtimeChannel(
|
||||||
|
"realtime.pong",
|
||||||
|
pongSchema,
|
||||||
|
{ scope: "authenticated" },
|
||||||
|
);
|
||||||
|
|
||||||
|
export function realtimePingInboundDescriptor(
|
||||||
|
broadcaster: IRealtimeBroadcaster,
|
||||||
|
): IInboundDescriptor<typeof realtimePingChannel.name, typeof pingSchema> {
|
||||||
|
return {
|
||||||
|
descriptor: realtimePingChannel,
|
||||||
|
handler: async (input: PingPayload, ctx: RealtimeContext): Promise<void> => {
|
||||||
|
await broadcaster.broadcast(realtimePongChannel, {
|
||||||
|
at: input.at,
|
||||||
|
echo: ctx.userId ?? "anonymous",
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user