Categories
Sitecore

How I have used SignalR on Sitecore 8

On an existing Sitecore 8 project, I had to use SignalR for a page to facilitate real-time update of the information for all the visitors/users that were interacting with that page.

Indeed I turned to Sitecore SignalR module and used as resources other blogs/websites that wrote about the subject:

Everything looked great, I’ve built my page with my entire functionality, getting to the point where I needed the username of the visitor of my page/site. Since my website is protected with ADFS Authentication, I had to protect the SignalR hub and to pass authentication information to clients. Then I’ve used this, from the official documentation, and I’ve changed the code from InitializeSignalR.cs and before the line

args.App.MapSignalR(Settings.SignalR.Path, new HubConfiguration());

I’ve added

GlobalHost.HubPipeline.RequireAuthentication();

And as for the username, I’ve saved it into a cookie on the page load of the component that has the SignalR functionality, and then I’m using it into the JS functionality of my SignalR functionality.

Then, since our implementation will be residing on a couple of servers that were behind a LoadBalancer, and it was needed that the messages distributed by SignalR to get to all clients so SignalR Scaleout with SQL Server was needed. I’ve used the documentation from here, as follows:

  1. Created a new empty database. The backplane will create the necessary tables in this database.
  2. Added an extra NuGet package to your application:
  3. Added the following code to InitializeSignalR.cs to configure the backplane:
    • var connection = System.Configuration.ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString;
      GlobalHost.DependencyResolver.UseSqlServer(connection);
      
  4. Then Enable Service Broker and give proper permissions to the database for the user used in the connection string as it explains further in the documentation.

I’ve added the entire file InitializeSignalR.cs into a Gist


using Owin;
namespace Sitecore.SignalR.Pipelines.InitializeOwinMiddleware
{
using Microsoft.AspNet.SignalR;
using Sitecore.Diagnostics;
using Sitecore.Owin.Pipelines.Initialize;
using Sitecore.SignalR.Configuration;
public class InitializeSignalR : InitializeProcessor
{
public override void Process([NotNull] InitializeArgs args)
{
Assert.ArgumentNotNull(args, nameof(args));
var connection = System.Configuration.ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString;
GlobalHost.HubPipeline.RequireAuthentication();
GlobalHost.DependencyResolver.UseSqlServer(connection);
args.App.MapSignalR(Settings.SignalR.Path, new HubConfiguration());
}
}
}

Recompile your code, and you should be all set.

Advertisement

By Sebastian Tecsi

Sitecore MVP 2018-2021
Sitecore Architect

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.