ASP.NET SignalR Basic

Today we are going to learn about SignalR with a very basic sample Asp.Net MVC project.

dicon Download source code 7.27 MB

What is SignalR?

According to Wikipedia: ASP.NET SignalR is a library for ASP.NET developers to add real-time web functionality to their applications.

SignalR is a framework for building asynchronous applications. “R” stand for “Real Time”. SignalR is a realtime communication framework built on top of the WebSoket specification.

It does fallback to classic HTTP when websockets are not supported by client or server. SignalR is stateless by design and it’s not aware of ASP.NET user session.

MVC SignalR Project:

To create the sample applications, we need to have Visual Studio 2012 or later installed and be able to run the server on a platform that supports .NET 4.5.

Step: 1


Step: 2


Step: 3


Getting Started with SignalR:

The first thing is NuGet, which is used to get the needed SignalR assemblies into our solution. Next, we focus on creating OwinStartup class.

Get it on NuGet!

Install-Package Microsoft.AspNet.SignalR


Register SignalR middle ware:

The following code to define the SignalR Hubs route using an OWIN startup class. Now we will create OwinStartup Class:7

using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(WebAppSignalR.Startup))]

namespace WebAppSignalR
    public class Startup
        public void Configuration(IAppBuilder app)


The following attribute will set the startup class to the TestStartup class in the StartupDemo namespace.

[assembly: OwinStartup(typeof(WebAppSignalR.Startup))]

Add the following code to the Startup.Configuration method:

public void Configuration(IAppBuilder app)

This code adds a simple piece of middleware to the OWIN pipeline, implemented as a function that receives a Microsoft.Owin.IOwinContext instance. When the server receives an HTTP request, the OWIN pipeline invokes the middleware. The middleware sets the content type for the response and writes the response body.

Create and use Hub classes:

After this we will focus on creating a Hub, which we discuss about earlier.11

The following class is a simple Hub class in this application. Which is  derives from Microsoft.Aspnet.Signalr.Hub.

namespace WebAppSignalR.Hubs
    public class MessageHub : Hub
        public void SayHello(String message)


namespace WebAppSignalR.Controllers
    public class HomeController : Controller
        // GET: Home
        public ActionResult Index()
            return View();


    ViewBag.Title = "Index";

<h2>Say Hello</h2>

<div id="messages"></div>
<hr />
<input type="text" id="myMessage" value="" />
<button type="button" id="submit">Send!</button>

@section JavaScript{
    <script src="~/scripts/jquery-1.10.2.min.js"></script>
    <script src="~/scripts/jquery.signalR-2.2.0.min.js"></script>
    <script src="~/signalr/hubs"></script>
    <script type="text/javascript">
        $(function () {
            var hub = $.connection.messageHub;

            //client method which can be called from the server
            hub.client.hello = function (message) {
                $("#messages").append("<p>" + message + "</p>");

            // Wait for the connection
            $.connection.hub.start().done(function () {

                //click event handler to send the message
                $("#submit").click(function () {
                    var message = $("#myMessage").val();

                    //invoke method

                    //reset field



  1. It’s keeping a persistent connection from browser to the server
  2. Doesn’t have a dependency from ASP.NET and IIS hosting environment


  1. WebSockets it’s not universally supported across all browser and server.
  2. SignalR is stateless, it’s not aware of ASP.NET user session.

Hope this will help to startup of SignalR 🙂


Since March 2011, have 8+ years of professional experience on software development, currently working as Senior Software Engineer at s3 Innovate Pte Ltd.

Leave a Reply

Your email address will not be published.