forrealtime

Type Inference

Infer event types directly from your server schema without duplicating definitions.

Pass typeof realtime to createRealtime and the hook's types are inferred directly from your Zod schema, so you do not need to duplicate event definitions on the client.

Export realtime from your server file:

// server.ts
export const realtime = new Realtime({ schema, redis });
export const GET = handle({ realtime });

Then import it as a type on the client. TypeScript resolves the type without pulling any server code into your Svelte bundle:

// realtime.ts
import { createRealtime } from "forrealtime/client/svelte";
import type { realtime } from "./server";

export const { useRealtime } = createRealtime<typeof realtime>();

You can also pass a plain events type directly if you prefer not to import from the server:

type Events = {
  notification: {
    alert: string;
  };
  chat: {
    message: { text: string; user: string };
  };
};

export const { useRealtime } = createRealtime<Events>();

InferRealtimeEvents

Use InferRealtimeEvents when you need the inferred event payload types elsewhere in your app.

import type { InferRealtimeEvents } from "forrealtime";
import type { realtime } from "./server";

type Events = InferRealtimeEvents<typeof realtime>;

function handleEvent(event: Events["notification"]["alert"]) {
  console.log(event);
}

On this page