Smallchat – A minimal programming example for a chat server

2023-11-0222:4041github.com

A minimal programming example for a chat server. Contribute to antirez/smallchat development by creating an account on GitHub.

TLDR: This is just a programming example for a few friends of mine. I uploaded a video on my YouTube channel zooming into the code, to see what can be learned from a so simple and broken (on purpose) example. More videos and improvements will follow, see the end of this README file.

And now, the full story:

Yesterday I was talking with a few friends of mine, front-end developers mostly, who are a bit far from system programming. We were remembering the old times of IRC. And inevitably I said: that writing a very simple IRC server is an experience everybody should do (I showed them my implementation written in TCL; I was quite shocked that I wrote it 18 years ago: time passes fast). There are very interesting parts in such a program. A single process doing multiplexing, taking the client state and trying to access such state fast once a client has new data, and so forth.

But then the discussion evolved and I thought, I'll show you a very minimal example in C. What is the smallest chat server you can write? For starters to be truly minimal we should not require any proper client. Even if not very well, it should work with telnet or nc (netcat). The server's main operation is just to receive some chat line and send it to all the other clients, in what is sometimes called a fan-out operation. However, this would require a proper readline() function, then buffering, and so forth. We want it simpler: let's cheat using the kernel buffers, and pretending we every time receive a full-formed line from the client (an assumption that is in practice often true, so things kinda work).

Well, with these tricks we can implement a chat that even has the ability to let the user set their nick in just 200 lines of code (removing spaces and comments, of course). Since I wrote this little program as an example for my friends, I decided to also push it here on GitHub.

Future work

In the next few days, I'll continue to modify this program in order to evolve it. Different evolution steps will be tagged according to the YouTube episode of my series on Writing System Software covering such changes. This is my plan (may change, but more or less this is what I want to cover):

  • Implementing buffering for reading and writing.
  • Avoiding the linear array, using a dictionary data structure to hold the client state.
  • Writing a proper client: line editing able to handle asynchronous events.
  • Implementing channels.
  • Switching from select(2) to more advanced APIs.
  • Simple symmetric encryption for the chat.

Different changes will be covered by one or more YouTube videos. The full commit history will be preserved in this repository.


Page 2

You can’t perform that action at this time.


Read the original article

Comments

  • By AlbertoGP 2023-11-036:48

    Coincidentally, a programming video livestreamer I follow has been doing the same during the last days, but first in Go and then did the Rewrite It In Rust™ part.

    First video, for Go: “My Viewers DDoSed my Go App” https://www.youtube.com/watch?v=qmmQAAJzM54

    The RIIR series does not have yet the cleaned-up version he uploads later to YouTube but the raw Twitch streams are still available:

    > “When I uploaded the VOD the other day somebody in the comment section reminded me that, aren’t I supposed to not start any new Go projects because of all the telemetry stuff? And I realized that I completely forgot about it. And I suppose that was the plan of Google, to basically make it opt-in, make peole get comfortable, make them forget about it, and one day, one night, turn the telemetry on for everyone. That’s why we’re rewriting the whole thing in Rust. To be fair, I don’t really care that much, so if it wasn’t super-easy to do that, I wouldn’t even probably try to do that.”

    “Rewriting My Go App in Rust” https://www.twitch.tv/videos/1965033761

    “Chat is DDoS-ing me as I program in Rust” https://www.twitch.tv/videos/1965874043

    Yesterday it was about building a console/TUI (Text User Interface) client instead of using telnet: “Checking Crossterm Rust Library” https://www.twitch.tv/videos/1966704101

    Here is the code: https://github.com/tsoding/4at

HackerNews