Skip to main content

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

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

Use consistent, descriptive event names:
Design schemas for evolution and clarity:
Choose appropriate event granularity:
Always handle validation errors gracefully:

Performance Considerations

Event Size

Keep events reasonably sized for optimal performance:

Validation Performance

Zod schemas are fast, but complex validations can impact performance:

Next Steps

Storage

Learn how events are persisted and retrieved

Materializers

Transform events into queryable state

Sync

Understand real-time event synchronization

API Reference

Explore the complete event API