Using SignalR for .NET–> .NET scenarios

SignalR is an amazing library for creating real-time applications that are constantly being updated with new information. The focus has been on web applications but it turns out that the libraries do support .NET servers and clients. I found very few actual samples of such scenarios so I put one together. What will strike you first is how little code is required to get a real-time client-server notification system up and running. SignalR libraries do all the heavy lifting for you.

Note that these samples were tested with Visual Studio 2012 + Update 1.

From the SignalR site, here are the NuGet packages you need to pull into your self-hosted server app:

Install-Package Microsoft.Owin.Hosting –pre

Install-Package Microsoft.Owin.Host.HttpListener –pre

Install-Package Microsoft.AspNet.SignalR.Owin

And the server code is here:

 using System;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Hosting;
using Owin;
using Microsoft.AspNet.SignalR.Hubs;

namespace SignalRNotifier
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "https://localhost:8080";

            using (WebApplication.Start<Startup>(url))
            {
                Console.WriteLine("Server running on {0}", url);

                string c = Console.ReadLine();
                while (!string.IsNullOrEmpty(c))
                {
                    if (c == "S") // Send a message to all clients.
                    {
                        GlobalHost.ConnectionManager.GetHubContext<MyHub>().Clients.All.addMessage("Hello World");
                    }
                    c = Console.ReadLine();
                }
            }
        }
    }

    class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var config = new HubConfiguration { EnableCrossDomain = true };
            app.MapHubs(config);
        }
    }

    [HubName("TestHub")]
    public class MyHub : Hub
    {
    }
}
 For the client app, simply get the client package as follows:
 Install-Package Microsoft.AspNet.SignalR.Client 
 and the client code is extremely simply too:
 using Microsoft.AspNet.SignalR.Client.Hubs;
using System;

namespace SRClient
{
    class Program
    {
        static void Main(string[] args)
        {
            var hub = new HubConnection("https://localhost:8080");
            IHubProxy h = hub.CreateHubProxy("TestHub");
            
            hub.Start().ContinueWith(task => 
            {
                if (task.IsFaulted)
                    Console.WriteLine("Failed: {0}", task.Exception.GetBaseException());
                else
                {
                    Console.WriteLine("Connected with id {0}", hub.ConnectionId);
                    h.On("addMessage",  data => Console.WriteLine("Got the event from the server! [" + data + "]"));   
                }
            });
            Console.ReadLine();
        }
    }
}
 You can start multiple clients in different command windows and see all of them get updated near instantaneously.
 SignalR is great library for many purposes. Get to know it!