What are Events?
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
Event Structure
Every kSync event follows this structure:Example Event
Defining Event Schemas
Use Zod schemas to define the structure and validation rules for your events:Basic Schema
Advanced Schema Features
- Validation Rules
- Nested Objects
- Union Types
Sending Events
Once you’ve defined schemas, you can send type-safe events:Basic Event Sending
Batch Event Sending
For better performance, kSync automatically batches events:Error Handling
Listening to Events
Set up event listeners to react to events as they happen:Basic Event Listeners
Type-Safe Event Handlers
Removing Event Listeners
Event Patterns
Command Events
Events that represent user actions or commands:State Change Events
Events that represent changes to application state:System Events
Events generated by the system or external services:Event Versioning
As your application evolves, you may need to change event schemas:Schema Evolution
Migration Strategy
Best Practices
Event Naming
Event Naming
Use consistent, descriptive event names:
Schema Design
Schema Design
Design schemas for evolution and clarity:
Event Granularity
Event Granularity
Choose appropriate event granularity:
Error Handling
Error Handling
Always handle validation errors gracefully:

