CloudPDF
DocsPricing
Start building

Events

Every change to a document produces an event: an annotation created, a page rotated, a field filled in. Listen to them to keep your interface in sync without reading the document again.

const stop = doc.events.on('annotations.created', (event) => {
  addToSidebar(event.annotation);
});
 
// later
stop();

on() returns a function that stops listening. To hear every event, use subscribe():

const stop = doc.events.subscribe((event) => {
  console.log(event.type);
});

What an event contains#

An event carries exactly what the call that made the change returned, plus its type and where it came from (origin). The event for create() has the same annotation that create() resolved with:

doc.events.on('annotations.updated', ({ annotation, origin }) => {
  if (origin.kind === 'remote') showToast(`${annotation.author} changed a comment`);
});
FieldWhat it is
origin.kind'local' when the change was made through this doc, 'remote' when it wasn’t.
origin.subjectThe user who made it (on the cloud).
origin.atWhen it happened, as an ISO 8601 string.
origin.transactionThe write it came from: its opId, and its place among that write’s events.
origin.undoOfOn an undo’s events: the opId of the change it undid.

An event fires after the change is saved, never before, and before the call’s promise resolves. By the time await page.annotations.create(…) returns, your listeners have run.

Every event#

TypeWhen
annotations.created, .updated, .deletedAn annotation changed.
annotations.reorderedA page’s annotations were restacked. order is the page’s new order, bottom to top.
annotations.restoredAn undo brought back deleted annotations.
annotations.flattenedAnnotations were baked into a page.
pages.reordered, .rotated, .deleted, .insertedThe page order or rotation changed.
pages.namedA page name was added or removed.
pages.flattenedAll annotations of pages were baked in.
pages.scaleSetA page’s measurement scale changed.
forms.valueSetA field was filled in.
forms.created, .updated, .deleted, .widgetAdded, .widgetRemovedA field’s structure changed.
forms.widgetUpdated, forms.widgetsReorderedA widget was moved or restyled, or a page’s widgets were restacked.
forms.widgetDeleted, forms.widgetRestoredA widget was deleted, or an undo brought it back.
forms.calculationsReorderedThe order calculate scripts run in changed.
forms.restoredAn undo brought back a deleted field.
forms.repairedMany values or fields changed at once.
attachments.created, .deletedAn attached file was added or removed.
attachments.restoredAn undo brought back a deleted attached file.
fonts.changedThe fonts you can name in the document changed. Read doc.fonts.list() again.
metadata.updatedThe title, author or another standard metadata field.
metadata.customUpdatedYour own metadata keys.
outline.created, .updated, .moved, .deletedAn outline item was added, changed, moved or deleted.
outline.restoredAn undo brought back deleted outline items.
redaction.appliedRedactions were applied.
signatures.prepared, .completed, .cancelledA signature was started or finished.
document.versionedA new version of the document was saved, such as after signing. Read what you show again.
stream.desynced(cloud) Events were missed. Read what you show again.

Changes that touch one page (annotations) carry that page, so you can update only what’s on screen.

Which write an event came from#

Every write takes an opId in its options, a name for the write, and every event it fires carries it as origin.transaction.id. Leave it out and the engine makes one up. That’s how you match an event to a change you made:

const opId = crypto.randomUUID();
doc.events.on('annotations.updated', ({ origin }) => {
  if (origin.transaction?.id === opId) markSaved();
});
await page.annotations.update(ref, { contents: 'Seen' }, { opId });

One event comes from no write: fonts.changed when you register fonts on the local engine. It has no origin.transaction.

An opId is 1 to 255 visible ASCII characters (what an HTTP header can carry), on every engine. On the cloud, a write retried with the same opId is applied once, and the retry gets the first answer. The cloud engine retries a write lost on the network itself, under the same opId, so use a fresh opId for every write. A change, and the annotation, form and metadata calls a change can hold, do the same on the local engine.

The opId is also how you undo a write: doc.apply({ undoOf: opId }). See Changes & undo.

Some calls make many changes at once. An import of 50 annotations fires 50 annotations.created events, one after the other, all with the same origin.transaction:

doc.events.on('annotations.created', ({ annotation, origin }) => {
  addToSidebar(annotation);
  const { transaction } = origin;
  if (transaction && transaction.index === transaction.count - 1) redrawSidebar(); // the last of the batch
});

transaction.id is the same for all of them, and transaction.index counts from 0 to transaction.count - 1. A write that makes one change fires one event, with transaction.index 0 and transaction.count 1.

Other people’s changes#

Everyone who has the document open hears every change as the server saves it. Changes made through this doc arrive with origin.kind: 'local'; everyone else’s with 'remote' and the user in origin.subject. Every open() is someone else here, even by the same user: a second tab, or the same document opened twice in one app (a preview beside an editor). Their changes arrive as 'remote', with the same subject.

The engine connects to the server when you add the first listener and disconnects after you remove the last, so listening is free until you need it.

If the connection drops and the engine can’t catch up with everything that happened in the meantime, you get a stream.desynced event. You get it too when someone on a newer version makes a kind of change this version doesn’t know. Read what you show again (doc.annotations.list(), doc.pages.list()); events continue from there.

Good to know#

  • Each change arrives once. Your own changes aren’t sent back to you a second time.
  • A listener that throws doesn’t stop the other listeners, and doesn’t make the call fail.
  • origin.kind is about the open document, not the user. “Did I do this?” (undo, optimistic UI): check origin.kind. “Did this user do it?”: check origin.subject.