signalr について
ASP.NET Core SignalR によるクライアントとサーバー間のリアルタイム通信を実装するスキルです。Hub とグループのパターン、JWT クエリパラメーター認証、サービス連携、接続テストを扱います。
主な機能
- 01SignalR Hub の実装パターン
- 02グループ単位のメッセージルーティング
- 03型付き Hub とクライアントインターフェース
- 04JWT クエリパラメーター認証
- 05サービス通知とクライアントテスト
活用シーン
- 01購読者にワークフロー通知を配信
- 02Register またはウォレット単位で台帳イベントを振り分け
- 03通知ロジックを Hub から分離
- 04認証済み Hub 接続をテスト
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ stuartf303/ sorcha --skill signalrインストール後、Claude Code で/signalrを入力して読み込まれたか確認します。
同梱ファイルreferences/patterns.mdreferences/workflows.md
詳細
SignalR スキル
リアルタイムのクライアント・サーバー通信を実現する ASP.NET Core SignalR の実装です。Sorcha では、ワークフロー通知用の ActionsHub(Blueprint Service)と、台帳イベント用の RegisterHub(Register Service)の 2 つのハブを使用します。どちらも、クエリパラメーターを介した JWT 認証と、グループベースのブロードキャストを使用します。
クイックスタート
ハブの実装
// 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);
主な概念
| 概念 | 用途 | 例 |
|---|---|---|
| グループ | メッセージの送信先を購読者ごとに振り分けます | wallet:{address}, register:{id}, tenant:{id} |
| 型付きハブ | コンパイル時の安全性を確保します | Hub<IRegisterHubClient> |
| IHubContext | サービスから送信します | IHubContext<THub> を注入します |
| JWT 認証 | クエリパラメーターによる認証 | ?access_token={jwt} |
よくあるパターン
ハブを介したサービスの抽象化
対象: ビジネスロジックを SignalR の実装から分離する場合
// Interface in Services/Interfaces/
public interface INotificationService
{
Task NotifyActionAvailableAsync(ActionNotification notification, CancellationToken ct = default);
}
// Register in DI
builder.Services.AddScoped<INotificationService, NotificationService>();
Program.cs でのハブ登録
builder.Services.AddSignalR();
// Map after authentication middleware
app.MapHub<ActionsHub>("/actionshub");
app.MapHub<RegisterHub>("/hubs/register");
関連項目
関連スキル
- aspire - サービスのオーケストレーションと構成
- jwt - ハブ接続用の認証トークンのセットアップ
- redis - スケーリング用のバックプレーン構成
- xunit - 統合テストのパターン
- fluent-assertions - ハブのテスト用アサーション
ドキュメントリソース
Context7 で最新の SignalR ドキュメントを取得します。
Context7 の使用方法:
mcp__context7__resolve-library-idを使用して「signalr aspnetcore」を検索します- ソースコードよりもウェブサイトのドキュメント(
/websites/で始まる ID)を優先します - 解決したライブラリ ID を使用して
mcp__context7__query-docsでクエリします
ライブラリ ID: /websites/learn_microsoft_en-us_aspnet_core (SignalR を含む ASP.NET Core ドキュメント)
おすすめのクエリ:
- 「SignalR ハブのグループと認証」
- 「SignalR Redis バックプレーンのスケーリング」
- 「SignalR の強く型付けされたハブ」
コメント
Markdown 対応