WebSocket¶
The WebSocket module provides real-time bidirectional communication. Enable with [WebSocket] on your [RestApi] runtime and use IWebSocketConnectionManager to manage connections, groups, and broadcasting.
Package: Deepstaging.Realtime.WebSocket
Quick Start¶
This generates a /ws endpoint with JWT authentication from ?token=.
Connection Manager¶
public interface IWebSocketConnectionManager
{
Task OnConnected(string connectionId, string userId, WebSocket socket);
Task OnDisconnected(string connectionId);
Task JoinGroup(string connectionId, string groupId);
Task LeaveGroup(string connectionId, string groupId);
Task SendToGroup(string groupId, string message, string? excludeConnectionId = null);
Task SendToUser(string userId, string message);
Task<IReadOnlyList<string>> GetGroupMembers(string groupId);
}
await connectionManager.JoinGroup(connectionId, $"order-{orderId}");
await connectionManager.SendToGroup($"order-{orderId}", JsonSerializer.Serialize(update));
Providers¶
InMemoryConnectionManager uses ConcurrentDictionary — suitable for single-server and development. For multi-server, implement IWebSocketConnectionManager with a distributed store (e.g., Redis pub/sub).