A simple chatroom written in Go using the native net/rpc package for efficient client-server communication. The implementation keeps the latest 1000 messages in memory and enforces per-client history access (clients can only request their own messages).
Go's native RPC package provides type-safe, efficient communication between Go programs. It offers excellent performance, built-in concurrent handling of connections, and seamless integration with Go's type system.
- 📦
pkg/chat— core types and theChatServiceimplementation (pkg/chat/api.go). Thread-safe in-memory storage with automatic cleanup (keeps last 1000 messages). - 🖥️
cmd/server— RPC server implementation using Go's net/rpc package for efficient communication (cmd/server/main.go). - 🧑💻
cmd/client— interactive terminal client that connects via RPC and supports simple commands (cmd/client/main.go).
-
Build the server
go build ./cmd/server
-
Run the server (default port 8080)
.\server.exe -port 8080
The server supports graceful shutdown via Ctrl+C.
Server flags:
-port <n>— port to listen on (default8080)-logfile <path>— write logs to file instead of stdout
-
Build the client
go build ./cmd/client
-
Run the client and follow the prompt
.\client.exe -server localhost:8080
/help— show help and available commands/history [sinceID]— fetch your own messages. IfsinceIDomitted or0, returns all messages for your user./whoami— print current username/exit— quit the client- Any other text — sent as a message from your username
The server exposes two RPC methods:
- Args:
SendMessageArgs{ Author string, Text string } - Reply:
SendMessageReply{ OK bool, Error string }
- Args:
GetHistoryArgs{ Author string, SinceID int64 } - Reply:
GetHistoryReply{ Messages []Message }
where Message is:
type Message struct {
ID int64 // unique message id
Author string // username
Text string // message body
Timestamp time.Time // server timestamp
}- In-memory storage: Restarting the server clears the history.
- No authentication: The server trusts the
Authorvalue provided by clients. For real deployments add authentication and server-side identity verification. - CLI input cap: The CLI enforces an ~8KB input cap per message; the server itself does not limit message size today.
pkg/chat/api.go— core types andChatServicecmd/server/main.go— RPC server implementationcmd/client/main.go— interactive client
Docker (Docker Hub Link)
This project includes Dockerfiles and a docker-compose.yml to build and run the server and client containers locally.
- Server image:
moazloaie/simple_chatroom:server - Client image:
moazloaie/simple_chatroom:client
Run these from the repository root so the Dockerfiles can access top-level files like go.mod:
docker build -t moazloaie/simple_chatroom:server -f cmd/server/Dockerfile .
docker build -t moazloaie/simple_chatroom:client -f cmd/client/Dockerfile .Start the server and publish port 8080 to the host:
docker run --rm -d -p 8080:8080 --name simple_chatroom-server moazloaie/simple_chatroom:serverRun the client interactively and point it to the host server (Docker Desktop on Windows exposes host.docker.internal):
docker run --rm -it --name simple_chatroom-client moazloaie/simple_chatroom:client -server host.docker.internal:8080The compose file defines a server service (background) and a client service in the client profile (interactive). Recommended workflow:
-
Start the server only (detached):
docker compose up -d server -
Run the client interactively (attached to the compose network):
docker compose run --rm client -server server:8080
Notes:
- Running
docker compose upwithout arguments will start services in your compose file. Theclientservice is in a profile and will not start unless explicitly included or run withdocker compose run. - If you want both services started automatically (not recommended for most dev workflows because the client is interactive), remove the
clientprofile and rundocker compose up. - To persist server logs or data, mount a host directory into the server container and pass
-logfile:
docker run --rm -d -p 8080:8080 -v ${PWD}\data:/data --name simple_chatroom-server moazloaie/simple_chatroom:server -logfile /data/server.log- ✅ Add unit tests for
pkg/chat(recommended) - 🔐 Add authentication so clients can't impersonate each other
- 💾 Add persistence so history survives restarts
This project is licensed under the MIT License.
MIT License
Copyright (c) 2025
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.