Adding a hub with SignalR and Umbraco
Umbraco ships with signalR installed, find out how to add your own hub(s) to the existing setup
Umbraco ships with signalR installed. This article shows how to add your own hub(s) to the existing setup.
Create a hub and its interface
We are going to go for the most basic implementation possible, a status ping. So first create a new interface with the following code:
public interface ITestHubEvents
{
// Define the events the clients can listen to
public Task Pong();
}And then the actual hub:
using Microsoft.AspNetCore.SignalR;
public class TestHub : Hub<ITestHubEvents>
{
// when a client sends us a ping
public async Task Ping()
{
// we trigger the pong event on all clients
await Clients.All.Pong();
}
}Add the routing to the Umbraco Composer
The next step in the setup is registering our custom route:
Add the route in appsettings.json file
When setting up SignalR routes, add the route to ReservedPaths in the appsettings.json file like:
Test the setup
And lastly we can test the setup with some JavaScript in our view:
When you insert this in a view, you should see a signalR connection established console message followed by Pong.
Last updated