SignalR provides bidirectional calls and events between a browser and a .NET server. WebSocket is a transport; SignalR adds invocation semantics, framing and connection lifecycle above it. A reliable integration also needs explicit handling for authentication, initial failures, reconnects and the rate at which a recipient can process messages.
Hub methods and server-originated events
A public hub method represents a server operation that clients may invoke. Clients.User, Clients.Group and Clients.All select recipients. Group names are routing labels, so membership must be decided using authenticated identity rather than a client-supplied claim.
Hub instances are transient. Storing shared state in hub fields assumes an object lifetime the framework does not promise. Long-lived publishers can use IHubContext. Selecting a recipient and authorizing that recipient remain separate responsibilities.
Microsoft Learn — SignalR hubs
WebSocket selection and negotiation
The transport option in withUrl restricts the client transport. skipNegotiation: true is used with an explicitly selected WebSocket connection. The server and any proxy must agree. Working HTTP endpoints do not prove that a proxy will allow WebSocket upgrade.
The /hubs/stream route below is an illustrative address and must match the server mapping. Method names, event names and payload fields form a contract. A connection can remain open while incompatible payload assumptions produce incorrect application state.
const connection = new HubConnectionBuilder()
.withUrl('/hubs/stream', {
transport: HttpTransportType.WebSockets,
skipNegotiation: true,
accessTokenFactory: () => readCurrentAccessToken()
})
.withAutomaticReconnect([0, 2000, 10000, 30000])
.build();Microsoft Learn — SignalR configuration
invoke, send and event handlers
invoke waits for the hub method to complete and can return its result. send does not provide the same result contract. Choose according to whether the caller needs confirmation from the server operation, and handle rejection separately from local transport state.
Register event handlers before starting the connection. Remove the same function reference when its consumer is disposed. Adding another anonymous callback on every screen entry can duplicate effects. A TypeScript payload type also does not validate remote JSON at runtime.
const onState = (payload: StateDto) => acceptValidatedState(payload);
connection.on('StateUpdated', onState);
await connection.start();
const result = await connection.invoke<ResultDto>('Join', roomId);
// On consumer disposal:
connection.off('StateUpdated', onState);Reconnect does not handle the initial start failure
withAutomaticReconnect handles recovery after an established connection is lost. Failure of the first connection.start() needs separate handling. onreconnecting can update the UI, while onreconnected is the point to recover required membership and current state.
A restored transport does not reveal whether an earlier command executed. Repeatable commands need application identifiers. Monotonic state sequence numbers can reject obsolete data. Neither identifier should be confused with the socket connection identifier.
connection.onreconnecting(() => showConnectionState('reconnecting'));
connection.onreconnected(async () => {
await restoreMembershipAndCurrentState();
showConnectionState('connected');
});
try {
await connection.start();
} catch {
showConnectionState('initial-start-failed');
// Let the caller schedule a bounded retry or user action.
}Microsoft Learn — JavaScript client
accessTokenFactory and authenticated connections
accessTokenFactory should return the current token rather than a stale value captured during construction. If several callers can request renewal concurrently, share the renewal work through one in-flight promise.
Browser WebSocket APIs cannot attach a custom Authorization header, so SignalR may transmit the token as a query parameter for that transport. HTTPS protects transit, while query-string logging still needs attention. Claims on an established connection are not automatically reloaded for every message.
Microsoft Learn — SignalR authentication
Backpressure and ordering
If production outpaces consumption, the outgoing queue grows. A bound limits memory, but dropping policy depends on meaning: newer state can replace older state, while a one-time event may still matter.
Carry events forward when coalescing state and deduplicate them with event identifiers. A saturated critical backlog needs an explicit failure-and-recovery policy rather than unlimited retention. Queue length, oldest-message age and reconnect duration explain problems that round-trip latency alone cannot.
Related technical articles
- Supabase Storage and S3: signed HTTP requests, streaming and caching
- Phaser and TypeScript: scene lifecycle, time steps and interpolation
- MailKit and an outbox: SMTP, TLS and retryable email
- JWT and refresh tokens: validation, rotation and browser sessions
- PostgreSQL and EF Core: DB-first, queries and concurrency