skip to content

Transports & Hub Security

A SignalR connection negotiates, falls back from WebSockets to Server-Sent Events to long polling, and authenticates with a cookie or an access_token query value. Interviewers probe hub auth.

on this pageshow

explore

questions

6

Which transports does ASP.NET Core SignalR support, and how does a client end up using one of them?

level: juniorimportance: must knowfreq 58%

answer

  1. three transports, one preferred order
  2. POST /negotiate lists availableTransports
  3. HttpTransportType is a [Flags] enum
  4. Server-Sent Events: text only, one-way
  5. fall back when a transport fails to start

basics

~20 s

SignalR 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 lines
javascript
import * 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

for a junior

Recall the three transports, WebSockets, Server-Sent Events and long polling, in that order of preference, and that SignalR falls back automatically.

for a middle

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.

for a senior

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.

for a principal

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.
open as a page

How does a browser SignalR client send a bearer token to an ASP.NET Core hub, and why must the server read access_token from the query string?

level: middleimportance: must knowfreq 60%

basics

~20 s

The client supplies the token through accessTokenFactory. SignalR sends it in an Authorization header where it can, but browsers cannot set headers on WebSocket or EventSource requests, so it appends access_token to the URL; JwtBearer's OnMessageReceived must copy that value into context.Token for hub paths.

open as a page

In ASP.NET Core SignalR, what does [Authorize] on a hub class check compared with [Authorize] on a hub method, and when is each evaluated?

level: middleimportance: should knowfreq 48%

basics

~20 s

[Authorize] on the hub class becomes endpoint metadata on the hub's negotiate and connection endpoints, so unauthorized clients are refused before a connection exists. [Authorize] on a method is checked on every invocation against Context.User, the principal captured when the connection started.

open as a page

Your SignalR hub's users sit behind a corporate proxy that blocks WebSockets. What happens, and how do Transports and SkipNegotiation change it?

level: middleimportance: should knowfreq 38%

basics

~20 s

With defaults the WebSocket attempt fails and the client falls back to Server-Sent Events or long polling, so the hub keeps working, just less efficiently. Restricting transports to WebSockets, or skipping negotiation, removes that fallback, and those users cannot connect at all.

open as a page

A SignalR hub on api.example.com serves a browser app on app.example.com. Which CORS policy does the hub need, and why can it not use AllowAnyOrigin?

level: seniorimportance: should knowfreq 40%

basics

~20 s

The SignalR JavaScript client sends credentials by default, so the hub needs a policy with WithOrigins for the app's origin, AllowCredentials, its request headers and GET, POST and DELETE. ASP.NET Core refuses to build a policy combining AllowAnyOrigin with AllowCredentials.

open as a page

In ASP.NET Core SignalR, how do KeepAliveInterval, ClientTimeoutInterval and the client's server timeout fit together, and what else makes the server close a connection?

level: seniorimportance: should knowfreq 44%

basics

~20 s

On WebSockets and Server-Sent Events each side pings when idle (every 15 s) and gives up after 30 s of silence: ClientTimeoutInterval on the server, the server timeout on the client, each at least double the other's keep-alive. An incoming message over 32 KB also closes the connection.

open as a page