Realtime Cloud Storage
Notifications

  • Feb 27, 2015
  • Starting Guide

One of the key features of Realtime Cloud Storage is the ability of providing real-time notifications when data changes inside the storage. This means that’s incredibly easy to develop applications that synch data between several users. Your application simply defines which events are of interest (e.g. table inserts, item updates, item deletes, …) and Realtime Cloud Storage will push real-time messages to every interested recipient (your application users) when these events occur. For simplicity and performance the notification message will contain a snapshot of the actual item being updated, so you can use it straight away in your application login or interface without any round-trip to the Realtime Cloud Storages servers.

In our Todo example (https://github.com/realtime-framework/Storage) we are simply using a single table named todoTable where the todo listName is the primary key and the timestamp of the todo insertion is the secondary key. To allow the synchronization of a specific todo list between users, each user “subscribes” the real-time notifications on the items of todoTable where the primary key is equal to the todo list they’re using. The cool thing is that some users can be using the JavaScript app and others the iOS Objective-C app and their lists are transparently synchronized through the Realtime Cloud Storage.

There are three functions that handle the real-time notifications, on, once and off.

The on function “subscribes” (or sets a listener) on a given event and will call the appropriate callback whenever the event occurs. The once function does the exact same thing but only calls the callback at the first occurrence of the event (just once). The off function simply turns off the notifications for a given event.

To synchronize the todo list in the Todo example, each user calls the on function for the todoTable events involving items in the list they’re using (items where the primary key listName is equal to the list name they’ve entered at the app UI).

This is how it’s done in JavaScript, assuming the variable currentListName holds the name of the current list in the UI and this.storage holds a valid reference for the Realtime Cloud Storage:

filter = {item: "listName", value: currentListName };
 
// Set a reference for the items in todoTable with listName == currentListName
this.tableRef = this.storage.table(“todoTable”).equals(filter);

// When a new item is added call the this.onPut function
this.tableRef.on("put",this.onPut.bind(this));
 
// When an item is deleted call the this.onDelete function
this.tableRef.on("delete",this.onDelete.bind(this));
 
// When an item is updated call the this.onUpdate function
this.tableRef.on("update",this.onUpdate.bind(this));

The onUpdate callback function that will be invoked when an item is updated looks like this:

// itemSnapshot holds the updated item data
// so you don’t need to retrieve it from the storage
onUpdate : function(itemSnapshot){
     if(itemSnapshot){
            // load the updated item attributes into variable todo
            var todo = itemSnapshot.val();
           
            // handle the updated todo completion state
            todo.state = todo.state == 1 ? true : false;
           
            // update the UI todo list with the updated todo
            // so this user list is kept in synch with the other users list
            this.notificationCallback(todo);                                   
     }
}

In the previous examples we’ve set real-time notifications at the table level but Realtime Cloud Storage also allows you to set real-time notifications on specific items.

Just set a reference to the item and call the on or once method referring the event you’re interested in. In JavaScript it would be done like this for an update event:

// Set the item reference
// this will depend on your table key schema
var itemRef = storageRef.table("myTable").item({
       primary: "myId",
       secondary: 111222333444
});
 
// Set the item update event callback
itemRef.on("update", function(itemSnapshot) {
       // itemSnapshot holds the updated item attributes
       // write the current attributes of the item at the console
       console.log(itemSnapshot.val());
});

Your application will be able to scale to millions of concurrent users and keep the notifications latency below 200ms thanks to the high-scalability and high-performance of the Realtime Cloud Messaging Service used internally by Realtime Cloud Storage.

If you want to know more about this messaging service please go to http://framework.realtime.co/messaging/

Back to Query and Scan or proceed to Security

If you find this interesting please share: