Course contentsShow
← C#
Lesson 1438 of 1,90813. Real-time Communication with SignalRFree lesson

Introduction to the SignalR Hub Protocol

Understand how the Hub abstraction simplifies client-server handshakes and message routing.

In this lesson, you will learn how the SignalR Hub abstraction acts as a high-level RPC (Remote Procedure Call) layer that masks the complexity of managing raw WebSocket connections and message routing.

The Problem with Raw Sockets

When working with raw WebSockets, you are responsible for defining your own communication protocol. You must manually serialize data, decide how to identify which client sent a message, and route responses back to specific users or groups. This boilerplate code quickly becomes fragile and hard to maintain as your real-time requirements grow.

How Hubs Simplify the Handshake

The Hub abstraction in SignalR abstracts this away by providing a strongly-typed framework for client-server communication. When you inherit from Hub in your C# code, SignalR handles the low-level handshake—negotiating the best transport mechanism (WebSockets, Server-Sent Events, or Long Polling)—automatically.

Instead of writing custom routers, you define methods within your class that can be invoked directly by the client. SignalR handles the message framing, invocation mapping, and serialization logic for you:

public class ChatHub : Hub
{
    // The client can call 'SendMessage' directly, 
    // and SignalR routes the request to this method.
    public async Task SendMessage(string user, string message)
    {
        // Hubs provide built-in routing to all connected clients
        await Clients.All.SendAsync("ReceiveMessage", user, message);
    }
}

Automatic Routing

In this example, the Hub acts as the message broker. When the server executes Clients.All.SendAsync, SignalR manages the underlying transport state. It identifies which connections are active and pushes the data through the established pipe without you needing to worry about the state of individual socket frames. You simply define the "intent" (what method to call on the client), and the Hub protocol ensures the message is routed to the correct destination, regardless of whether the client is using a browser, a mobile device, or a backend service.

By using the Hub abstraction, you shift your focus from managing connection overhead to building application-level features.

Key Takeaway: The SignalR Hub protocol replaces manual socket management with a high-level RPC framework that handles transport negotiation, automatic serialization, and simplified message routing between the server and connected clients.