Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web

Quick Look into SignalR

5.00/5 (4 votes)
19 Jul 2020CPOL3 min read 11.4K  
See with a working demo how SignalR works and how it can be used
Last year, I was looking into various ways of communication between client and server for one of my projects. Evaluated SignalR too as one of the options. I found decent documentation online to put across a test app to see how it works.

Introduction

SignalR is a free and open-source software library for Microsoft ASP.NET that provides the ability to server-side code to push content to the connected clients in real-time.

Quote:

Pushing data from the server to the client (not just browser clients) has always been a tough problem. SignalR makes it dead easy and handles all the heavy lifting for you.

https://github.com/SignalR/SignalR

Detailed documentation can be found here.

Most of the shareout online considers it as ASP.NET/Web application solution only which is not true. As mentioned in the quote above, the client could be a non-browser too like a desktop application.

I gathered that behind the scenes, SignalR primarily tries to use Web Socket protocol to send data. WebSocket is a new HTML5 API (refer to my detailed article on it) that enables bi-directional communication between the client and server. In case of any compatibility gap, SignalR uses other protocols like long polling.

P.S.: Since most of the code to make the test app was picked from the web, all the credit goes to them. One specific source that I can recall is Tutorial: SignalR Self-Host.

Now, a Quick Peek Into the Signalr Test App

  • Using the SignalR Hub class to setup the server. Defined a hub that exposes a method like an end point for clients to send a message. Server can process the message and send back a response to all or few of the clients.
    C#
    [HubName("TestHub")]
    public class TestHub : Hub
    {
        public void ProcessMessageFromClient(string message)
        {
            Console.WriteLine($"<Client sent:> {message}");
    
            // Do some processing with the client request and send the response back
            string newMessage = $"<Service sent>: 
                   Client message back in upper case: {message.ToUpper()}";
            Clients.All.ResponseToClientMessage(newMessage);
        }
    }
  • Specify which “origins” we want to allow our application to accept. It is setup via CORS configuration. CORS is a security concept that allows end points from different domains to interact with each other.
    C#
    public void Configuration(IAppBuilder app)
    {
         app.UseCors(CorsOptions.AllowAll);
         app.MapSignalR();
    }
  • Server is setup using OWIN (Open Web Interface for .NET). OWIN defines an abstraction between .NET web servers and web applications. This helps in self-hosting a web application in a process, outside of IIS.
    C#
    static void Main(string[] args)
    {
        string url = @"http://localhost:8080/";
    
        using (WebApp.Start<Startup>(url))
        {
            Console.WriteLine($"============ SERVER ============");
            Console.WriteLine($"Server running at {url}");
            Console.WriteLine("Wait for clients message requests for server to respond OR");
            Console.WriteLine("Type any message - it will be broadcast to all clients.");
            Console.WriteLine($"================================");
    
            // For server broadcast test
            // Get hub context 
            IHubContext ctx = GlobalHost.ConnectionManager.GetHubContext<TestHub>();
    
            string line = null;
            while ((line = Console.ReadLine()) != null)
            {
                string newMessage = $"<Server sent:> {line}";
                ctx.Clients.All.MessageFromServer(newMessage);
            }
    
            // pause to allow clients to receive
            Console.ReadLine();
        }
    }

    In the above code, using IHubContext:

    1. Server is setup to have earlier defined Hub as one of the broadcast end point.
    2. Server is also setup to broadcast any message to all clients by itself if need be.
  • Client is setup to communicate with the server (send and receive message) via hub using HubConnection & IHubProxy. Client can invoke the exposed end point in the hub to send a message.
    C#
    static void Main(string[] args)
    {
        string url = @"http://localhost:8080/";
    
        var connection = new HubConnection(url);
        IHubProxy _hub = connection.CreateHubProxy("TestHub");
        connection.Start().Wait();
    
        // For server side initiation of messages
        _hub.On("MessageFromServer", x => Console.WriteLine(x));
        _hub.On("ResponseToClientMessage", x => Console.WriteLine(x));
    
        Console.WriteLine($"============ CLIENT ============");
        Console.WriteLine
          ("Type any message - it will be sent as a request to server for a response.");
        Console.WriteLine($"================================");
    
        string line = null;
        while ((line = Console.ReadLine()) != null)
        {
            // Send message to Server
            _hub.Invoke("ProcessMessageFromClient", line).Wait();
        }
    
        Console.Read();
    }

With the above setup, we can see the communication between Client & Server realtime like below:

SignalR test

Things to Consider While Opting for SignalR

SignalR looks awesome. But, there are a couple of things one should know while using it:

  • It’s a connected technology – each client connected through SignalR is using a persistent and dedicated connection on the Web Server. SignalR is shared as scalable but it never harms to look for any queries around connections, memory, CPU, etc. with higher number of clients.
  • One would need to setup a valid host server with a PORT open on it that can be used for communication. This could depend on an organisation security protocols and approvals.

Hope this gives a quick overview about SignalR. Keep learning!

Download the entire code for lookup from https://github.com/samewara/SignalRTest.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)