What is SignalR?

Basically, SignalR is the abstraction of two words: Signal + R, where according to Google Signal means a gesture, action or sound that is used to convey information or instructions typically by prearrangement between the parties concerned and R stands for real-time.
So SignalR is a way to allow us to create real-time applications that require high-frequency updates from the server.
SignalR is an ASP.Net library developed by Microsoft that simplifies the developer works to add real-time web functionality into their application and enables the server to send the asynchronous data to the client-side applications.
There are many scenarios where we can use the SignalR like Stock Exchange, Gaming, Dashboard and Monitoring, Chat server application, etc.

SignalR Architecture

SignalR Architecture

In fig. 1.1 (1) Console Client that will receive the data from (2) SignalR and that SignalR will fetch the data from (3) database that is being continuously receiving updates from (4) 3rd party application.
Now Console client will try to connect with SignalR Hub and (5) will create the Hubproxy for that. Now SignalR server will (6) fetch the data from database and will (7) push the data to the connected client.
In the same way, we can connect different type of clients like Mobile client, web client, etc. and all client will receive the data once they will connect to the SignalR server.

What is Hub?

Basically, hub means a center of an effective activity or network. In SignalR hub provides the high-level communication between client and server whereas they can call a method on each other directly.

Step by step Stock-Exchange application with SignalR2

Part 1: Setting up Stock Exchange database

Create a Console application that will generate some random Stock market data and will store in an Excel sheet (here we are using excel sheet as a database)
– Open Visual Studio 2015 and select a console application and replace this script with Program.cs file

Part 2: Setting up SignalR server for fetching data from StockExchange Database

Step 1: Open Visual Studio 2015 > Create New Project > Select ASP.Net Web Application Empty Template

Step 2: To install Microsoft.AspNet.SignalR, Open Package manager console and type the following command:

Step 3: Add new Item > Add class file (StockExchange) and replace the default code with the following code:

Step 4: Add new Item > Add a SignalR Hub Class (v2)
Adding Hub Class

Step 5: Create a model class for UserConnection.

Step 6: Give a Hub name to your HubClass
HubNameStep 7: Override the Lifecycle events of Hub Class and replace with the following code snippet:

Step 8: Now we will fetch the data from database in every 5 second. Below is the code snippet for that:

Step 9: To Map the hub connection, we need to create a Startup class. So, for that
Right click on project >> Add Owin Startup class
Add/replace the default code with the following code snippet.

Part 3: Setting up SignalR .Net client to consume SignalR data

Step 1: Open Visual Studio 2015 > Create New Project > Select Console Application

Step 2: Add a StockExchange class and replace the code with following code snippet.

Step 3: To install Microsoft.AspNet.SignalR.Client, Open Package manager console and type the following command:

Step 4: Replace Program.cs class code with below code snippet:

Now run all the three applications.

I hope you understood the basics of SignalR.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

*