Skip to main content

Simplified signaling with SignalR


Introduction

Users are always interested in a rich experience, or a well UI Experience. They expect real-time action between both client and server, no matter if the technology used is HTML5 web sockets or something else. There comes the SignalR. It is framework built on top of ASP.NET and JavaScript library that helps to build rich interactive web applications.

We can use signalR in the scenarios where require polling server after fix interval to check if server has something which client needs to update. Well, In Definition SignalR is an asynchronous signaling framework that helps maintain persistent connections between client and server”. It uses the raw technique of keeping connections open between a web client and a web server. Actually it uses an existing technique called long polling. Behind the scenes, it could very well use Web Sockets transparently.

How it works

Long Polling: With long-polling, the browser sends a request to the server and the server keeps the request open for a set period.  If a notification is received within that period, a response containing the message is sent to the client. If a notification is not received within the set time period, the server sends a response to terminate the open request

SignalR: SignalR registers some code to run at application start time that finds all hubs in your application which is done by jquery.signalR.min.js and SignalR.dll. On the first request to signalr/hubs, the proxy is generated and cached for the lifetime of the application.



Server Hub:
[HubName("testHub")]
public class TestingHub :Hub
{
 /// <summary>
        /// This method is called from signalR client
        /// Clients is dynamic property.
        /// </summary>
        /// <param name="message"></param>
        public void Send(string message)
        {
            Clients.broadcastMessage(message);
        }

}

Client Side Scripting:
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.signalR.min.js" type="text/javascript"></script>
<script src="/signalr/hubs" type="text/javascript"></script>
   
    <script type="text/javascript">
        $(function () {

            var broadCaster = $.connection.testHub;
            broadCaster.broadcastMessage = function (message) {
                $('#brodcastArea').html(message);
            };


            $.connection.hub.start();
            $('#btnBroadCast').click(function () {
                broadCaster.send($('#txtMessage').val());
            });


        });


    </script>

Business problem with SignalR

Real time stock update system, we can create a very interactive stock updating tool, which reflects the live stock market change to the clients.  Our server will communicate to Stock API’s and updates all the open clients with the real time market data. 

History

The concept of SignalR that is long polling or server side scripting is not new in the world of Web. It is an existing technique which can be achieve by Socket.IO, node.js and Nowjs. We could say SignalR is related to this, but a new prospect that it is completely uses JavaScript on Client side and ASP.NET on server side.

Comments

Popular posts from this blog

pjax with MVC, and form post like pjax

Last weekend I was working on a interesting project, and came across an interesting JavaScript library called pjax. More information on pjax: https://github.com/defunkt/jquery-pjax/ http://pjax.heroku.com/ pjax loads HTML from your server into the current page without a full reload. It's ajax with real permalinks, page titles, and a working back button that fully degrades, It just enhances the browsing experience. I just loved the way we could use it with MVC; Below code sample explains how we could use the same with MVC. Clientside code would look as below: Since the Form post is not handled with pjax, we can do a work around as below. Updated _ViewStart.cshtml as below: @{ if (Request.Headers["X-PJAX"] != null || Request.Headers["AJAX-FORM"]!=null) { Layout = "~/Views/Shared/_PjaxLayout.cshtml"; } else { Layout = "~/Views/Shared/_Layout.cshtml"; } } _PjaxLayout.cshtml ...

Why not to have a static const in c#

This is just a thought, that I was thinking why can't we have a constant with static in C#, and the answer is 'NO'; That we cannot have a static constant; e.g: I created a class as below: public class Constants1 { public const string Const1 = "Hello"; public const string Const2 = "World"; public static string Static1 = "Hello Static"; } When we compile the program into IL, the C# compiler does a magic in IL, that the constants converts into static literals, of course it has to, that's why we are able to access the constants as Constants1.Const1

Localization in Umbraco, Item Page field localization

Umbraco is one of the most deployed Web Content Management Systems on the Microsoft stack. It's in the top five most popular server applications and among the ten most popular open source tools in general. You can directly download and start using it from http://www.umbraco.com If you have not used the application yet, its always worth trying it once. I am sure you will love it. Well, for the first time users, Umbraco provide step by step instruction for initial set up, once you are done you can use some sample template or create a blank website. To know more on umbraco, Please refer below links http://our.umbraco.org/wiki/how-tos/a-complete-newbie's-guide-to-umbraco http://umbraco.com/help-and-support/video-tutorials/getting-started?freeVideos=1 http://our.umbraco.org/wiki/how-tos/getting-started-with-umbraco-what-is-next-after-you-install http://our.umbraco.org/wiki/install-and-setup ,  I have created a blank website, Now we will go step by step. Fi...