Setting up your server for real-time collaboration

In realtime collaboration, a server will merely act as an online database that triggers events upon data creation/modification/deletion. As long as the above requirement is met, your server can be built in any language and stack of your choice. For the simplicity of this guide, we will be using Firebase.

  1. Go to the Firebase Console, login and create a project.
  2. Click "Add Firebase to your Web App" and copy the whole code for "Initializing Firebase". If storageBucket is empty, close the popup and try again (that's a known bug from Firebase).
  3. Create a JavaScript file and name it server.js.
  4. Paste the code that you have copied from Firebase. (Note that you should remove the script tags)
  5. Store the firebase.database.References for annotations and users. We will use these to create/update/delete data, and listen to data change events as well.

JavaScript

1window.Server = function() {
2 const config = {
3 apiKey: "YOUR_API_KEY",
4 authDomain: "PROJECT_ID.firebaseapp.com",
5 databaseURL: "https://PROJECT_ID.firebaseio.com",
6 storageBucket: "PROJECT_ID.appspot.com",
7 messagingSenderId: "YOUR_SENDER_ID"
8 };
9 firebase.initializeApp(config);
10
11 this.annotationsRef = firebase.database().ref().child('annotations');
12 this.authorsRef = firebase.database().ref().child('authors');
13};
  1. Create a custom bind function for authorization and data using firebase.auth.Auth#onAuthStateChanged and firebase.database.Reference#on.

JavaScript

1Server.prototype.bind = function(action, callbackFunction) {
2 switch(action) {
3 case 'onAuthStateChanged':
4 firebase.auth().onAuthStateChanged(callbackFunction);
5 break;
6 case 'onAnnotationCreated':
7 this.annotationsRef.on('child_added', callbackFunction);
8 break;
9 case 'onAnnotationUpdated':
10 this.annotationsRef.on('child_changed', callbackFunction);
11 break;
12 case 'onAnnotationDeleted':
13 this.annotationsRef.on('child_removed', callbackFunction);
14 break;
15 default:
16 console.error('The action is not defined.');
17 break;
18 }
19};
  1. Define a method to check if author exists in the database. We will use firebase.database.Reference#once and firebase.database.DataSnapshot#hasChild to do so.

JavaScript

1Server.prototype.checkAuthor = function(authorId, openReturningAuthorPopup, openNewAuthorPopup) {
2 this.authorsRef.once('value', authors => {
3 if (authors.hasChild(authorId)) {
4 this.authorsRef.child(authorId).once('value', author => {
5 openReturningAuthorPopup(author.val().authorName);
6 });
7 } else {
8 openNewAuthorPopup();
9 }
10 });
11};
  1. Define a sign-in method. In this guide, we will use firebase.auth.Auth#signInAnonymously.

JavaScript

1Server.prototype.signInAnonymously = function() {
2 firebase.auth().signInAnonymously().catch(error => {
3 if (error.code === 'auth/operation-not-allowed') {
4 alert('You must enable Anonymous auth in the Firebase Console.');
5 } else {
6 console.error(error);
7 }
8 });
9};
  1. From the Firebase console click the "Authentication" button on the left panel and then click the "Sign-in Method" tab, just to the right of "Users". From this page click the "Anonymous" button and choose to enable Anonymous login.
  2. Define data-write methods using firebase.database.Reference#set and firebase.database.Reference#remove.

JavaScript

1Server.prototype.createAnnotation = function(annotationId, annotationData) {
2 this.annotationsRef.child(annotationId).set(annotationData);
3};
4
5Server.prototype.updateAnnotation = function(annotationId, annotationData) {
6 this.annotationsRef.child(annotationId).set(annotationData);
7};
8
9Server.prototype.deleteAnnotation = function(annotationId) {
10 this.annotationsRef.child(annotationId).remove();
11};
12
13Server.prototype.updateAuthor = function(authorId, authorData) {
14 this.authorsRef.child(authorId).set(authorData);
15};

Last but not least, you should add server-side permission rules for writing data. Although client-side permission checking is supported in WebViewer, every user does have access to each annotation's information (including authorId and authorName). Thus, data-write permission should be regulated in the server as well. In this guide, we have used Firebase's Database Rules.

  1. Copy the JSON below and paste it in your Firebase Console's Database Rules. From the console click the "Database" button on the left panel and then click the "Rules" tab, just to the right of "Data". This will make sure that trying to modify someone else's annotation isn't allowed.

JavaScript

1{
2 "rules": {
3 ".read": "auth != null",
4
5 "annotations": {
6 "$annotationId": {
7 ".write": "auth.uid === newData.child('authorId').val() || auth.uid === data.child('authorId').val() || auth.uid === newData.child('parentAuthorId').val() || auth.uid === data.child('parentAuthorId').val()"
8 }
9 },
10
11 "authors": {
12 "$authorId": {
13 ".write": "auth.uid === $authorId"
14 }
15 }
16 }
17}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales