Crestapps core signalr

From CrestApps/CrestApps.AgentSkills · View interactive version

Crestapps core signalr is an AI agent skill from the GitHub repository CrestApps/CrestApps.AgentSkills. Skill for SignalR in CrestApps.Core 2.0. Covers AddSignalR on the AI suite builder and AddCoreSignalR with a tenant path prefix and the store-committer hub filter, the global hub defaults it sets (camelCase JSON, eight parallel invocations per client, a stream buffer of 64) that realtime WebRTC sign

How to use this skill

Skill file

CrestApps.Core SignalR

The CrestApps.Core.SignalR package registers SignalR with CrestApps defaults and a HubRouteManager that builds hub paths and absolute URLs (with an optional tenant prefix). The chat hubs themselves (AIChatHubCore<TClient>, ChatInteractionHubBase) live in CrestApps.Core.AI.Chat; see crestapps-core-chat-interactions.

Register SignalR

Through the AI suite builder, optionally with the store-committer hub filter so staged store writes are committed after each hub invocation:

using CrestApps.Core.SignalR;

builder.Services.AddCrestAppsCore(crestApps => crestApps
    .AddAISuite(ai => ai
        .AddSignalR(pathPrefix: "", addStoreCommitterFilter: true)
    )
);

Without the builder, AddCoreSignalR(pathPrefix) returns the ISignalRServerBuilder:

builder.Services.AddCoreSignalR("/tenant-a")
    .AddCrestAppsStoreCommitterFilter();

AddCoreSignalR registers HubRouteManager as a singleton and sets host-wide defaults:

| Setting | Value | Why |

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

| JSON protocol naming | camelCase | Matches the bundled chat scripts |

| MaximumParallelInvocationsPerClient | 8 | StartRealtimeWebRtc holds its invocation open while trickled AddRealtimeIceCandidate calls arrive; with SignalR's default of one, ICE never completes on a remote host |

| StreamBufferCapacity | 64 | Realtime audio is uploaded as a stream of small frames; the default of 10 blocks the dispatch loop while the provider session opens |

The chat hubs need more than that (10-minute client timeout, 15-second keep-alive, 10 MB messages). Configure each concrete hub type:

using CrestApps.Core.AI.Chat;

builder.Services.ConfigureCrestAppsChatHubOptions<AIChatHub>();
builder.Services.ConfigureCrestAppsChatHubOptions<ChatInteractionHub>();

ConfigureCrestAppsChatHubOptions<THub>() uses AddHubOptions<THub>. Setting services.Configure<HubOptions<THub>>(...) directly is silently ignored by SignalR, and a hub mapped without per-hub options runs on the global defaults.

Map Hubs with Minimal Hosting

Use the registered HubRouteManager instance so the configured prefix is applied:

using CrestApps.Core.SignalR.Services;

var app = builder.Build();

var hubRouteManager = app.Services.GetRequiredService<HubRouteManager>();

app.MapHub<ChatInteractionHub>(hubRouteManager.GetPathByHub<ChatInteractionHub>());
app.MapHub<AIChatHub>(hubRouteManager.GetPathByHub<AIChatHub>());

The default path is /Communication/Hub/{HubTypeName}, prefixed when a path prefix is configured (/tenant-a/Communication/Hub/AIChatHub). The static HubRouteManager.MapHub<T>(routes) maps the default path without any prefix and is meant for module startups that are already tenant-scoped. A host may also map a literal path (the sample hosts use /hubs/ai-chat and /hubs/chat-interaction) as long as the client is given the same URL.

Generate Client URLs

using CrestApps.Core.SignalR.Services;

namespace MyApp.Services;

public sealed class ChatHubUrlProvider
{
    private readonly HubRouteManager _hubRouteManager;

    public ChatHubUrlProvider(HubRouteManager hubRouteManager)
    {
        _hubRouteManager = hubRouteManager;
    }

    public string GetChatHubUrl(HttpContext httpContext)
        => _hubRouteManager.GetUriByHub<AIChatHub>(httpContext);
}

| Method | Returns |

|---|---|

| GetPathByHub<T>() | Prefixed relative path for MapHub |

| GetPathByRoute(pattern) | Prefixed relative path for a custom pattern |

| GetUriByHub<T>(httpContext) | Absolute URL from the current request scheme and host |

| GetUriByRoute(httpContext, pattern) | Absolute URL for a custom pattern |

A HubRouteManager constructed with a siteBaseUrlResolver builds absolute URLs from that base URL instead of the request, which helps behind reverse proxies. AddCoreSignalR registers one without a resolver; register your own instance to supply one.

Authorization

AddCoreSignalR() does not configure authentication; app.UseAuthentication() and app.UseAuthorization() must run before endpoints.

Headless clients send a bearer token as the access_token query parameter during the WebSocket handshake. Accept it only on hub paths:

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(options =>
    {
        options.Events = new JwtBearerEvents
        {
            OnMessageReceived = context =>
            {
                var accessToken = context.Request.Query["access_token"];

                if (!string.IsNullOrEmpty(accessToken) &&
                    context.HttpContext.Request.Path.StartsWithSegments("/Communication/Hub"))
                {
                    context.Token = accessToken;
                }

                return Task.CompletedTask;
            },
        };
    });

Inside a hub use Context.User and Context.GetHttpContext(), never IHttpContextAccessor, which is unreliable during hub invocations. Services that need the caller should take the principal as a parameter or use IUserAccessor; the built-in chat hubs publish the connection's principal to it for every invocation. When the transport left IHttpContextAccessor.HttpContext empty (long polling), AIChatHubCore sets the connection's own HttpContext on it for the invocation and clears it afterwards, so visitor identity and rate-limit keys resolve on every transport. Besides SendMessage, the AI chat hub has RunAgent, which streams AG-UI events (see crestapps-core-ag-ui). Do not attach a policy that lists schemes to a hub that must also accept anonymous connections; a failed policy replaces HttpContext.User with an empty principal.

Browser Client

Pin the SignalR browser client and check its bytes, as the sample hosts do:

<script src="https://cdn.jsdelivr.net/npm/@microsoft/signalr@10.0.11/dist/browser/signalr.min.js"
        integrity="sha384-N+pL/4qXZHZcvsa6hVMd5/7cdF3lC59ZMNHeQyRo5q9jmC7yw0hcJILPjUDSU7ZS"
        crossorigin="anonymous"></script>

A floating @latest URL cannot carry an integrity hash.

Scale-Out

SignalR keeps connection state in memory. Behind a load balancer with several instances, add a backplane (Redis via AddStackExchangeRedis(...) on the ISignalRServerBuilder, or Azure SignalR Service). A realtime voice session is bound to one server for its whole life, so use sticky sessions when WebSockets are not guaranteed.

Troubleshooting

Upgrading from 1.x

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.