Event-based realtime communication library built on Socket.IO, Hono, and Bun
bun add dialogue-ts zod// server.ts
import { createDialogue, defineEvent } from "dialogue-ts";
import { z } from "zod";
const Message = defineEvent("message", {
schema: z.object({
text: z.string(),
senderId: z.string(),
}),
});
const dialogue = createDialogue({
rooms: {
chat: {
name: "Chat Room",
events: [Message],
},
},
});
await dialogue.start();// client.ts
import { createDialogueClient } from "dialogue-ts/client";
const client = createDialogueClient({
url: "ws://localhost:3000",
auth: { userId: "user-123" },
});
await client.connect();
const chat = await client.join("chat");
chat.on("message", (msg) => {
console.log(msg.data.text);
});
chat.trigger("message", {
text: "Hello!",
senderId: "user-123",
});Events help you handle any types of real-time interactions with a unified mental model, every interaction is an event
You can define schemas for events using zod and validate to easily avoid mismatch bugs at runtime.
Hook into various stages of the event lifecycle for custom logic and side effects.
Server and clients share similar mental models accross interactions, so it's easy to reason about things.
Define a Message event with Zod schema for text validation. Use user:joined and user:left events for presence tracking. Add a typing event to show when users are composing messages. All events are type-safe and automatically validated.
Create events like metrics:update and data:refresh with schemas that enforce data structure. Use Dialogue's room system to organize different dashboard views. Type inference ensures your frontend receives correctly-typed data from the server.
Define cursor:move, document:edit, and selection:change events to track user interactions. Dialogue's event system handles real-time broadcasting to all participants in a room while Zod validates each action's payload.
Set up events like player:move, game:action, and lobby:join with strict schemas. Use rooms for game lobbies and matches. Dialogue validates player inputs server-side, preventing cheating and ensuring game state consistency.
| Feature | Dialogue | Socket.IO |
|---|---|---|
| Schema Validation | Built-in Zod integration | Manual validation required |
| Type Safety | Automatic type inference from schemas | Manual type definitions needed |
| Event System | Declarative event definitions with first-class support | String-based event names, no structure |
| Room Management | Configured upfront with type-safe joins | Manual join/leave handling |
| Developer Experience | Unified API, less boilerplate | More setup code, repetitive patterns |
| Runtime Safety | Events validated against schemas at runtime | No built-in validation |