Chapter 12. Creating a Real-time Application

In this chapter, we will create a real-time application to explore the distribution of geotagged tweets containing user-defined topics. Creating this visualization will involve implementing a server-side application and a client-side application.

The server-side application will handle connections from the clients, receive topics to be heard on Twitter, and send tweets matching the topics to the corresponding clients.

The client-side application will connect with the streaming server, send it topics as the user enters the streaming server, receive tweets, and update the visualization as the tweets arrive.

We will begin this chapter by learning the basics of real-time interaction in client-side applications. We will use the HDI Explorer application from Chapter 8, Data-driven Applications, a service that provides the necessary backend to implement real-time applications.

We will then implement the server-side application using Node, Twit, and Socket.IO, a library that provides real-time communication support. Lastly, we will use Backbone, Socket.IO, and D3 to create the client-side application.

Collaborating in real time with Firebase

In some visualization projects, it can be convenient to have the application state shared among users so that they can collaborate and explore the visualization as a group.

Adding this feature would usually imply the installation and configuration of a server and the use of WebSockets or a similar technology to manage the communication between the server and client-side code in the browser. Firebase is a service that provides real-time data storage and synchronization between application instances using the client-side code. If the data changes at one location, Firebase will notify the connected clients so that they can update the state of the application. It has libraries for several platforms, including OS X, iOS, Java, and JavaScript. In this section, we will use Firebase's JavaScript library to add real-time synchronization to the HDI Explorer application.

Adding synchronization to HDI Explorer doesn't make sense in most cases; it would be weird if a user is seeing the evolution of the Human Development Index for one country and it suddenly changes to a different country. On the other hand, if several users were seeing the same visualization on their respective computers and discussing it, this would be useful because changing the selected country would update the visualization for the rest of the users as well, so they would all see the same content. To differentiate between the two scenarios, we will create a new page of the HDI Explorer application with synchronization and leave the index and share pages as they are now. The examples of this section are in the firebase.md file of the hdi-explorer repository.

Configuring Firebase

To add real-time support to our application, we need to create a Firebase account. Firebase offers a free plan for development, allowing up to 50 connections and 100 MB of data storage, which is more than enough for our application. Once we have created our Firebase account, we can add a new application. The name of our application will be hdi-explorer. This name will be used to generate the URL that identifies our application, in our case, http://hdi-explorer.firebaseio.com. By accessing this URL, we can see and modify the application data. We will create a single object with the code attribute to store the country code of the HDI Explorer application. Once we have our account and initial data configured, we can install the JavaScript library with Bower:

$ bower install --save-dev firebase

This will install Firebase in the bower_components directory. We will also update the Gruntfile to concatenate the Firebase library along with the other dependencies of our application in the dependencies.min.js file. Firebase data for the HDI Explorer application is shown in the following screenshot:

Configuring Firebase

We can connect to the Firebase application using the Firebase client. We will create a script element at the end of the firebase.md page that contains the synchronization code and create an instance of the Firebase reference to our data:

<script>
    // Connect to the Firebase application
    var dataref = new Firebase('https://hdi-explorer.firebaseio.com/'),

    // Application callbacks...
</script>

Integrating the application with Firebase

Before integrating Firebase, we will review the structure of the HDI Explorer application. We used Backbone to organize the components of the application and the REST API of the World Bank as the data source for our models.

Our application has three models, that is, the ApplicationModel, CountryTrend, and CountryInformation models. The application model manages the application state, which is defined by the three-letter code of the country selected by the user. The country trend model contains the time series of the aggregated Human Development Index, and the country information model contains information about the main components of the index: education, life expectancy, and average income.

The Countries collection contains instances of the CountryTrend model. This collection has two views, the search view and the chart of the HDI trends. The CountryInformation model has one associated view, the table of indicators at the right-hand side of the page. The HDI Explorer application is shown in the following screenshot:

Integrating the application with Firebase

All these elements are initialized in the app/setup.js file; instances are created and the callbacks for the change:code event in the application model are defined. The views will update themselves when the user selects a country in the search field. To synchronize the application state among the connected users, we need to synchronize the country code.

We can read the data from Firebase through asynchronous callbacks. These callbacks will be invoked in the same way for both the initial state and for the changes in the data. Events will be triggered if any object under the current location is changed, added, removed, or moved. The value event will be triggered for any of these modifications. We will use this event to update the application state as follows:

// Update the application state
dataref.on('value', function(snapshot) {
    app.state.set('code', snapshot.val().code);
});

When something changes under the current location (such as the country code), the value event will be triggered, and the callback will be called with a snapshot of the current data as the argument. The snapshot will contain the most up-to-date object, representing the state of our application. We can access the object by calling the val() method of the snapshot and get the value of the current country code.

If we modify the value of the code in Firebase, users connected to the same URL will have their application instances updated. We also want to synchronize the state from the application to Firebase. We will add an event listener to the application model in order to update the Firebase data:

// The model will update the object with the selected country code.
app.state.on('change:code', function(model) {
    dataref.set({code: model.get('code')});
});

The set method will update the contents of the Firebase location, triggering an update of all the views of the local application instance and any other client connected to the firebase page.

In this section, we learned how to add real-time interaction to the application created in Chapter 8, Data-driven Applications. We learned how to change the state of the client-side application by adding callbacks for the events triggered at the backend.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset