Define Events

In pluv.io, you can define custom events with an input validation schema and resolver, and have them be automatically type-safe without having to manage your own types.

Usage example

1import { yjs } from "@pluv/crdt-yjs";
2import { createIO } from "@pluv/io";
3import { platformNode } from "@pluv/platform-node";
4import { z } from "zod";
5
6export const io = createIO({
7 crdt: yjs,
8 platform: platformNode(),
9})
10 // When event "SEND_MESSAGE" is sent by the frontend and received
11 // on the server
12 .event("SEND_MESSAGE", {
13 // Define a zod validation schema for the input
14 input: z.object({
15 message: z.string(),
16 }),
17 // Emit a "MESSAGE_RECEIVED" from the server to the client
18 resolver: ({ message }) => ({ MESSAGE_RECEIVED: { message } }),
19 })
20 .event("EMIT_EMOJI", {
21 input: z.object({
22 emojiCode: z.number(),
23 }),
24 resolver: ({ emojiCode }) => ({ EMOJI_RECEIVED: { emojiCode } }),
25 });
26
27// Export the io type instance of the io itself
28export type AppPluvIO = typeof io;

Next steps