Realtime collaboration logic in the guide

There’s a part of the collaboration guide that doesn’t look quitre right to me.

The code example is given here:
Client Setup for Collaborating with PDF.js Express | Documentation

annotManager.on('annotationChanged', (annotations, type, { imported }) => {
  if (imported) {
  annotations.forEach(annotation => {
    annotManager.exportAnnotCommand().then(xfdf =>{
      if (type === 'add') {
            let parentAuthorId = null;
            if (annotation.InReplyTo) {
              parentAuthorId = annotManager.getAnnotationById(annotation.InReplyTo).authorId || 'default';

The code above listens for the annotationChanged event and receives a list of changed annotations. It iterates through them and calls exportAnnotCommand() on each one. I thought that exportAnnotCommand() just returns a command for all changes since the last time it was called. The annotations returned are not directly related to the annotation in the current iteration.

Also, the annotationChangedEvent seems to group changes by change ‘type’ (add, modify, delete). Whereas exportAnnotCommand could in theory produce a mixed bag of changes in the xfdf command.

In fact, it looks like the exportAnnotCommand() is not synchronised with the annotationChanged event. It’s difficult to reproduce, but I think I’ve seen the annotationChanged event fire before exportAnnotCommand is able to return any changes. I.e I’ve called exportAnnotCommand in the event handler and it doesn’t return any changes in the xfdf command, but a subsequent call does. I’ve noticed it in the logs, but haven’t been able to reproduce it, so I caould be mistaken about this.

Hello, I’m Ron, an automated tech support bot :robot:

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Hey there,

exportAnnotCommand and exportAnnotations can both be used in this situation. exportAnnotCommand exports the entire annotation, and not just the changes since last time. The only difference is that the exported format is a bit different.

I would recommend using exportAnnotations since its better to store raw XFDF in your database (since it is the PDF spec).

Thanks for pointing this out! I will update that guide to use the recommended approach.