⚡ Zig Guide LiveUnofficialbut fully verified
✓ Zig 0.18.0-dev.624+b7f2c6d8fWhat's newOn an older Zig?Blograylib-zig

A Chat Server

This project builds a chat room on the four WebSocket chapters of Networking. Each chapter makes one decision: what a message means, who may write to a socket, what to do with a client that stops reading, and how to read frames that std refuses. The protocol, the hub and the frame reader take bytes and return bytes, so those chapters run in your browser. The last chapter puts them on real sockets, with three clients.

  1. Give each connection one writer. Other tasks put lines into its outbox. Then two tasks never write to one socket at the same time.
  2. A broadcast must never wait for one member. Put with a minimum of 0, and if the outbox is full, drop that member.
  3. A slot number is not an identity. When the hub can drop a member and reuse its slot, find members by a pointer that the connection owns.
  4. Read each close frame to its end. Then no byte is left unread, and the server can close the TCP connection first, as RFC 6455 asks.

6 chapters.

  • What You Will BuildA chat room over WebSocket with only the standard library, and the four decisions that the next chapters make.
  • The ProtocolCommands in, events out. A text protocol inside WebSocket messages, a tagged union for each direction, and a line size that the compiler checks.
  • The HubOne writer for each socket. The hub keeps the members, copies each line into a bounded outbox, and never touches a socket.
  • Slow ClientsA member that does not read fills its outbox. The hub puts with a minimum of 0, drops the member when nothing fits, and the room does not wait.
  • Read the Frames YourselfA frame reader for the server. It joins fragments, returns the close code, checks UTF-8 and the size, and maps each failure to a close code.
  • The ServerThe protocol, the hub and the frame reader on real sockets. A reader task and a writer task for each connection, and three clients that chat.