Oct 20, 2012

Real Time Chart With SignalR and ASP.NET MVC

In my recent post, we have implemented Real-Time Chart using HTML5 Push Notification (SSE) and ASP.NET Web API. I got many requests to implement it using SignalR. So In this tutorial, we are going to display real-time updates with SignalR and ASP.NET MVC. SignalR is an async signaling library for ASP.NET to help build real-time, multi-user interactive web applications. It makes easy to push data from the server to the client.

 Real Time Chart using HTML5 Push Notification (SSE) and ASP.NET Web API

1. Create ASP.NET MVC 4 Project.
2. To install SignalR, run the following command in the Package Manager Console
install-package signalr

3. Download Flot library and Add Flot library scripts (jquery.flot.min.js, excanvas.min.js) in scripts folder


4. Add a new folder “Hubs” in the project and add “ChartDataHub.cs” and “ChartData.cs” classes.
in ChartData.cs:

 public class ChartData
        private readonly static Lazy<ChartData> _instance = new Lazy<ChartData>(() => new ChartData());
        private readonly ConcurrentQueue<int> _points = new ConcurrentQueue<int>();    
        private readonly int _updateInterval = 250; //ms        
        private Timer _timer;
        private readonly object _updatePointsLock = new object();
        private bool _updatingData = false;
        private readonly Random _updateOrNotRandom = new Random();
        private int _startPoint = 50, _minPoint = 25, _maxPoint = 99;

        private ChartData()

        public static ChartData Instance
                return _instance.Value;

        /// <summary>
        /// To initialize timer and data
        /// </summary>
        /// <returns></returns>
        public IEnumerable<int> initData()
            _timer = new Timer(TimerCallBack, null, _updateInterval, _updateInterval);
            return _points.ToArray();

       /// <summary>
       /// Timer callback method
       /// </summary>
       /// <param name="state"></param>
        private void TimerCallBack(object state)
            // This function must be re-entrant as it's running as a timer interval handler
            if (_updatingData)
            lock (_updatePointsLock)
                if (!_updatingData)
                    _updatingData = true;

                    // Randomly choose whether to udpate this point or not           
                    if (_updateOrNotRandom.Next(0, 3) == 1)
                    _updatingData = false;

        /// <summary>
        /// To update data (Generate random point in our case)
        /// </summary>
        /// <returns></returns>
        private int UpdateData()
            int point = _startPoint;
            if (_points.TryDequeue(out point))
                // Update the point price by a random factor of the range percent
                var random = new Random();               
                var pos = random.NextDouble() > .51;
                var change = random.Next((int)point/2);
                change = pos ? change : -change;
                point += change;
                if (point < _minPoint) {
                    point = _minPoint;
                if (point > _maxPoint) {
                    point = _maxPoint; 
            return point;

        private void BroadcastChartData(int point)

        private static dynamic GetClients()
            return GlobalHost.ConnectionManager.GetHubContext<ChartDataHub>().Clients;

initData: initialize data and timer
UpdateData: to get the latest data(Generate random point in our case)
BroadcastChartData: to broadcast the point

In previous article, we were changing time-interval of timer in timercallback method to get random data at random time interval. Here we are randomly perform the operation to produce same behavior instead of changing time-interval.

 if (_updateOrNotRandom.Next(0, 3) == 1){
 ...do operation...

5. in ChartDataHub.cs:

    public class ChartDataHub : Hub
        private readonly ChartData _pointer;

        public ChartDataHub() : this(ChartData.Instance) { }

        public ChartDataHub(ChartData pointer)
            _pointer = pointer;

        public IEnumerable<int> initData()
            return _pointer.initData();

It’s the interface for SignalR. HubName Attribute is the ‘client side’ name of the ‘Hub’.


5. Create new action in Home controller, add view without layout. in the view, add jQuery,SignalR and Flot libraries files:

    <!--[if lte IE 8]><script language="javascript" type="text/javascript" src="~/Scripts/excanvas.min.js"></script><![endif]-->
    <script src="~/Scripts/jquery.flot.min.js" type="text/javascript"></script>
    <script src="~/Scripts/jquery.signalR-0.5.3.min.js" type="text/javascript"></script>
    <script src="/signalr/hubs" type="text/javascript"></script>

Here is the HTML Structure:

<div id="body">
        <div style="text-align: center">
                Trade Price: <span id="priceHolder"></span>
            <div id="placeholder" style="width: 600px; height: 300px; margin: 0 auto">
            <a href="https://techbrij.com" style="font-style: italic; font-size: medium">techbrij.com</a>

To create proxy and start SignalR operations, the following javascript code is used:

         //Create the proxy
        var chartData = $.connection.chartData;

        function init() {
            return chartData.initData();

        chartData.updateData = function (data) {

        // Start the connection
		function () {

chartData.updateData is called on getting new data. We’ll implement it to update chart.


 var ypt = [], totalPoints = 30;

        function initData() {
            for (var i = 0; i < totalPoints; ++i)
            return getPoints();

        function getData(data) {
            if (ypt.length > 0)
                ypt = ypt.slice(1);
            return getPoints();
        function getPoints() {
            var ret = [];
            for (var i = 0; i < ypt.length; ++i)
                ret.push([i, ypt[i]]);
            return ret;

        // setup plot
        var options = {
            series: { shadowSize: 0, bars: {
                show: true,
                barWidth: 0.75,
                fill: 1
            }, // drawing is faster without shadows
            yaxis: { min: 0, max: 100,
                tickFormatter: function (val, axis) {
                    return '$' + val;
            xaxis: { show: false }

        var plot = $.plot($("#placeholder"), [initData()], options);
        function update(data) {
            $('#priceHolder').text('$' + data);

We are using same code as used in previous article to draw chart.

You can download source code from Github.

Hope, you enjoy it. Share your opinion or suggestion how you are displaying real-time updates.


  1. This works nice with signalR 2.0 using following changes:

    1. change “GetClients().updateData(point);” to “GetClients().All.updateData(point);”

    2. change “return chartData.initData();” to “return chartData.server.initData();”

    3. change “chartData.updateData = function (data) {” to “chartData.client.updateData = function (data) {“

  2. I tried this example, this code does not work with new SignalR Microsoft ASP.NET SignalR 1.0.1. When I run this project it fails on “initData” method. When I add $.connection.hub.logging = true; code to log, it logs these messages :

    [15:06:38 GMT-0400 (Eastern Daylight Time)] SignalR: Websocket opened jquery.signalR-1.0.1.min.js:10
    [15:06:38 GMT-0400 (Eastern Daylight Time)] SignalR: Now monitoring keep alive with a warning timeout of 13333.333333333332 and a connection lost timeout of 20000 jquery.signalR-1.0.1.min.js:10

    and then code stops on “return chartData.initData();” , saying “chartData Object # has no method ‘initData’ ” and I cannot see what is causing this.

    I would be great to see this blog example done with new Microsoft ASP.NET SignalR 1.0.1.

Leave a Reply

Your email address will not be published. Required fields are marked *