Skip to main content

How to Design a Good Realtime Data Model in your own Collabrative App

Use Case Requirement of my Mobile App:

Let us consider the following use case of my mobile app to design a good realtime data model.

Use case: Reconcile chat messages in multiple devices. I need to show up history data generated by one device in another device in real time when user login in two different devices using same account details.

To implement this we need a central storage server and a synchronization service.

Solution:

Google Drive can be used as a file storage server and also I need to look for a javascript library or we should implement Operational Transformations (OT) that would allow us to synchronize history data in realtime between multiple devices. And Operational Transformations seems to fit my need for realtime sync. Google Drive Realtime API provides synchronization service for files in Google Drive via the use of Operational Transforms. This API is a JavaScript library hosted by google that provides collaborative objects, events, and methods for creating collaborative applciations.

The user has to authorize App access to his Google Drive. The following details explains how i did realtime data synchronization for my phonegap application and similarly you can do for any applicaitions (Web applications, Mobile applications and PhoneGap applications).

As I explained Google Drive Realtime API takes care of everything. Only thing that you have to do is to design a good Shared/Realtime/Collaborative data model and focus on how you can build your app. I have designed the below shared data model for my app.

Designing a Good Realtime Data Model:

The following is the proposed data model for my mobile application to implement realtime sync with Google Drive.



The Realtime/Shared/Collaborative Map entry contains a key and value pair. The Map's key must be a String. My mobile app/ chat application uses Cloud reference id as key which is generated using the combination of user's jabber id and timestamp of the chat session. The values can contain realtime collaborative objects. Here the value of the map would be collaborative list that contains the messages for the particular chat session.

Changes to the collaborative map will automatically be synced with ther server and other collaborators. To listen for changes we need to add EventListeners for the gapi.drive.realtime.EventType.VALUE_CHANGED event type.

Changes to the list will automatically be synced with the server and other collaborators. To listen for changes, add EventListeners for the following event types:

gapi.drive.realtime.EventType.VALUES_ADDED
gapi.drive.realtime.EventType.VALUES_REMOVED
gapi.drive.realtime.EventType.VALUES_SET

First entry in the collaborative list should always contain meta data about my app chat history session which includes cloud reference id, reciever jabber id, isActive flag , initiator device id etc. The remaining entries in the list are actual IM chat messges.

Web SQL Realtime Sync With Google Drive:

The above Realtime/Shared data model is designed based on the local database (WebSQL Storage)/ Data Models of my mobile application. The data model for chat history of my mobile application has the following two entities/tables: 
  1. History table (For storing Chat Sessions)
  2. IM table (For storing Messages)

Each chat session contains collection of messages chat by users. The session can be two party chat session or multiparty chat session. Whenever a collaborative data model is modified. The API then silently sends a representation of the change to the server (called a "mutation") so that the change can be recorded in the realtime file and communicated to other collaborators.


So we have discussed in detail how to design collaborative/realtime data model by understanding your local storage/ local data models. And also discussed how to do realtime data synchronization between two or more collaborators.

Comments

Popular posts from this blog

How to Build Realtime Collaborative Apps using Google Drive Realtime API

Google Drive lets users create Docs, Sheets, and Slides, collaborate on them in realtime. The Google Drive Realtime API provides collaboration as service for files in Google Drive via the use of Operational Transforms. Using Google Drive Realtime API, you can now easily build your own collaborative apps. Realtime API is based on the same collaboration technology used by Google Docs. Realtime Collaboration System: What Google Drive Realtime API does for you ? And what you have to do ? Realtime API handles everything for you like network communication, storage, conflict resolution, and other collaborative details: Functions to load and work with Realtime documents. Built-in collaborative objects (Strings, Lists and Maps) Also you can create your own custom collaborative objects. Events for detecting changes to the collaborative data model. A text binder to bind your collaborative objects to the DOM. Google Drive Realtime API provides all the tools you need to ...

Know Where to Place Chitika or Adsense Ads for Maximum Revenue

After you open your Adsense or Chitika account, don't simply copy and paste the ad code where you want in the website.Because there are many things that you need to consider while placing your ads in your website in order to effectively increase your revenue with Adsense or Chitika. The following are the main/basic mantra's to maximize your revenue. Choosing the Best Spots for your Ad Placement: The following picture shows you the best spots for placing ad units on your website. Place the ad directly below your blog post’s title and then followed by post content. Place the ad within the content of  your blog post. Place the ad directly below the post content and before the comments section. Customize your Ad: Login to your publisher account ,  Choose the appropriate ad format based on the spot for placing your ad unit. Select the color scheme which suits your blog content color scheme Get the Ad code and place in your website. Displaying...

How to make Chrome Packaged App that's trying to load realtime data model

A packaged app (Chrome App) cannot have less restrictive CSP (Content Security Policy) than the default CSP value. And it looks like the Google Drive Realtime API cannot be used in the Packaged app's without any hacks. Because the default packaged app Content Security Policy (CSP) value disallows the use of eval() or new Function(). However, a variety of library uses eval() and eval-like constructs such as new Fucntions() for performance optimization and for ease of expression. It will result in following error: "EvalError when trying to load realtime data model" Steps to resolve this issue: Load GAPI using a webview tag. Load Webview inside standalone localhost. Use a chrome socket. Handle your own OAuth flow and supply the token to the gapi auth client. This way you can make your Chrome Packaged App to work with Google Drive Realtime API.