Skip to content
ballmüllersolutions logo Send an email Call us
November 27, 2026

WebSocket

WebSocket is a protocol (RFC 6455) for a lasting two-way connection between browser and server over TCP. Both sides can send messages at any time, without the client having to ask each time.

WebSocket is a protocol that sets up a lasting channel between client and server over which both sides can send messages at any time. It is described in RFC 6455 and runs over TCP connections.

Request and response versus an open connection

With normal HTTP, the client asks and the server answers. If a server wants to report something on its own, such as "there is a new booking", all it can do is wait for the client to ask regularly. With WebSocket, the connection starts as an HTTP request and is then switched to the WebSocket protocol. After that it stays open, and the server can send the browser messages as soon as something happens. Typical uses are chats, dashboards, progress displays, and games.

WebSockets in ASP.NET Core

ASP.NET Core supports WebSockets with a middleware. An endpoint accepts the connection and reads and writes messages:

app.UseWebSockets();

app.Map("/ws", async context =>
{
    if (!context.WebSockets.IsWebSocketRequest)
    {
        context.Response.StatusCode = StatusCodes.Status400BadRequest;
        return;
    }

    using var socket = await context.WebSockets.AcceptWebSocketAsync();
    var buffer = new byte[4096];
    var result = await socket.ReceiveAsync(
        new ArraySegment<byte>(buffer), context.RequestAborted);

    while (!result.CloseStatus.HasValue)
    {
        await socket.SendAsync(
            new ArraySegment<byte>(buffer, 0, result.Count),
            result.MessageType, result.EndOfMessage, context.RequestAborted);
        result = await socket.ReceiveAsync(
            new ArraySegment<byte>(buffer), context.RequestAborted);
    }

    await socket.CloseAsync(
        result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
});

This is an echo: everything that arrives goes back. For your own applications, Microsoft recommends SignalR instead of raw WebSockets in most cases. SignalR uses WebSockets when they are available and otherwise falls back to server-sent events or long polling. It offers a simple model in which client and server call methods on each other, and according to Microsoft it costs no significant performance disadvantage in most cases. The connection behind interactive Blazor components on the server is a SignalR connection too.

What to watch out for

Further terms: API, web server, and REST as the request-response style that WebSocket complements.

An example from the Kladde

Whoever has the appointment list open in the office has so far only learned of a new booking after reloading the page. In the real-time module, the Kladde gets a SignalR hub: as soon as a booking is stored, it appears in the open list, and a screen reader announces it. Before that, you decide whether real time pays off here at all.

In the course

You learn WebSockets and SignalR in K2, module 2.13 (real time with SignalR): Hubs and clients, then sign-in and permissions in the hub, and Scaling SignalR. Raw WebSockets without SignalR are not a teaching topic of their own.

Sources