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:
- http://sitecoremadness.blogspot.com/2015/06/using-signalr-with-sitecore.html
- https://visionsincode.wordpress.com/category/real-time/
- https://community.sitecore.net/technical_blogs/b/exploring_sitecore/posts/signalr-and-sitecore
- https://medium.com/@jackspektor/short-guide-to-how-to-setup-signalr-in-sitecore-solution-cee348e0c991
- https://www.asp.net/signalr
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:
- Created a new empty database. The backplane will create the necessary tables in this database.
- Added an extra NuGet package to your application:
- Added the following code to InitializeSignalR.cs to configure the backplane:
-
var connection = System.Configuration.ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString; GlobalHost.DependencyResolver.UseSqlServer(connection);
-
- 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.