GitHub

SignalR Connector

Declarative SignalR hub connection that swaps received HTML fragments into the DOM in real time.

<rhx-signalr>

Examples

Server Clock

A background service pushes the server time every 2 seconds via IHubContext<DemoHub>. The component replaces its content on each message.

Chat Messages

Type a message and click Send. The hub broadcasts to all connected clients and the beforeend swap appends each message.

Messages will appear here...

Hub Implementation

Define a standard SignalR hub. The JoinGroup method is required if you use rhx-groups.

public class ChatHub : Hub
{
    public Task JoinGroup(string groupName)
        => Groups.AddToGroupAsync(Context.ConnectionId, groupName);

    public async Task SendMessage(string user, string message)
    {
        var html = $"<div><strong>{user}</strong>: {message}</div>";
        await Clients.All.SendAsync("ReceiveMessage", html);
    }
}

Server-Side Extensions

Use HtmxSignalRExtensions to send HTML fragments from controllers, background services, or anywhere you have IHubContext.

// From a controller or background service via IHubContext:
await hubContext.SendHtmlAsync("ReceiveMessage",
    "<div>Broadcast to everyone</div>");

await hubContext.SendHtmlToGroupAsync("room1",
    "ReceiveMessage", "<div>Group-only message</div>");

await hubContext.SendHtmlToConnectionAsync(connectionId,
    "ReceiveMessage", "<div>Private message</div>");

Setup

// Program.cs
builder.Services.AddSignalR();

var app = builder.Build();
app.MapHub<ChatHub>("/chatHub");

Properties

Property Type Default Description
hub-url string - The SignalR hub URL to connect to
page string - Razor Page path for hub URL generation
page-handler string - Page handler for hub URL generation
rhx-method string - Hub method name to listen for
rhx-swap string innerHTML How received HTML is swapped: innerHTML, beforeend, outerHTML, etc.
rhx-target string self CSS selector for the swap target element
rhx-reconnect bool true Automatically reconnect on connection loss
rhx-transport string auto Force transport: websockets, sse, or longpolling
rhx-groups string - Comma-separated group names to join on connect
rhx-connection-state bool false Show visual connection state indicator

Accessibility

  • Container has aria-live="polite" so screen readers announce new content as it arrives.
  • Connection state indicator uses color tokens with sufficient contrast (green/red/yellow).
  • Reduced-motion users see a static indicator instead of the pulse animation.
  • Swapped content is processed by htmx so any aria-* attributes in the HTML fragments are preserved.