In the SignalR JavaScript and .NET clients, how do invoke/InvokeAsync and send/SendAsync differ, and when should you pick each?
answer
- one waits for a completion message
- send resolves once the message is sent
- only invoke surfaces server exceptions
- HubException message vs generic error text
- pending invokes fail when the connection drops
basics
~20 sinvoke (InvokeAsync in .NET) waits for the hub method to finish and returns its result or error. send (SendAsync) completes once the message is sent, so it returns nothing and never reports server-side failures. Use invoke when the outcome matters.
solid answer
~50 s`invoke` sends an invocation with an id and waits for the server's completion message: the promise, or `InvokeAsync<T>` task in .NET, resolves with the hub method's return value or rejects with its error. `send`, or `SendAsync` in .NET, sends a non-blocking invocation with no id; it completes as soon as the message is written, so the server never replies, a result is impossible and an exception in the hub method is invisible to the caller. Server errors reaching `invoke` are generic ("An unexpected error occurred invoking 'Buy' on the server.") unless the hub threw a `HubException` or `EnableDetailedErrors` is on. Both fail immediately when the connection is not connected, and an `invoke` still pending when the connection closes is rejected. I use `invoke` for commands whose outcome the UI shows, `send` for high-volume fire-and-forget signals like a heartbeat.
code
csharp · 14 lines// .NET kiosk client
try
{
// Waits for TickerHub.Subscribe to finish; throws HubException on a server error.
Quote snapshot = await connection.InvokeAsync<Quote>("Subscribe", "MSFT");
Show(snapshot);
}
catch (HubException ex)
{
ShowError(ex.Message); // generic text unless HubException or detailed errors
}
// Fire-and-forget: completes once sent; no result, no server error reported.
await connection.SendAsync("Heartbeat", kioskId);go deeper
Remember that invoke gives you the result and any error, while send only fires the message and tells you nothing back.
Explain the invocation id and completion message behind invoke, what send's promise means, and how HubException and EnableDetailedErrors shape error text.
Handle in-flight calls across a dropped connection: rejected invokes, no replay, and idempotent hub methods so retries are safe.
Set a team rule for which calls may be fire-and-forget, tied to what a lost or failed message costs the business.
## Two ways to call a hub method Both SignalR clients offer two calls that look alike and behave very differently: | | JavaScript | .NET | |---|---|---| | wait for the result | `invoke(name, ...args)` | `InvokeAsync(name, ...)` / `InvokeAsync<T>(name, ...)` | | fire and forget | `send(name, ...args)` | `SendAsync(name, ...)` | ## What goes over the wire The hub protocol distinguishes the two by an **invocation id**: - `invoke` creates an invocation **with** an id and registers a callback for it. The server runs the hub method and replies with a **completion message** carrying that id, plus either the result or an error. - `send` creates a **non-blocking** invocation **without** an id. The server runs the method but sends nothing back. Consequences: 1. `invoke` resolves when the server has **finished** the hub method, so `await connection.invoke("Buy", order)` also tells you the method succeeded. 2. `send` resolves when the message has been **sent** by the client. It does not mean the server received it, ran it, or succeeded. 3. Only `invoke` can carry a return value, and only `invoke` surfaces server exceptions. ## Errors you can see When a hub method throws during an `invoke`, the client's promise rejects (or the .NET task throws a `HubException`). The text depends on the server: - by default it is generic: *An unexpected error occurred invoking 'Buy' on the server.*; - if the hub threw a **`HubException`**, its message is appended, because SignalR treats that type as safe to show clients; - if `HubOptions.EnableDetailedErrors` is `true`, any exception's type and message are appended. It is `false` by default because messages can leak internals. A failure in a method called with `send` produces nothing the client can observe. ## Connection state and in-flight calls - Calling either while the connection is not connected fails at once: the JavaScript client rejects with "Cannot send data if the connection is not in the 'Connected' State.", and the .NET client throws an `InvalidOperationException` saying the method cannot be called if the connection is not active. - If the connection closes while an `invoke` is waiting, the client rejects the pending call (in JavaScript with an error such as "Invocation canceled due to the underlying connection being closed."). Automatic reconnect does **not** replay it; the app decides whether to retry, which matters for anything that is not idempotent, such as placing an order. ## Server-side ordering The server runs one hub method per client at a time by default, queueing the rest. A dashboard that fires many `send` calls quickly still has them processed in order, one after another; `send` saves the round trip, not the server's work. ## Choosing - Use **`invoke`** for commands whose outcome the user sees ("order placed", "symbol not found"), for queries that return data, and whenever you need to know the server finished. - Use **`send`** for high-frequency, low-value signals where a lost or failed message is acceptable: a typing indicator, a heartbeat, a viewport-changed hint. - For a sequence of results over time, neither fits: call a streaming hub method with `stream` (JavaScript) or `StreamAsync<T>` (.NET). In the stock-ticker app, the browser dashboard uses `invoke("Subscribe", symbol)` because it needs the snapshot that comes back, while the kiosk's "still alive" ping every few seconds uses `SendAsync("Heartbeat", kioskId)`.
- A Buy call made with invoke was in flight when the connection dropped and automatic reconnect brought it back. Was the order placed?Unknown. The client rejected the pending invoke when the connection closed, but the server may or may not have run the method before the drop, and reconnect does not replay calls. Make the hub method idempotent, for example with a client-generated order id the server deduplicates, then retry safely.
- Why does the error text from a failed invoke often say only that an unexpected error occurred?SignalR hides exception details by default. Unless the hub threw a `HubException` or `HubOptions.EnableDetailedErrors` is true, the client gets only "An unexpected error occurred invoking '<method>' on the server." Throw `HubException` with a user-safe message for expected failures, and keep detailed errors to development.
- send resolves only after the server has run the hub method.
- SendAsync throws on the client when the hub method throws.
- Automatic reconnect replays invocations that were pending when the connection dropped.
- EnableDetailedErrors is on by default, so clients always see exception messages.
- send can return a value if the hub method has a return type.