Loading...
Loading...
Implements real-time WebSocket communication using SignalR for action notifications and register events. Use when: Adding real-time notifications, creating hub endpoints, broadcasting to groups, or testing WebSocket communication.
npx skill4agent add stuartf303/sorcha signalrActionsHubRegisterHub// Strongly-typed hub with client interface
public class RegisterHub : Hub<IRegisterHubClient>
{
public async Task SubscribeToRegister(string registerId)
{
await Groups.AddToGroupAsync(Context.ConnectionId, $"register:{registerId}");
}
}
public interface IRegisterHubClient
{
Task RegisterCreated(string registerId, string name);
Task TransactionConfirmed(string registerId, string transactionId);
}public class NotificationService
{
private readonly IHubContext<ActionsHub> _hubContext;
public async Task NotifyActionConfirmedAsync(ActionNotification notification, CancellationToken ct)
{
await _hubContext.Clients
.Group($"wallet:{notification.WalletAddress}")
.SendAsync("ActionConfirmed", notification, ct);
}
}var connection = new HubConnectionBuilder()
.WithUrl($"{baseUrl}/actionshub?access_token={jwt}")
.Build();
connection.On<ActionNotification>("ActionConfirmed", notification => { /* handle */ });
await connection.StartAsync();
await connection.InvokeAsync("SubscribeToWallet", walletAddress);| Concept | Usage | Example |
|---|---|---|
| Groups | Route messages to subscribers | |
| Typed Hubs | Compile-time safety | |
| IHubContext | Send from services | Inject |
| JWT Auth | Query parameter auth | |
// Interface in Services/Interfaces/
public interface INotificationService
{
Task NotifyActionAvailableAsync(ActionNotification notification, CancellationToken ct = default);
}
// Register in DI
builder.Services.AddScoped<INotificationService, NotificationService>();builder.Services.AddSignalR();
// Map after authentication middleware
app.MapHub<ActionsHub>("/actionshub");
app.MapHub<RegisterHub>("/hubs/register");Fetch latest SignalR documentation with Context7.
mcp__context7__resolve-library-id/websites/mcp__context7__query-docs/websites/learn_microsoft_en-us_aspnet_core