Which transports does ASP.NET Core SignalR support, and how does a client end up using one of them?
answer
- three transports, one preferred order
- POST /negotiate lists availableTransports
- HttpTransportType is a [Flags] enum
- Server-Sent Events: text only, one-way
- fall back when a transport fails to start
basics
~20 sSignalR supports WebSockets, Server-Sent Events and long polling. By default the client first POSTs to the hub's /negotiate endpoint, gets the transports the server allows, and tries them in that order, falling back to the next one when a transport fails to start.
solid answer
~50 s`HttpTransportType` is a flags enum: `WebSockets` (1), `ServerSentEvents` (2) and `LongPolling` (4), and both the server's `HttpConnectionDispatcherOptions.Transports` and the clients default to all three. A client starts with `POST /hubs/quotes/negotiate?negotiateVersion=1`; the response carries a `connectionToken` and `availableTransports`, listed as WebSockets, Server-Sent Events, long polling, with WebSockets left out when the server reports no WebSocket support. The client tries them in that order and moves on when one fails to start. WebSockets are full duplex. Server-Sent Events stream server-to-client over one long response, send client messages as separate POSTs and carry text only, so a client using the binary MessagePack protocol skips them. Long polling repeats requests that the server holds open until it has something to send. If every transport fails, the JavaScript client rejects `start()` with "Unable to connect to the server with any of the available transports."
code
javascript · 11 linesimport * as signalR from "@microsoft/signalr";
const connection = new signalR.HubConnectionBuilder()
.withUrl("/hubs/quotes")
.configureLogging(signalR.LogLevel.Information)
.build();
connection.on("Quote", (symbol, price) => render(symbol, price));
// Negotiates, then tries WebSockets, Server-Sent Events and long polling in that order.
await connection.start();go deeper
Recall the three transports, WebSockets, Server-Sent Events and long polling, in that order of preference, and that SignalR falls back automatically.
Explain the negotiate POST, what availableTransports contains, how the client filters by its own options and transfer format, and that the transport is fixed for the connection.
Use the fallback deliberately: know which networks force long polling, how to restrict transports on both sides, and how to measure which transport clients really use.
Decide whether to support every transport or require WebSockets, trading reach behind restrictive networks against server cost and operational simplicity.
## The three transports A SignalR **transport** is the HTTP mechanism that carries hub messages between client and server. Hub code never changes with the transport; only the wire traffic does. | Transport | Direction on one request | Transfer formats | Requests per connection | |---|---|---|---| | WebSockets | both ways | text and binary | one upgrade that stays open | | Server-Sent Events | server to client only | text only | one long `GET`, plus a `POST` per client message | | Long polling | server to client per poll | text and binary | a `GET` per poll, plus a `POST` per client message | In code they are values of `HttpTransportType`, a `[Flags]` enum in `Microsoft.AspNetCore.Http.Connections`: `None` (0), `WebSockets` (1), `ServerSentEvents` (2) and `LongPolling` (4). `HttpTransports.All` combines the three, and it is the default for `HttpConnectionDispatcherOptions.Transports` on the server and for the client options. ## How negotiation picks one With default options, `connection.start()` in the JavaScript client runs these steps: 1. **Negotiate.** `POST /hubs/quotes/negotiate?negotiateVersion=1`. The server creates the connection and answers with JSON holding `connectionId`, `connectionToken` and `availableTransports`, each with its transfer formats. 2. **Order.** The server lists WebSockets first, then Server-Sent Events, then long polling, filtered by its `Transports` option. WebSockets appear only when the server reports WebSocket support for the request. 3. **Filter.** The client skips any transport its own options exclude, and any that cannot carry its protocol's transfer format. 4. **Try in turn.** It starts the first remaining transport with `?id=<connectionToken>`. If that transport fails to start, it logs the failure and tries the next one. 5. **Give up.** If nothing starts, `start()` rejects with "Unable to connect to the server with any of the available transports." Once a transport is running the client keeps it; it does not upgrade to WebSockets later. ## Why the fallback matters WebSockets are the cheapest transport: one connection, no per-message HTTP overhead, binary support. The fallbacks exist because real networks are not uniform: - a proxy or firewall that blocks the WebSocket upgrade; - a server or front end without WebSocket support; - a client protocol that needs binary frames, which rules out Server-Sent Events. The same hub therefore serves a client on a home network over WebSockets and a client behind a strict proxy over long polling, without any code change. ## The MessagePack wrinkle The JSON hub protocol is text, so it works on all three transports. The **MessagePack** protocol is binary. The server advertises Server-Sent Events with the `Text` transfer format only, so a MessagePack client skips it and goes from WebSockets straight to long polling. ## Restricting the list Either side can narrow the choice: - server: `app.MapHub<QuotesHub>("/hubs/quotes", o => o.Transports = HttpTransportType.WebSockets | HttpTransportType.LongPolling);` - JavaScript client: `withUrl(url, { transport: HttpTransportType.WebSockets | HttpTransportType.LongPolling })`; - .NET client: `options.Transports` inside `WithUrl`. The effective set is the intersection. If it is empty, the JavaScript client fails with "None of the transports supported by the client are supported by the server." ## Seeing which transport a connection uses Inside a hub, `Context.Features.Get<IHttpTransportFeature>()?.TransportType` returns the `HttpTransportType` in use. Logging it once per connection shows how many users actually fall back.
- Why does a SignalR client using the MessagePack protocol never use Server-Sent Events?MessagePack is a binary protocol, and Server-Sent Events can carry only text. The negotiate response advertises Server-Sent Events with the `Text` transfer format alone, so a client that needs `Binary` skips it and uses WebSockets or long polling, which both carry binary.
- Does a client that fell back to long polling switch to WebSockets once they become available?No. The transport is chosen once, when the connection starts, and kept for the connection's lifetime. A client only gets another chance at WebSockets when it starts a new connection, which negotiates again.
- SignalR tries long polling first because it works everywhere.
- Server-Sent Events let the client send hub invocations back over the same stream.
- Without a named transport the client fails when WebSockets are blocked.
- WebSockets are the only transport that can carry the MessagePack protocol.
- The negotiate request is itself the WebSocket handshake.