Streaming Updates to a Realtime Progress Bar with JavaScript

This tutorial is a quick and easy example of Realtime Updates using PubNub to stream status updates and display the progress in a realtime progress bar.

There are more than a fair share of tutorials out there that show you how to build a progress bar in JavaScript. But what good is a static progress bar?

In this tutorial, we’ll show you how to connect your progress bar to the internet and stream updates to it in realtime. This could be inventory from an IoT sensor, capacity in a chatroom, or uploading files – an infinite amount of other use cases.


You can find the source code for our realtime JavaScript progress bar here.


This Node.js script replicates the effect of the file upload. Ideally, we will get the percentage uploaded as bytes uploaded divided by total byte size of the file. Once we have the percentage, we are sending that data to ‘progress-bar’ channel every 5 sec to give an effect of file uploading taking time.

var pubnub = require("pubnub")({
   subscribe_key: 'demo', // always required
   publish_key: 'demo' // only required if publishing
var percentages = [0,20,40,60,80,100]

function send_percentage(percentage){
       channel: "progress-bar",
       message: {"value": percentage},
       callback: function(m){console.log(m)}

var interval_id = setInterval(function(){
}, 5000)


This is a simple HTML template with couple of CDNs (jQuery and PubNub) and placeholder for circular progress bar.

<!DOCTYPE html>
<html >
   <meta charset="UTF-8">
   <title>Circular Progress Bar</title>
   <link rel="stylesheet" href="css/normalize.css">
     <link rel="stylesheet" href="css/style.css">
<div id="page">
 <div class="progress-bar">
     <canvas id="inactiveProgress" class="progress-inactive" height="275px" width="275px"></canvas>
   <canvas id="activeProgress" class="progress-active"  height="275px" width="275px"></canvas>
   <script src=''></script>
   <script src=""></script>
   <script src="js/index.js"></script>


Most of this JavaScript is responsible for animation effect of circular progress bar. But apart from that, we are just subscribing to that particular PubNub channel, and passing the received percentage to drawProgress method.

            var pubnub = PUBNUB({
                subscribe_key : 'demo'

            var $pCaption = $('.progress-bar p');
            var iProgress = document.getElementById('inactiveProgress');
            var aProgress = document.getElementById('activeProgress');
            var iProgressCTX = iProgress.getContext('2d');


                channel: "progress-bar",
                message: function(message){
                    var percentage = message.value / 100;
                    console.log(percentage + '%');
                    drawProgress(aProgress, percentage, $pCaption);

            function drawInactive(iProgressCTX){
                iProgressCTX.lineCap = 'square';

                //outer ring
                iProgressCTX.lineWidth = 15;
                iProgressCTX.strokeStyle = '#e1e1e1';

                //progress bar
                iProgressCTX.lineWidth = 0;
                iProgressCTX.fillStyle = '#e6e6e6';

                //progressbar caption
                iProgressCTX.lineWidth = 0;
                iProgressCTX.fillStyle = '#fff';

            function drawProgress(bar, percentage, $pCaption){
                var barCTX = bar.getContext("2d");
                var quarterTurn = Math.PI / 2;
                var endingAngle = ((2*percentage) * Math.PI) - quarterTurn;
                var startingAngle = 0 - quarterTurn;

                bar.width = bar.width;
                barCTX.lineCap = 'square';

                barCTX.lineWidth = 20;
                barCTX.strokeStyle = '#76e1e5';
                barCTX.arc(137.5,137.5,111,startingAngle, endingAngle);

                $pCaption.text( (parseInt(percentage * 100, 10)) + '%');

And that’s it! This is just scratching the surface of realtime updates using PubNub. Check out some of our other use cases, and as always, you can contact us with any questions you have.

Try PubNub Today

Connect up to 100 devices for Free