SignalR without IIS.

Note: In this article I assume you have small knowledge across SignalR and what dose it do.
Desktop based programs such (WinForm, WPF) were a starting point for me in my software journey as I have developed many application from small to enterprise using WPF.
But sometimes working with limited resources environment rises many difficulties.
Let me explain a brief on the situation.
Our application here works on many clients out of which none could connect to the internet and hold limited resources and power.
The objective is to connect all clients through the network with the ability of real time CRUD operations and chat between the users.
So that would be easy if we were on the web with a simple Asp.Net Core application which almost has SignalR backed into it.
But the software was a WPF program and connecting to the web was not an option.
So that needed three things to be solved:
1- Hosting the server on the master machine which should be fast and dose not require much power.
2- Running SignalR on that server.
3- Easy connection from the clients to server.
Hosting the server..
The key thing here is OWIN (Open Web Interface for .Net).
OWIN defines a standard interface between .NET web servers and web applications. The goal of the OWIN interface is to decouple server and application, encourage the development of simple modules for .NET web development, and, by being an open standard, stimulate the open source ecosystem of .NET web development tools.
This interface helps to decouple the need for IIS in the machine which means I can run it in a simple console app or to be completely hidden from user sight install it and run at a service!
And BOOM! there you go a running server on which you could mount any tech such as SignalR and you can preform http request across it!.
CODE..
yeah! now that we got some kind of idea on what we need to use we can dig into code and explain what is happening along the way.
The first step to run our localhost server, in here I will be creating a simple console app but you could run it on any kind.
After creating the project we need to add some Nuget Packages by running the following command on package manger console.
install-package Microsoft.Owin.Hosting
install-package Microsoft.Owin.Host.HttpListener
install-package Microsoft.Owin.Cors
This will install the needed dependencies for running the server.
Now lets go to our main entry point in program.cs and write the following.
static void Main(string[] args)
{
Console.WriteLine("Enter port to run server on : ");
var port = Console.ReadLine();
using (WebApp.Start($"http://localhost:{port}/"))
{
Console.WriteLine($"Hosting is running on port {port}\npress any key to stop");
Console.ReadLine();
}
}
Looks pretty simple just need to do one more step as OWIN requires a startup class so it can inject IAppBuilder on which we can configure routes and many more.
I have created a new class called OwinStartup.cs which holds the following
using Owin;
[assembly: Microsoft.Owin.OwinStartup(typeof(ConsoleApp1.OwinStartup))]
namespace ConsoleApp1
{
public class OwinStartup
{
public void Configuration(IAppBuilder appBuilder)
{
}
}
}
I need to give an assembly attribute tag to tell OWIN that hey!! use this startup.
And we must create void method named Configuration which OWIN expects.
Now we are good to go!
Running the application will now not result in much as we only have ran a server but it can not do anything right now.
So back to our main point SignalR, right now is the perfect moment to add it as everything is setup and good to go.
We need to add another package by running the command.
install-package Microsoft.AspNet.SignalR.SelfHost
Note: if you are working on a NetCore app then install
install-package Microsoft.AspNetCore.SignalR.SelfHost
And now in the configuration method lets write:
public void Configuration(IAppBuilder appBuilder)
{
//We need to allow CORS as all of our request now will be considered as cross origin
appBuilder.UseCors(CorsOptions.AllowAll);
appBuilder.Map("/signalr", options =>
{
options.RunSignalR();
});
}
This will auto pick up all the SignalR hubs in the project and map under the provided path.
The moment of truth testing our logic. :()
Lets create a hub, should look like the following.
public class TestHub : Hub
{
public override Task OnConnected()
{
Console.WriteLine($"A new user connected with id {Context.ConnectionId}");
return base.OnConnected();
}
public void SendDate()
{
Clients.All.ReadDate(DateTime.Now.ToLongDateString());
}
}
Now for the client, for me I will be creating another console app so we can prove the connection.
We need to install the needed packages so run the command
install-package Microsoft.AspNet.SignalR.Client
Now in our main method write the following.
static void Main(string[] args)
{
var hubConnection = new HubConnection("http://localhost:8080/");
var testHubProxy = hubConnection.CreateHubProxy("testhub");
try
{
hubConnection.Start().Wait();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
Note: the hub connection map will go by default into host/signalr
Now running both server and client will result in the following.

WOOW! we got the connection now to test our method SendDate()
try
{
testHubProxy.On("ReadDate", (date) =>
{
Console.WriteLine($"GotDate from server {date}");
});
hubConnection.Start().Wait();
testHubProxy.Invoke("SendDate");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Here we register a listener on the wanted method and invoke the hub to call it after running we should see the date from hub on our client console!.
This was a simple explanation on how to host your own small server for some real time communication, but far less fare to the technology as there are many cool things to do and configure that will take your application to next level.
Wish that you enjoyed the read!
No Comments