This repository contains Chatr, a Text User Interface (TUI) chatbot that communicates over TCP. Built in Go, Chatr allows multiple clients to connect to a server and exchange messages in real-time through a command-line TUI. This project serves as the final assignment for the Network lecture under the guidance of Professor Hamid Haj Seyyed Javadi.
Chatr is a client-server chat application written in Go that uses raw TCP sockets to send and receive messages. The project demonstrates practical networking concepts by:
- Establishing a persistent TCP connection between a server and multiple clients.
- Broadcasting messages from one client to all connected clients.
- Dynamically tracking and displaying online users in a TUI.
- Providing a fluid command-line interface experience.
Key features include:
- TUI-based chat client to send and receive messages.
- Server broadcasting to all connected clients in real-time.
- User list management (join and leave notifications).
- Configurable through environment variables or command-line flags.
This project is especially relevant to the Network lecture, as it highlights low-level TCP socket management and concurrency.
Before installing or running this project, make sure you have the following:
-
Go (1.21+ recommended)
You can check your Go version with:go version
-
Git (to clone the repository).
-
Basic knowledge of TCP networking (ports, hosts, firewalls).
Follow these steps to set up Chatr on your local machine:
-
Clone the repository:
git clone https://github.com/itsmohammadheidari/chatr.git cd chatr -
Initialize Go modules (optional if already present):
go mod tidy
This will download and verify all the required dependencies listed in
go.modandgo.sum. -
(Optional) Create a
.envfile in the project root if you want to override default configurations via environment variables. For example:HOST=127.0.0.1 PORT=8080 USERNAME=MyUsername
Note: If you don' t create a
.envfile, Chatr will use the defaults (127.0.0.1:8080) or the values provided via flags.
-
Navigate to the project directory:
cd chatr -
Start the server:
go run main.go server
Alternatively, if you have built the binary (e.g.,
go build -o chatr):./chatr server
By default, the server listens on
127.0.0.1:8080. You can override this via flags or environment variables. -
Server logs: You'll see logs about new connections, disconnections, and server status on your terminal.
-
In a new terminal, navigate to the project directory again (or from anywhere if you have the binary in your
PATH). -
Run the client:
go run main.go client
Or using the built binary:
./chatr client
-
Provide a username (optional). If you want a custom username, you can either:
- Set it in
.env:USERNAME=yourname - Use a flag:
go run main.go client --username yourname
When the client starts, it will connect to the server and you will see a TUI displaying online users and chat messages.
- Set it in
Chatr supports the following environment variables (through .env or your system environment):
HOST(default:127.0.0.1)PORT(default:8080)USERNAME(default:Guestwhen using the client)
Each environment variable can also be overridden by command-line flags:
--hostor-H--portor-P--usernameor-u
- Default Host:
127.0.0.1
This means both client and server run on the localhost. - Default Port:
8080
If port 8080 is in use, choose another port (e.g.,--port 9090) and ensure that both server and client match the same port.
If running on different machines, ensure that:
- The server machine's firewall allows inbound connections on the chosen port.
- The client can reach the server over the network.
Once the client is running, you'll see:
- A User List panel on the left (or top, depending on your screen).
- The Chat Box displaying messages from you and other users.
- An Input Field at the bottom where you can type messages.
Sending Messages:
- Type your message in the input field.
- Press Enter to send the message.
- Watch the conversation scroll up in the Chat Box.
Exiting:
- Press Ctrl + C in the terminal that is running the client (or server).
- Or close the terminal window.
Contributions are highly encouraged! If you have any suggestions for improvements or encounter any bugs, please feel free to open an issue or submit a pull request.
This project is licensed under the GNU General Public License v3.0. For more information, refer to the LICENSE file.
For questions, feedback, or contributions, reach out via:
- Project Maintainer: Mohammad Heidari
- Email: [email protected]
Thank you for using Chatr!