SignalR hosted on different server

I was working on a cordova phonegap application, where my SignalR app was hosted on different  IIS dedicated server.

Its easy when you have SignalR project hosted on the Same server where you have your website hosted (or within same project). In my case it was different.

if you are building a SignalR solution for your local project, then simply create a new MVC Web Application Project and Add SignalR package

Install-Package Microsoft.AspNet.SignalR

This will install all SignalR stuff on your project, only thing you need to do is create a Startup class

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR(
                new HubConfiguration
                {
                    EnableJSONP = true
                }
                );
        }
    }

And modify web.config (AppSettings)

    <add key="owin:AppStartup" value="Notifications.SignalR.Startup"/>

That's all..Create a Hub Class like Below

    public class SignatureHub: Hub
    {
        public void announeToAll(string message)
        {
            var context = GlobalHost.ConnectionManager.GetHubContext<SignatureHub>();
            context.Clients.All.addMessage(message);
        }
    }

Add the Reference of your MVC SignaR Project to your Web project (whereever you want to use it), Now you have Hub Proxy ready to use into your actual Web project.

<script src="/signalr/hubs"></script>

<script>
    $(function () {
      
        var connection = $.connection.signatureHub

        connection.client.addMessage = function (msg) {
            alert(msg)
        }

        $.connection.hub.start({ jsonp: true }, function () {
            connection.server.announeToAll("Allah Saves us");
            console.log(123)
        })



    })
</script>

in case of my Cordova app, All I had to add was this line of code:
   $.connection.hub.url = "http://<NETWORK_ADDRESS>/signalr"; // (Before any Code)
and change the refernce to HUB like this...
  <script src="http://NETWORK_ADDRESS/signalr/hubs"></script>

And that is all :-)


Posted by:

No comments posted.

Name :  

Email :  

Comment Below