/SignalR.Orleans

SignalR backend based on Orleans.

Primary LanguageC#MIT LicenseMIT

SignalR.Orleans

Sketch7 SignalR.Orleans

fork from OrleansContrib/SignalR.Orleans

Package build/publish NuGet version

Orleans is a framework that provides a straight-forward approach to building distributed high-scale computing applications, without the need to learn and apply complex concurrency or other scaling patterns.

ASP.NET Core SignalR is a new library for ASP.NET Core developers that makes it incredibly simple to add real-time web functionality to your applications. What is "real-time web" functionality? It's the ability to have your server-side code push content to the connected clients as it happens, in real-time.

Sketch7.SignalR.Orleans is a package that allow us to enhance the real-time capabilities of SignalR by leveraging Orleans distributed cloud platform capabilities.

Installation

Installation is performed via NuGet

dotnet support

Version .NET Status
1.x netcore2
2.x netcore3
3.x net5
4.x net6 CI
5.x net7 CI
6.x net8 CI

From Package Manager:

PS> Install-Package Sketch7.SignalR.Orleans PS> Install-Package Sketch7.SignalR.Orleans.AspNet

.Net CLI:

# dotnet add package Sketch7.SignalR.Orleans # dotnet add package Sketch7.SignalR.Orleans.AspNet

Paket:

# paket add Sketch7.SignalR.Orleans # paket add Sketch7.SignalR.Orleans.AspNet

Configuration

Silo

We need to configure the Orleans Silo with the below:

  • Use .UseSignalR() on ISiloHostBuilder.

Example

new HostBuilder()
  .UseOrleans((context, siloBuilder) =>
  {
    siloBuilder
      .AddMemoryGrainStorage("PubSubStore") // You can use any other storage provider as long as you have one registered as "PubSubStore".
      .UseSignalR();
  });

Configure Silo Storage Provider and Grain Persistance

Optional configuration to override the default implementation for both providers which by default are set as Memory.

Example

.UseSignalR(signalrBuilder => signalrBuilder.Configure((sb, opts) =>
{
  siloBuilder.AddMemoryGrainStorage(opts.StorageProvider);
}));

Client

Now your SignalR application needs to connect to the Orleans Cluster by using an Orleans Client:

  • Use .UseSignalR() on IClientBuilder.

Example

var hostBuilder = new HostBuilder()
  .UseOrleans((ctx, builder) =>
  {
    builder.UseSignalR()
  });

hostBuilder.RunConsoleAsync();

Somewhere in your Startup.cs:

  • Add the following in ConfigureServices.
  • Use .AddSignalR() on IServiceCollection (this is part of Microsoft.AspNetCore.SignalR nuget package).
  • Use AddOrleans() on .AddSignalR().

Example

public void ConfigureServices(IServiceCollection services)
{
  ...
  services
    .AddSignalR()
    .AddOrleans();
  ...
}

Great! Now you have SignalR configured and Orleans SignalR backplane built in Orleans!

Features

Hub Context

HubContext gives you the ability to communicate with the client from orleans grains (outside the hub).

Sample usage: Receiving server push notifications from message brokers, web hooks, etc. Ideally first update your grain state and then push signalr message to the client.

Example

public class UserNotificationGrain : Grain<UserNotificationState>, IUserNotificationGrain
{
  private HubContext<IUserNotificationHub> _hubContext;

  public override async Task OnActivateAsync()
  {
    _hubContext = GrainFactory.GetHub<IUserNotificationHub>();
    // some code...
    await _hubContext.User(this.GetPrimaryKeyString()).Send("Broadcast", State.UserNotification);
  }
}

Contributions

PRs and feedback are very welcome!