SHASHANGKA SHEKHAR'S BLOG

Hi, I am Shashangka Shekhar, Working with Microsoft Technologies. This is my personal blog where I share some of my experiences.

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!

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

Explanation:

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

Add the following code to the Startup.Configuration method:

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.

Controller:

View:

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 🙂

Post a comment