Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create online signalR server

Tags:

c#

signalr

azure

I have built a simple chat application using Signalr and Windows Forms, however my app is self-hosted and only works on my localhost address, how can I upload server online on azure and connect my desktop client app to it? I have just started learning Signalr. I have done some research on Google but couldn't find anything that would answer my question.

like image 834
samatovS Avatar asked Dec 09 '15 23:12

samatovS


1 Answers

You only need to publish your signalR Hub as a website, it doesn't matter if you host your website in

  • Azure Web Apps
  • Azure Virtual machine
  • Azure Cloud Service

The most 'natural' way to host a SignalR Hub is an ASP.Net website, doesn't matter if the website is on azure or not. So you won't find any complex scenario there.

Your chat hub code could look like (code from signalR docs)

using System;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace SignalRChat
{
    public class ChatHub : Hub
    {
        public void Send(string name, string message)
        {
            // Call the broadcastMessage method to update clients.
            Clients.All.broadcastMessage(name, message);
        }
    }
}

The only thing you need to do is put the hub in a ASP.NET web Application, even an empty one. Off course you need to add the SignalR nuget package.

In the startup class (required by any OWIN application) you must to call app.MapSignalR as shown next:

using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(SignalRChat.Startup))]
namespace SignalRChat
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Any connection or hub wire up and configuration should go here
            app.MapSignalR();
        }
    }
}

That's all pretty much.

With the web App created you can host it wherever you want, I suggest you to host the app in an Azure Web App.

This is full/detailed tutorial about how to achieve the steps I told you above:

Tutorial: Getting Started with SignalR 2

like image 121
JuanK Avatar answered Sep 28 '22 18:09

JuanK