Websockets

Live on this server

A chat that pushes every message to all browsers in the same room.

A chat in the browser: every message is pushed to everyone in the same room, instantly and without polling. The page connects to /chat via a websocket, and ChatHandler forwards every message it receives to the connections in the room of the sender.

The reactive websocket API calls OnConnected, OnMessage and OnClose for you, and reads and writes JSON with ReadPayloadAsync and WritePayloadAsync. Open the page in two windows with the same room to chat with yourself.

To run it, create a console app (dotnet new console), add GenHTTP (dotnet add package GenHTTP.Full), replace Program.cs with the code below and start it with dotnet run - it answers at http://localhost:8080/.

Program.cs154 lines
using GenHTTP.Engine.Internal;

using GenHTTP.Api.Protocol;

using GenHTTP.Modules.IO;
using GenHTTP.Modules.Layouting;
using GenHTTP.Modules.Practices;
using GenHTTP.Modules.Websockets;

var chat = Websocket.Reactive()
                    .Handler(new ChatHandler());

var page = Content.From(Resource.FromString(ChatPage.Html).Type(ContentType.TextHtml));

var app = Layout.Create()
                .Index(page) // GET /
                .Add("chat", chat); // ws://localhost:8080/chat?room=...

await Host.Create()
          .Handler(app)
          .Defaults()
          .RunAsync();

public record ChatMessage(string Author, string Text, DateTime? Sent = null);

public class ChatHandler : IReactiveHandler
{
    // the room of every connected client, as requested via "?room=..."
    private readonly Dictionary<IReactiveConnection, string> _clients = [];

    // connections are not thread-safe, so we never write to them concurrently
    private readonly SemaphoreSlim _sync = new(1, 1);

    public async ValueTask OnConnected(IReactiveConnection connection)
    {
        var room = connection.Request.Header.Query.GetEntry("room") ?? "lobby";

        await _sync.WaitAsync();

        try
        {
            _clients[connection] = room;
        }
        finally
        {
            _sync.Release();
        }
    }

    public async ValueTask OnMessage(IReactiveConnection connection, IWebsocketFrame message)
    {
        // JSON sent by the client is deserialized into our record
        var received = await message.ReadPayloadAsync<ChatMessage>();

        if (received == null || received.Text.Length > 500)
        {
            return;
        }

        var toSend = received with { Sent = DateTime.UtcNow };

        await _sync.WaitAsync();

        try
        {
            var room = _clients[connection];

            foreach (var (client, clientRoom) in _clients)
            {
                if (clientRoom == room)
                {
                    // ... and serialized to JSON again
                    await client.WritePayloadAsync(toSend);
                }
            }
        }
        finally
        {
            _sync.Release();
        }
    }

    public async ValueTask OnClose(IReactiveConnection connection, IWebsocketFrame message)
    {
        await _sync.WaitAsync();

        try
        {
            _clients.Remove(connection);
        }
        finally
        {
            _sync.Release();
        }

        await connection.CloseAsync();
    }

}

public static class ChatPage
{

    public const string Html =
            """
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="utf-8">
            <title>Chat</title>
        </head>
        <body>
            <p>Room <strong id="room"></strong> - <a id="invite" target="_blank">open it in another window</a></p>
            <form id="form">
                <input id="author" placeholder="Your name" maxlength="50" required>
                <input id="text" placeholder="Your message" maxlength="500" required autofocus>
                <button>Send</button>
            </form>
            <ul id="messages"></ul>
            <script>
                // every visitor gets a room of their own, unless invited to another one
                const params = new URLSearchParams(location.search);
                const room = params.get('room') || Math.random().toString(36).substring(2, 8);

                document.getElementById('room').textContent = room;
                document.getElementById('invite').href = `?room=${room}`;

                const url = new URL(`chat?room=${room}`, location.href.replace(/^http/, 'ws'));
                const socket = new WebSocket(url);

                socket.onmessage = (event) => {
                    const message = JSON.parse(event.data);

                    const item = document.createElement('li');
                    item.textContent = `[${new Date(message.sent).toLocaleTimeString()}] ${message.author}: ${message.text}`;

                    document.getElementById('messages').prepend(item);
                };

                document.getElementById('form').onsubmit = (event) => {
                    event.preventDefault();

                    const text = document.getElementById('text');

                    socket.send(JSON.stringify({ author: document.getElementById('author').value, text: text.value }));

                    text.value = '';
                };
            </script>
        </body>
        </html>
        """;

}

Try it

Send the requests to the sample running on this server - change the path, headers or body as you like, or copy them as curl commands.

  1. Open the chat

    Every visitor gets a room of their own - open it a second time to chat with yourself.

    PAGE /demo/websockets/ Open