← All posts

The realtime engine

Every write in Dawnshard notifies connected clients. Not by firing an event per write and hoping the client keeps up, but by batching changes within an account, filtering them to what each profile is actually permitted to see, and delivering them once.

What we built

After every write, the base service records the change as dirty in the realtime hub, scoped to the account the record belongs to. The hub holds an in-memory buffer per account and waits for a short batch window before flushing.

When the window closes, the hub coalesces multiple writes to the same record down to the latest dirty state and notifies the relevant connected profiles. The notification tells the client what has changed, not what the new authoritative state is. The client refreshes through the normal read path, keeping authorization, filtering, and data resolution in one place. A device that disconnects keeps a ledger of unacknowledged dirty records so those invalidations can be recovered when it reconnects. If that ledger becomes too stale, the client falls back to a full refresh.

The transport sits behind an adapter contract. The concrete implementation is Socket.IO. Swapping it is a plugin job.

The problem it solves

Without realtime, collaborative applications fall back to polling or manual refresh. Multiple people working in the same account, checking in inventory, recording ingredient use, placing orders. If each client is polling for state, you either poll aggressively and hammer the server, or poll slowly and let the UI go stale. Neither is a good answer for work that people are doing at the same time.

The batch window exists for the same reason. Firing one realtime event per write would spam connected clients during any operation that touches multiple records. A five-second window is short enough that updates feel live and long enough to absorb a burst of writes cleanly.

The account scope follows naturally from how the product works. Collaboration happens within an account. The people who need to be kept in sync are the people working in the same account.

The challenge

The whole system was fiddly to get right, and most of the difficulty was the same problem that came up in previous posts: finding the correct boundary.

The realtime engine has to respect RBAC. A write that one profile is not permitted to see should not appear in their notification batch. But resolving permissions at broadcast time, across potentially many profiles and models in a single flush, has to be done without becoming a performance problem of its own.

Getting the broadcast correct meant being precise about what the hub is responsible for: it knows what changed and who is connected, but permission decisions go through the access engine the same way they do everywhere else. The boundary between those two concerns had to be clean, and getting there took some iteration.

For the first version, we deliberately stopped short of building a replication system. The socket only needs to say what became dirty. Authoritative state still comes through the existing read path, which meant realtime did not need to duplicate the application's authorization and data-resolution behaviour.

What makes this different

The distinction is not that the realtime engine does authorization. It is that it participates in the same object ownership and RBAC model automatically, without each domain having to separately wire up realtime subscriptions or permission checks for notifications.

A new object type gets realtime notifications through the same path as everything else. There is no channel to define, no subscription model to maintain alongside the access control model, no separate authorization logic to write for the notification layer. It is already there because the engine knows who owns what.

What this unlocks

Dawnshard now has a general-purpose realtime invalidation layer that any domain gets through the same object lifecycle. It is intentionally a small first version: the same boundary can later support richer replay and durable infrastructure without changing which part of the system owns authoritative state.

What's next

The next post covers the business rule engine: how account-level policy and tier limits are evaluated against writes, and how that fits into the same service layer described in earlier posts.

Liked this post? Tap the heart to let us know.

Get early access

Be the first to hear about early access, roadmap updates, and exclusive early adopter pricing.

Join the early access list