Author : HASSAN MD TAREQ

Note: “SignalR” in this site refers to “ASP.NET Core SignalR” (not ASP.NET SignalR)

What is SignalR?

  • SignalR is an open source framework that wraps the complexity of real-time web transports
  • ASP.NET Core SignalR is an open-source library that simplifies adding real-time web functionality to apps
  • “real-time web” functionality?
    • ability to have your server-side code push content to the connected clients in real-time
    • client-server communication instantly
  • SignalR takes care of everything behind the scenes that makes real-time client-to-server and server-to-client communications possible
  • SignalR provides an API for creating server-to-client remote procedure calls (RPC). The RPCs call JavaScript functions on clients from server-side .NET Core code
  • SignalR provies server-side C# library and JavaScript client library
    • The SignalR server library is included in the ASP.NET Core 3.0 shared framework
    • The JavaScript client library isn’t automatically included in the project (Libman: Right Click > Add > Client Library > unkpg > @microsoft/signalr@latest)
  • Links:

Note: In ASP.NET Core, SignalR is a middleware that can be pluged into ASP.NET core app to build real-time apps

Features of SignalR

SignalR simplifies a rather complicated process. It takes care of a lot of the heavy lifting when it comes to real-time communication protocols on the web. Not all browsers support the same real-time communication protocols, and the best protocols to use may depend on a few different scenarios. SignalR takes care of those decisions for you and abstracts where the details of those communication protocols, so you don’t have to worry about implementing any of those details yourself. In a nutshell, SignalR provides a nice, easy to use interface on top of a lot of the complicated details involved in real-time communication on the web.

Features of SignalR for ASP.NET Core:

  • Handles connection management automatically
  • Sends messages to all connected clients simultaneously. For example, a chat room
  • Sends messages to specific clients or groups of clients
  • Scales to handle increasing traffic

Transports

  • A transport layer is needed for bi-directional real-time communication (Server <=> Client)
  • SignalR supports the following techniques for handling real-time communication:
    • WebSockets
    • Server-Sent Events
    • Long Polling

Transports negotiation: SignalR automatically chooses the best transport method that is within the capabilities of the server and client

SignalR implements these protocols on the client and server side and abstracts away the details, meaning you don’t have to worry about which transport is being used by your clients. SignalR also detects the browser’s ability to support each transport and picks the best one for each client.

See: WebSocket

Components

SignalR consists of Hubs (server-side) and Clients (client-side)

  • Hubs
    • Server-side component
    • Written in C#
    • Runs on web server
  • Clients
    • Browser-side component
    • Written in JavaScript
    • Runs on user’s browser

Clients can invoke methods on the hub, and the hub can invoke methods on the clients