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

1

Step: 2

2

Step: 3

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

5

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)
        {
            app.MapSignalR();
        }
    }
}

Explanation:

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)
        {
            app.MapSignalR();
        }

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)
        {
            Clients.All.Hello(message);
        }
    }
}

Controller:

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

View:

@{
    ViewBag.Title = "Index";
}

Say Hello


JavaScript{ }

Output:16

Advantage:

  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

Disadvantage:

  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 🙂

Author:

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