We know that SignalR is the Microsoft’s push solution in web
application. Prior to Owin the concept was like that. Introduction of Owin has changed
the scenario. Now SignalR can able to host with some other stand alone
application. It’s not needed to configure
SignalR hub within IIS server. We know
that OWIN(Open Web Interface for .NET) which has introduced by Microsoft is a specification
which sits between Web server and Web application.
The reason to introduce Owin is to reduce the dependency of
Web application on Web Server. For example,
if we implement some Owin specification (most popular is KATANA) in our
application then may be our application can able to run in any server irrespective
of IIS.
The Owin has given facility of self hosting which we can
adapt to host out SignalR hub application in single stand alone process. The
process may be one exe or Windows service or something else.
Why self hosting?
If you have concept of SignalR 1.0 and implemented in your
web application then you may ask the question. We have hosted SignalR in IIS
server along with web application, that’s easy and fine. Here are few advantage
of self hosting.
- If there is no environment to host application in IIs
- If we don’t want to pressure on IIS
So, those might be valid reason where self hosting may come
in picture. So, let’s implement example of self hosting and access SignalR hub
from other application.
Let’s take one console application and install below
packages from nugget package manager. The below package is to install SignalR
in self hosting environment.
Install-Package Microsoft.AspNet.SignalR.SelfHost
Now, as the hosting environment will be stand alone
application and client will be calling the application from some other domain,
we have to enable Cross Origin Resource Shairing(CORS) in application. Here is
the package for it.
Install-Package Microsoft.Owin.Cors
Once you install both the packages, you should see those
references in application.
Fine, we have setup the necessary references, now we will
define Owin startup class. This startup
class will be the entry point of application and we are enabling CORS in this
entry point. Here is sample code of the startup class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Owin;
using Microsoft.Owin.Cors;
using Microsoft.Owin.Hosting;
using Microsoft.Owin;
[assembly: OwinStartup(typeof(OWINApplication.Startup))]
namespace OWINApplication
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
}
}
}
The AllowAll property indicates
that we want to allow all domains to request to the SignalR hub. This is both pros and cons. If we allow all domains
then many applications from different domain can able to consume the SignalR
hub, this is the advantage and at the same time we have to implement strong
access policy and security for various domains.
Now, we will implement SignalR hub where we will define the
hub class and the function which will broadcast the message across client. Here
is simple implementation.
public class messageHub : Hub
{
public void sayHello()
{
Clients.All.sendMessage("Welcome to shelf hosting..");
}
}
It’s too simple, the sayHello() function will invoke
sendMessage() function which is defined in client end of SignalR subscriber.
Now , we have to host the application in some endpoint . Within Main() function we have defined the url
and arbitrary port where SignalR hub will host.
public class Program
{
public static void Main(string[] args)
{
string url = "http://localhost:1234";
using (WebApp.Start(url))
{
Console.WriteLine("Server running...");
Console.ReadLine();
}
Console.ReadLine();
}
}
Once we run the application, we will see below screen.
We have hosted SignalR server successfully .now we will
consume the service from client application. It’s not mandatory that the client
should be JavaScript client always, but it’s normal scenario where we consume
SignalR server from JavaScript client.
Here we have implemented one simple client which will
consume SignalR hub. Have a look on below code.
<script src="~/Scripts/jquery-1.6.4.min.js"></script>
<script src="~/Scripts/jquery.signalR-2.1.1.min.js"></script>
<script src="http://localhost:1234/signalr/hubs"></script>
<script>
$(function () {
$.connection.hub.url = "http://localhost:1234/signalr";
var appProxy = $.connection.messageHub;
appProxy.client.sendMessage = function (message) {
alert(message);
}
$.connection.hub.start().done(function () {
appProxy.server.sayHello();
});
});
</script>
And once we run the application, we will see below output.
The message has come from hub function which we have hosted in console
application.
Conclusion:-
In this example we have learned to host SignalR server in
self hosting application. Hope it has
given you a clear idea about self hosting of SignalR application.
hi,
ReplyDeletei applied same but when i host Console Application HUB on production server then if i access my web application it does not connect... do you have any solution.
how can i connect other computer with ip address
ReplyDelete