How To Install SignalR Within Episerver

In today's tutorial, I'm going to create how to install SignalR with Episerver. First, you need to install two Nuget packages, right-click on your solution, select 'Manage Nuget Packages for solution...', from your search type in SignalR episerver_signalr_tutorial_1 You will also need to install, 'Microsoft.Owin.Host.SystemWeb': episerver_signalr_tutorial_2 To make SignalR work you need to register it, in an OwinStartup class.

using EpiSampleSite.SampleLogic;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin;
using Owin;
using Microsoft.Owin.Host.SystemWeb;

[assembly: OwinStartup(typeof(EpiSampleSite.SampleLogic.Startup))]
namespace EpiSampleSite.SampleLogic
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR();
        }
    }
}
To register your Owin Startup class to load, you need to decorate it with the OwinStartup attribute, by registering the class, as seen above.  This code won't fire unless you make sure you have the following in your web.config, make sure you have the following app setting section otherwise your OwinStartup start-up class won't fire.
<add key="owin:AutomaticAppStartup" value="true" />
The next step you'll need to add a 'Hub' class.

using System;
using System.Web;
using Microsoft.AspNet.SignalR;

namespace EpiSampleSite.SampleLogic
{
    public class NotificationHub : Hub
    {
        public void YourMethod()
        {
               // Custom Code
        }
    }
}
This is the basic skeleton code and set-up that you need to get up and running with SignalR. The next step is to start adding custom logic into the hub and then using the SignalR Javascript library to register the client with the server so it can push messages to it and pull notifications from it
    <script src="/Scripts/jquery.signalR-2.1.0.js"></script>
    <script src="/signalr/hubs"></script>
For reference the following Javascript includes will be required in your HTML in order for SignalR to work.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top