Creating SignalR Services with ASP.Net Core

Creating SignalR Services with ASP.Net Core

SignalR Services Overview

ASP.NET SignalR is a library for ASP.NET developers to add real-time web functionality to their applications. Real-time web functionality is the ability to have server-side code push content to the connected clients as it happens SignalR takes advantage of several transports, automatically selecting the best available transport for the given the clients and server. SignalR takes advantage of WebSocket, an HTML5 API that enables bi-directional communication between the browser and server. SignalR will use WebSockets under the covers when it's available, and gracefully fall back to other techniques and technologies when it isn't, while the application code remains the same. SignalR also provides a simple, high-level API for doing server-to-client RPC and clients to server (call JavaScript functions in a client's browser from server-side .NET code) in an ASP.NET application, as well as adding useful hooks for connection management, such as connect/disconnect events, grouping connections, authorization.

SignalR Uses

SignalR serves as real-time web functionality to your application without refreshing web page or application. Based on the best available transport layer like WebSockets, Service side events and long pooling Service will choose best transport layer and sends the messages accordingly. Chat application is an example of SignalR where user doesn’t refresh the page but receives the messages accordingly

Connections and Hubs

The SignalR API contains two models for communicating between clients and servers: Persistent Connections and Hubs.

PersistentConnection

A Connection represents a simple endpoint for sending single-recipient, grouped, or broadcast messages. The Persistent Connection API (represented in .NET code by the PersistentConnection class) gives the developer direct access to the low-level communication the protocol that SignalR exposes [1].

Hub

A Hub is a more high-level pipeline built upon the Connection API that allows your clients and server to call methods on each other directly. SignalR handles the dispatching across machine boundaries as if by magic, allowing clients to call methods on the server as easily as local methods, and vice versa. By Using the Hubs communication model, it will be familiar to developers who have used remote invocation APIs such as .NET Remoting. Using a Hub also allows you to pass strongly typed parameters to methods, enabling model binding. [1]

Requirements

Visual studio 2017 .net framework 46

Here we will create chat application for understanding SignalR Services.

Steps for creating SignalR Services

1.Create .Net Core Web Application project

The following screenshot [screenshot1 & screenshot2] will demonstrate the steps for creating a project

image.png Screenshot 1

image.png

Screenshot 2

Click ok to create project.

2.Install Packages of SignalR

Install following Nuget packages Microsoft.AspNet.SignalR Microsoft.AspNet.SignalR.Core Right click on the project dependencies -> Manage Nuget packages and add the above mention packages.

3.After creating the project we need to create ChatHub

Right click on project add new item -> select class template and name it as ChatHub.

ChatHub

In the ChatHub we do have following methods

  • OnConnectedAsync : In this method, I have created functionality to notify all the connected clients when new client is connected and it has event method to subscribe the alert, when New client is connected.

  • OnDisconnectedAsync: In this method, I have created functionality to notify all the connected clients when client is disconnected and it has event method to subscribe the alert, when client get disconnected.

  • JoinGroup: When clients wants to send a message intended to specific group, then client need to call this method passing the groupName. Client will register in that group. Send : server will broad cast message to all the connected clients

  • Send : server will broad cast message to all the connected clients

  • SendPrivateMessage : with this method, sends message to all the connected clients for the specified group.

The above mention method as shown in screenshot3

image.png

Screenshot 3

Enable Cors

In order to work with SignalR on multi domain we need to EnableCors on server. By Default Browser security does not allow web pages to make AJAX requests to another domain. This prevention is called "same-origin policy".

To set up CORS is at least a 2 step process:

  • You register CORS functionality
  • You configure CORS options

Register and Define a Policy To do this start with registering CORS functionality in ConfigureServices() of Startup.cs. as shown in screenshot4

image.png Screenshot 4

Apply the Policy

Once the policy has been defined it can be applied.

You can apply the policy globally to every request in the application by call app.useCors() in the Configure()method of Startup. As shown screenshot5

image.png Screenshot 5

Configure SignalR

SignalR server must be configured to pass SignalR requests to SignalR as shown in screenshot 6

image.png

Screenshot 6

References

  1. docs.microsoft.com/en-us/aspnet/signalr/ove..