Setup SingalR self-host in SSL

Hi Guys,

There has been a lot of buzz about singalR lately and If you havent checked it out yet, singalR is a fantastic realtime communication system, you should give it a go.

For friends who have played around with singalR and tried singalR self host and want to host in SSL, read on.

Setting up singalR in SSL is relatively very easy but not straight forward, I did not find it in documentation that this is the way to do it.
So here is my own.

Steps to create and host a signalR package in SSL

1)Get singalR self-host running, for help and quick start install nuget owin package

Install-Package Microsoft.Owin.Hosting -pre
Install-Package Microsoft.Owin.Host.HttpListener -pre
Install-Package Microsoft.AspNet.SignalR.Owin

Lets set up singalR on http://localhost:8082

static void Main(string[] args)
        {
            string url = "http://localhost:8082";
            using (WebApplication.Start<Startup>(url))
            {
                Console.WriteLine("Server started URL "+url);
                Console.ReadLine();
            }
        }

 

    class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // to turn on cross domain and other stuff
            var config = new HubConfiguration { EnableCrossDomain = true, EnableJavaScriptProxies = true, EnableDetailedErrors = true };
            app.MapHubs(config);
        }
    }

 

After successfully doing it you can navigate to http://localhost:8082/singalr/hubs and you will see your dynamically generated hubs file.

so at the moment we have it running on localhost on *non* SSL environment.

 

To run it in SSL, you need an SSL certificate. Two things to note here

1) SSL is port 443 or your ip address(localhost)

2) SSL needs a certificate (I am not going into detail about creating an SSL certificate but you can find it here)

But wait, we have hosted our singalR at socket address= localhost:8082, How is it going to map at port 443 then?

It wont. When you set up an SSL it will map to socket address = localhost:443 (because of point 1)

 

To make it work you need a SSL certificate (a self signed will do) for your localhost and a *unique* url to host singalR

so after creating a SSL certificate i have changed the above mentioned code to

static void Main(string[] args)
        {
            string url = "https://localhost/server1/";
            using (WebApplication.Start<Startup>(url))
            {
                Console.WriteLine("Server started URL "+url);
                Console.ReadLine();
            }
        }

now if you navigate to https://localhost/server1/singalr/hubs you will see a dynamically generated file.

What does this tell me?

It tells me that I have hosted my signalr on https://localhost:443/server1/

But I have not enabled any port sharing, how does this work?

IIS automatically enables port sharing, and so does singalR. You can host as many server on SSL on a singal ip address/domain name

Note: if you have a domain name for your localhost you can use that too for example, https://mydomainname.com/server1/singalr/hubs

You can point your localhost/127.0.0.1/loopback address to a domain name in your host file.

 

Simple and quick, Hopefully this will help someone start easy.


Posted

in

,

by

Tags: