Events are the fundamental building blocks of kSync. They represent immutable facts about what happened in your application. Instead of directly modifying state, you create events that describe the changes.
Immutable
Events are never modified once created, providing a reliable audit trail
Type-Safe
Every event is validated against a Zod schema at runtime
Timestamped
Events include precise timestamps for ordering and conflict resolution
Traceable
Full provenance tracking with client ID and version numbers
interface KSyncEvent<T = unknown> { id: string; // Unique identifier (UUID) type: string; // Event type (e.g., 'message-sent') data: T; // Event payload (your data) timestamp: number; // Unix timestamp in milliseconds clientId: string; // ID of the client that created the event version: number; // Sequence number for ordering}
// ✅ Good - Clear and specific'message-sent''user-joined-channel''file-upload-completed''payment-processed'// ❌ Bad - Vague or inconsistent'message''user''fileUploaded''payment_done'
Schema Design
Design schemas for evolution and clarity:
Copy
// ✅ Good - Extensible and clearconst OrderSchema = z.object({ id: z.string(), customerId: z.string(), items: z.array(OrderItemSchema), status: z.enum(['pending', 'confirmed', 'shipped', 'delivered']), metadata: z.record(z.unknown()).optional(), // For future extensions});// ❌ Bad - Rigid and unclearconst OrderSchema = z.object({ data: z.any(), // Too generic type: z.string(), // Unclear purpose});