Setting up listen/trigger events for real-time collaboration in the client
In realtime collaboration, a client will merely act as a listener or trigger for events upon data creation/modification/deletion updating the current user or sending updates to other users.
Create a JavaScript file and name it
main.js.Instantiate WebViewer on a DOM element, making sure to wrap this code and any further code inside
$(document).ready(). Initial document can be any PDF or XOD file.
$(document).ready(() => {
WebViewer({
path: "lib",
initialDoc: "MY_INITIAL_DOC.pdf",
documentId: "unique-id-for-this-document"
}, document.getElementById('viewer'))
.then(instance => {
// do something...
});
});Create the server.
const server = new Server();Bind a callback function to DocumentViewer.documentLoaded event.
WebViewer(...)
.then(instance => {
const { documentViewer, annotationManager } = instance.Core;
documentViewer.addEventListener('documentLoaded', () => {
// Code in later steps will be added here...
});
})WebViewer(...)
.then(instance => {
const { docViewer, annotManager } = instance;
docViewer.on('documentLoaded', () => {
// Code in later steps will be added here...
});
})Inside the documentLoaded callback, bind another callback function to server's onAuthStateChanged event that is defined in
server.js. A firebase.User object will be passed as a parameter.If the user is not logged in we'll call the sign-in method that we defined in
server.js.If the user is logged in, we'll store their
uidin theauthorIdvariable, which will be used for client-side annotation permission checks.We call
server.checkAuthorwith parametersauthorId,openReturningUserPopupfunction andopenNewUserPopupfunction. These functions will be discussed in next steps.Then, we will send author information to the server and bind callback functions to annotation events. Details of the callback functions will be discussed in next steps.
Define callback functions for
annotationCreated,annotationUpdatedandserver.annotationDeletedevents. A data object will be passed as a parameter. For more information, refer to firebase.database.DataSnapshot.openReturningAuthorPopupis a callback function triggered when author data is found in the database. It will receive authorName as a parameter, and open a popup with the authorName as a visual feedback.openNewAuthorPopupis a callback function triggered when author data is not found. Then we will open a popup for a new author to setup an author name.updateAuthoris a function which will set author name in both client and server using annotationManager.setCurrentUser andserver.updateAuthor, respectively.
Define callback functions for
annotationCreated,annotationUpdatedandserver.annotationDeletedevents. A data object will be passed as a parameter. For more information, refer to firebase.database.DataSnapshot.onAnnotationCreatedandonAnnotationUpdatedhave the exact same behavior in this guide. They will useannotManager.importAnnotCommandto update the viewer with the xfdf change.We also set a custom field
authorIdfor the updated annotation to control client-side permission of the created/updated annotation.onAnnotationDeletecreates a delete command string from the annotation's id and is simply able to call importAnnotCommand on it.
After server callback functions are bound, we'll also bind a function to annotManager.annotationChanged event.
First parameter,
e, has a propertyimportedthat is set totrueby default for annotations internal to the document and annotations added byimportAnnotCommandorimportAnnotations.Then we iterate through the annotations that are changed, which is passed as the second parameter.
Third parameter, type, defines which action it was. In this guide, we'll have the same behavior for both
addandmodifyaction types.When annotations are added and modified, we will call
server.createAnnotationorserver.updateAnnotationwhich needs four variables:annotationId,authorId,parentAuthorIdandxfdf.annotationIdcan be retrieved from annotation.Id.authorIdwas saved as a reference when user logged in.parentAuthorIdrefers to the parent annotation's author id, if any. This will be used to distinguish replies, and will be referenced in server-side permission. Thus, we retrieveauthorIdof the parent annotation by using annotation.InReplyTo, which returns the annotation id of the parent annotation.xfdfcan be retrieved usingannotationManager.getAnnotCommand. It will get an XML string specifying the added, modified and deleted annotations, which can be used to import the annotation usingannotationManager.importAnnotCommandin server data callback functions.
Lastly, we will overwrite the client-side permission checking function using
annotManager.setPermissionCheckCallback. The default is set to compare the authorName. Instead, we will compare authorId created from the server.
Last updated
Was this helpful?