# Realtime collaboration logic in the guide

**URL:** https://pdfjs.community/t/realtime-collaboration-logic-in-the-guide/674
**Category:** Technical Support
**Tags:** pdfjs-express
**Created:** [March 31, 2021, 6:05pm UTC](https://pdfjs.community/t/realtime-collaboration-logic-in-the-guide/674 "2021-03-31T18:05:59Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![eric1](https://yyz1.discourse-cdn.com/flex031/user_avatar/pdfjs.community/eric1/32/206_2.png) [@eric1](https://pdfjs.community/u/eric1)
#### Post date: [March 31, 2021, 6:05pm UTC](https://pdfjs.community/t/realtime-collaboration-logic-in-the-guide/674/1 "2021-03-31T18:05:59Z")

</div>

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](https://pdfjs.express/documentation/collaboration/realtime-collaboration-client)

```
annotManager.on('annotationChanged', (annotations, type, { imported }) => {
  if (imported) {
    return;
  }
  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.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex031/uploads/pdfjscommunity/original/1X/ffbcd25c7d423db8d5acd5db201c272f4db306dd.png) [@system](https://pdfjs.community/u/system)
#### Post date: [March 31, 2021, 6:06pm UTC](https://pdfjs.community/t/realtime-collaboration-logic-in-the-guide/674/2 "2021-03-31T18:06:01Z")

</div>

**Hello, I’m Ron, an automated tech support bot 🤖**

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

- [Add annotation events - annotationChanged (add/modify/delete)](https://pdfjs.express/documentation/annotation/annotation-events#annotationchanged-(add/modify/delete))
- [CoreControls.AnnotationManager - exportAnnotCommand()](https://pdfjs.express/api/CoreControls.AnnotationManager.html#exportAnnotCommand__anchor)
- [EventHandler - on(type, fn)](https://pdfjs.express/api/EventHandler.html#on__anchor)
- [Samples for real-time collaboration](https://pdfjs.express/documentation/collaboration/samples)
- [Clear previously used tool properties like background colors](https://pdfjs.expresshttps://pdfjs.community/t/clear-previously-used-tool-properties-like-background-colors/96/1)

---

<div class="post-metadata">

### Author: ![Logan](https://yyz1.discourse-cdn.com/flex031/user_avatar/pdfjs.community/logan/32/13_2.png) [@Logan](https://pdfjs.community/u/Logan)
#### Post date: [April 5, 2021, 2:42pm UTC](https://pdfjs.community/t/realtime-collaboration-logic-in-the-guide/674/3 "2021-04-05T14:42:22Z")

</div>

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.

Logan
