Live Updates & File Uploads

Publishing Committed Changes


Learning Objectives

  • You can publish an observation after a stored operation commits.
  • You can distinguish committed state from delivery of a notification.
  • You can construct a public observation without exposing private source fields.

The stream delivers public snapshots to an open viewer. We now follow the operation that produces a new snapshot. An accepted bid changes a stored price. Its notification tells the viewer about that change. What happens if the write succeeds but delivery fails? The stored result and its notification have separate outcomes.

Committed state and notification failures

Here, publishing means sending a snapshot to the event hub’s listeners. If an operation publishes its accepted state, publication follows the completed transaction. A rejected operation publishes nothing. Conversely, a notification failure after commit cannot undo the saved result or justify repeating the change. The browser recovers by reading current state.

Suppose a bid raises the stored price to 1250 cents at version eight. The transaction finishes, but sending its snapshot to listeners throws. The buyer’s bid is still accepted. A viewer left at version seven can recover by reading the current record; repeating the bid would request another change instead.

Figure 1 follows that failed delivery after the stored operation has completed. The bidder receives the accepted result, while a separate viewer can obtain the same version through a current read.

Figure 1. Publication follows commit; a failed notification leaves acceptance intact and a current read available to the viewer.

In the next scenario, trace the stored record and the result available to the caller at each stage.

A bid is still being committed

0 / 5 points

The protected placeBid operation returns a promise that resolves only after its database transaction commits. A wrapper also publishes the accepted price to an event hub, whose publish call can throw. Which ordering preserves the meaning of an accepted bid?

The service promise confirms the bid outcome before publication begins. Its caller and the listeners can therefore receive different results:

StageResult available to the callerPublication
Persistence is pending.Still pending.Nothing yet.
The bid promise rejects.Propagate that rejection.Nothing.
The bid commits and delivery succeeds.Return the same accepted result.Publish its public observation once.
The bid commits and delivery throws.Still return the same accepted result.Report the delivery failure separately.

Catching both persistence and delivery as one failure would obscure a committed write. Keep the publication error boundary after awaiting the service. That lets the caller retain its accepted result even when an observer needs to catch up later.

Check publication order

A recording publisher can show which snapshots it received. Hold a bid’s persistence promise pending: publication should still be empty. Resolve that promise, then inspect the accepted result and publication. Reject it instead and check that no event is sent. If publication throws after acceptance, the caller must still receive the accepted result.

Controlling when the persistence promise settles makes publication order observable.

Types and public representations

The committed row can contain private fields that listeners must not receive. A TypeScript type describes which properties code can use through a variable. Annotating an existing value does not construct another object or delete extra runtime properties. For example, assigning a complete account record to a variable typed as { displayName: string } does not remove its password hash. Serializing that value can still include the extra fields.

A public observation therefore needs an explicit representation of the permitted fields. Check that distinction on a small serialization example before connecting the committed operation to the hub.

Inspect a serialized observation

0 / 5 points

This TypeScript program is valid. Which properties appear in the JSON string?

type PublicState = { listingId: number; version: number };
const row = {
listingId: 12,
version: 4,
ownerEmail: "seller@example.test",
};
const observed: PublicState = row;
const json = JSON.stringify(observed);

The next exercise returns the original accepted result to the caller and sends a small public snapshot to the hub. Its typed service fulfills with a committed bid marked accepted: true, or rejects a declined bid. A delivery failure must leave the committed result available to the caller.

Publish only committed public state

0 / 20 points

Goal

Publish a public observation after its bid has committed.

Files and supplied code

Edit api/src/live/live-services.ts, completing publicSnapshot and withLiveBids. The starter gives the Listing, Snapshot, BidResult, Services and Hub types. The optional report(error) dependency records delivery failures and does not throw.

BidResult contains accepted: true. Every fulfilled bid result is committed acceptance; declined bids reject the promise.

Task

publicSnapshot(row) returns null for drafts and unsupported statuses. For active/closed rows construct exactly listingId, currentPrice, endsAt, status and version, keeping their existing values. Never copy private source fields. Complete withLiveBids(service, hub, report), preserving the service’s other methods. Its bid method must invoke the original bid once with the same arguments and await its committed result. Publish the result’s public snapshot once, when one exists, then return that same accepted result.

A rejected bid publishes nothing. If publication throws after commitment, report that delivery error and still return the accepted result. Do not increment versions or retry mutations.

Inputs follow the supplied types; operations may reject and publishing may throw.

Examples

A pending bid promise produces no event before it resolves. A committed version 8 publishes version 8 without exposing any private fields in its source row. If publication throws, the bid remains accepted and the reporter receives that error.

Check and submit

Choose Submit solution to run the checks. Submit only api/src/live/live-services.ts. Public projection is worth 5 points, one postcommit publication 10, and rejection/delivery-failure behavior 5.

The wrapper now publishes committed public state without changing the caller’s accepted result when delivery fails. A later read can recover that state.

An in-memory hub belongs to its API process. Observing changes from another process needs an additional mechanism; the optional background explains that boundary.

Next, we follow these observations into the browser and recover its confirmed state after a connection gap.

Check Your Understanding

  1. Why must publication wait until the stored operation commits?
  2. What should the caller receive if notification throws after acceptance?
  3. Why does a TypeScript public type not remove private runtime fields?
  4. How does a current snapshot repair a missed notification without repeating the write?