Orchardcore signalr

From CrestApps/CrestApps.AgentSkills · View interactive version

Orchardcore signalr is an AI agent skill from the GitHub repository CrestApps/CrestApps.AgentSkills. Skill for SignalR in CrestApps Orchard Core 3.0, where hosting moved to the Orchard Core framework. Covers migrating from the deprecated CrestApps.OrchardCore.SignalR, CrestApps.OrchardCore.SignalR.Redis and CrestApps.OrchardCore.SignalR.Azure compatibility features to OrchardCore.SignalR, OrchardCo

How to use this skill

Skill file

Orchard Core SignalR - Prompt Templates

Build Tenant-Aware SignalR Features

You are an Orchard Core expert. Generate accurate hub, route, resource, and client code for SignalR in CrestApps Orchard Core 3.0. SignalR hosting now lives in the Orchard Core framework (OrchardCore.SignalR); CrestApps contributes only routing and tenant-isolation helpers that every CrestApps hub follows.

Guidelines

Feature Overview

| Item | Value |

|---|---|

| Hosting feature | OrchardCore.SignalR (package OrchardCore.SignalR) |

| Redis backplane | OrchardCore.SignalR.Redis (depends on OrchardCore.Redis) |

| Azure backplane | OrchardCore.SignalR.Azure (Azure SignalR Service) |

| Deprecated compatibility features | CrestApps.OrchardCore.SignalR, CrestApps.OrchardCore.SignalR.Redis, CrestApps.OrchardCore.SignalR.Azure |

| Route and URL helpers | SignalRHubRoutes, SignalRHtmlHelperExtensions in CrestApps.OrchardCore.Core (namespace CrestApps.OrchardCore) |

| Tenant destinations | TenantSignalRGroupName, HubConnectionWork in CrestApps.OrchardCore.SignalR.Core |

| Hub path convention | /Communication/Hub/{HubTypeName} |

| Script resource | signalr (registered by OrchardCore.SignalR, @microsoft/signalr 10.0.0) |

| JSON protocol | camel case, with Orchard JOptions.KnownConverters |

Enable SignalR

{
  "steps": [
    {
      "name": "Feature",
      "enable": [
        "OrchardCore.SignalR"
      ],
      "disable": []
    }
  ]
}

Define and Map a Hub

Reference CrestApps.OrchardCore.Core for the routing helpers. Install third-party module packages in the web/startup project.

using CrestApps.OrchardCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.SignalR;
using OrchardCore.Modules;

namespace MyCompany.OrchardCore.Live;

public sealed class NotificationsHub : Hub
{
    public Task PingAsync()
    {
        return Clients.Caller.SendAsync("pong");
    }
}

public sealed class Startup : StartupBase
{
    public override void Configure(
        IApplicationBuilder app,
        IEndpointRouteBuilder routes,
        IServiceProvider serviceProvider)
    {
        routes.MapHub<NotificationsHub>(SignalRHubRoutes.GetHubPath<NotificationsHub>());
    }
}

Declare OrchardCore.SignalR as a dependency of the feature that maps the hub so the SignalR services are present.

Configure a specific hub only when it needs non-default timing:

services.Configure<HubOptions<NotificationsHub>>(options =>
{
    options.ClientTimeoutInterval = TimeSpan.FromMinutes(10);
    options.KeepAliveInterval = TimeSpan.FromSeconds(15);
});

Connect from a View

@using CrestApps.OrchardCore
@{
    var hubUrl = Html.SignalRHubUrl<NotificationsHub>();
}

<script at="Foot" depends-on="signalr">
    document.addEventListener("DOMContentLoaded", () => {
        const connection = new signalR.HubConnectionBuilder()
            .withUrl("@hubUrl")
            .withAutomaticReconnect()
            .build();

        connection.on("pong", () => console.log("Connected."));
        connection.start().catch(error => console.error("SignalR connection failed.", error));
    });
</script>

Outside a view, call SignalRHubRoutes.GetTenantAwareHubUrl<T>(httpContext). Built scripts are bundled per file in 3.0, so a script that exposes an API must assign it to window.

Tenant-Isolated Destinations

A backplane is shared infrastructure while user ids and group names are tenant-local. Qualify every destination with the shell name:

using CrestApps.OrchardCore.SignalR.Core;
using Microsoft.AspNetCore.SignalR;
using OrchardCore.Environment.Shell;

public sealed class AlertsHub : Hub
{
    private readonly ShellSettings _shellSettings;

    public AlertsHub(ShellSettings shellSettings)
    {
        _shellSettings = shellSettings;
    }

    public override async Task OnConnectedAsync()
    {
        var userId = Context.UserIdentifier;

        if (string.IsNullOrEmpty(userId))
        {
            Context.Abort();

            return;
        }

        // Group membership is durable state, so never abandon it half-way on ConnectionAborted.
        await Groups.AddToGroupAsync(
            Context.ConnectionId,
            TenantSignalRGroupName.ForUser(_shellSettings.Name, userId),
            HubConnectionWork.MustComplete);

        await base.OnConnectedAsync();
    }
}

Publish from a service through IHubContext<T> to the same name:

await hubContext.Clients
    .Group(TenantSignalRGroupName.ForUser(shellSettings.Name, userId))
    .SendAsync("notify", payload);

Scale-Out

| Backplane | Feature | Configuration |

|---|---|---|

| Redis | OrchardCore.SignalR.Redis | Uses Orchard Core Redis (OrchardCore.Redis) options; the channel prefix is qualified with the Redis instance prefix and the shell name. |

| Azure SignalR Service | OrchardCore.SignalR.Azure | Shell configuration SignalR:Azure:ConnectionString (in appsettings.json under OrchardCore); the application name is derived per tenant. A warning is logged when the connection string is missing. |

{
  "OrchardCore": {
    "SignalR": {
      "Azure": {
        "ConnectionString": "Endpoint=https://contoso.service.signalr.net;AccessKey=...;Version=1.0;"
      }
    }
  }
}

The Contact Center topology check still looks for the deprecated CrestApps.OrchardCore.SignalR.Redis feature ID when a topology profile (for example single-node-distributed) requires a Redis backplane. Enable that compatibility feature on those sites; it turns on OrchardCore.SignalR.Redis. See orchardcore-contact-center.

Upgrading from 2.x

Common Failures

| Symptom | Check |

|---|---|

| 404 on hub negotiation | Enable OrchardCore.SignalR and map the hub with SignalRHubRoutes.GetHubPath<T>() |

| URL misses the tenant prefix | Generate it with Html.SignalRHubUrl<T>() |

| signalR is undefined | Add depends-on="signalr" and render footer scripts |

| Payload casing differs | The JSON protocol is camel case |

| Messages reach users of another tenant | Use TenantSignalRGroupName destinations instead of Clients.User |

| Agent connected but receives no queue events | Group membership was cancelled half-way; use HubConnectionWork.MustComplete |

| Tenant fails to start with both backplanes | Enable only one of OrchardCore.SignalR.Redis and OrchardCore.SignalR.Azure |

| Multi-node clients miss messages | Enable a backplane and configure Redis or Azure SignalR |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.