SignalR

What is SignalR?

  • SignalR provides the consistent or persistent connection between the server and the client.

  • It is used to develop real-time applications.

  • it bridges the gap between the server and the client so the information can flow in and out in real-time without any restriction or barrier.

  • it is bi-directional meaning server can send data to the client and vice versa.

  • Eg. Chat Applications, Social Applications.

How does SignalR works?

  • SignalR uses only one of the four components / technologies.

    Web sockets -> Event Source -> Forever Frame -> Long Polling

  • First it goes to Web Sockets if not available then Event Source and so on as it goes further towards the end the communication efficiency between server and client decreases.

Why use SignalR?

  • implementing SignalR means getting all the 4 components in a single package.

  • if any of the 4 components gets any updates, SignalR is gonna handle those and make the application work as it was expected to do.

  • SignalR is an abstraction means we don't have to go into details how it works.

  • it is cross-application compatible and easy to use.

  • it can be used to make broadcast groups.

  • scalable

AJAX

  • AJAX provides the server validation and it can also check for the new data.

  • It is not an open/persistent connection .

Long Polling

  • AJAX requests the server for the new data if it got any and keep on requesting.

  • While Long Polling will request the server once for any new data that the client request, the server will keep the request as soon as it gets the data it will respond.

  • Disadvantage

    • simulates the persistent connection

    • one way means server cannot act on its own behind there is always request from client

Forever Frame

  • it uses IFrame

  • server pushes the data to the client

  • Disadvantages

    • high memory usage

    • client should support IFrames

    • one-way persistent connection means from server to client.

Event Source

  • it uses content-type (text/event-stream)

  • one-way persistent connection means server to client

  • asynchronous

  • think of it as subscribe option when you subscribe to the channel you get the updates not the another way around

Web sockets

  • bi-directional persistent connection

  • multiuser

  • full -duplex - communication between server and client can happen at the same time

  • asynchronous

SignalR

  • inherits the Hub class

  • RPC (Remote Procedure Call)

    • server can call the functions at client side and vice versa

  • Hubs vs Persistent Connections

    • Hubs are at higher level than Persistent Connections

    • both of these are classes which can be inherited

    • Persistent Connection get instantized when connection is established and stays persistent until connection is closed. While Hubs are instantized for every new request.

Installing

StartUp.cs
app.mapSignalR();
ConfigureAuth(app);
_Layout.cshtml
<script src="~/Scripts/jquery.signalR-2.2.0.min.js></script>
<script src="~/signalr/js"></script> //this will be created 

@RenderSection("scripts", required: false)
SignalR.js
$.connection.hub.start()
    .done(function(){ 
        console.log("")
        $.connection.myHub.server.Announce("Connected");
    })
    .fail(function(){ alert() })
    
    
@.connection.myHub.client.Announce = function(message){alert(message)}
public class MyHub: Hub{
    public void Announce(string message){
        Clients.All.Announce(message);
    }
}   

Last updated