visit
A quick preview of how the outcome would look We will build a simple voting app that allows the conference attendees to vote their feedback about the conference in realtime as shown above! The attendees will use another application that consists of three simple buttons, one for each type of vote. Once, a user has voted, all the buttons are disabled so they do not vote repeatedly. However, this application is not fool proof as it doesn’t require user login and hence, a single user can cast multiple votes by repeatedly refreshing the page. Hence, this is just for a realtime demo :)
Ably Realtime is an excellent realtime messaging platform that makes it easy to add realtime functionality to our applications. It comes with both realtime and REST libraries to be used in accordance with the use case. For our app, we’ll use which lets us connect to the platform over WebSockets. For this, you’ll first need to create a new account (you can get one for free) , and obtain an APP URL from the dashboard.
Chart.js is library for implementing various types of charts is a library that let’s us to easily include beautiful graphs that represent either static or dynamically changing data. We’ll use Chart.js to show the votes casted by our users.
Angular Command Line Interface We build the voting app using Angular 4. Why? Because Angular is an a really good JavaScript framework for building frontend web apps. Moving forward, Angular has the potential to beautifully present the data obtained in realtime using a platform such as . If you don’t already have this installed on your system, follow simple steps from the .
Start by creating a new Angular app. I’ve named it ng-vote-ably, feel free modify this.
Next, move into the new project folder and serve this app locally on your browser to ensure that there were no problems in the initial setup.If everything so far works, we shall start with creating a new component that will hold our chart. I’ve named the component vote-chart, feel free to modify this.
Now within the index.html file inside src folder, add two scripts each for Ably and Chart.js, as shown below:
index.html Next, navigate to src->app->app.component.ts and modify it to contain the follows:
app.component.html Note that a new tag ‘app-vote-chart’ is also added which will hold the content from the new component that we created earlier. The ‘title’ variable is declared in src->app->app.component.ts. Modify it’s contents to ‘Vikings’ so that when interpolated in the HTML file, it would make some sense.
vote-chart.component.html Now in the component’s TS file, we’ll connect to Ably’s realtime library using:
this.ably = new Ably.Realtime(‘<YOUR-APP-URL>’);Attach to the channel on which votes will be published by the users as follows:
this.receiveChannel = this.ably.channels.get(‘vote-channel’);Next, subscribe to updates on this channel so that a callback function is triggered whenever any data (new votes) is published on this channel as follows:
this.receiveChannel.subscribe(“update”, function(message: any) {}
But before doing this, make sure to import chart.js and declare the Ably varible. Our goal is to update the graph every time a user publishes any votes on the channel called ‘vote-channel’, this is shown below. Your vote-chart.component.ts file should contain the following:
vote-chart.component.ts As seen above, we create a new chart and set the labels and the actual data which comes from the vote publishers. Further, each type of vote has a different background colour. For every vote published, we increment a simple counter and update the chart to reflect this new data.