How I Built a Command-Line Chat Application: Project Details

Building a project as a backend developer is not often fancy like that of a frontend developer. So I decided on creating a full-stack app, but with the command line being the user interface and an HTTP and socket server being the back end. The project has been on my mind, but I only got to work on it for the GitHub + DEV Hackathon.

Project Overview

Tarminal-chat-app is a command-line chat application that allows for real-time communication between users in a public chat room. The app aims to provide a seamless chat experience from the command line.

The command-line interface was built with commander.js and inquirer.js. It uses Axios for making HTTP requests to the server, while the socket.io-client library is used for establishing real-time communication with the server.

The server is an Express app that uses the socket.io library to enable real-time communication with the client. It uses mongoose for storing user data on MongoDB, bcrypt for hashing user passwords, jsonwebtoken for authentication, and redis for token storage.

Project Goals

The main goal of the project was to build a command-line application that lets users chat with each other across several different terminals.

The features I had in mind were private chat, group chat, and public chat messaging.

I did some research and got a general overview of what the app would look like.

From my research, I intended to make the user interface an NPM package and the server deployed somewhere online so that users could communicate with each other from their respective terminals.

Initially, I did not know how to meet the goals of the project. But as a software developer who knows some fundamentals, I believed it was something I could do.

During development, I changed my mind about the features and only implemented the public chat messaging feature. This feature allowed any user to create a chat room, which would be added to a list of chat rooms that any user could join and start messaging each other.

Project Features

The key features of the projects are:

  • Authentication: Each user is required to provide an email, username, and password during registration. It allows them to log in with their username and password on subsequent requests.

  • Username: This feature allows for the unique identification of users. It is being used to identify each user in the chat rooms after a successful login.

  • Creating and joining public chat rooms: Create-Chat-Room is a home menu option that allows any user to create a chat room and be added to a list of chat rooms that other users can join. Join-Chat-Room is also a home menu option but displays a list of chat rooms that can be joined. With either of the two options, the user is automatically brought to a chat message interface.

  • Chat message interface: This feature allows the user to type text-based messages into the command line. It is enabled only after a user successfully creates or joins a public chat room.

  • Real-time chat: The socket.io and socket.io-client libraries enable this feature to take user-typed messages from the command line and transmit them to all users in the chat room in real time.

  • The Home and Exit option: The 'Exit' option is available throughout the application. It can either be accessed by choosing the option in the authentication and home menu or by typing -e in the chat message interface. The 'Home' option can only be accessed in the chat message interface by typing -h.

Project Structure

The Model-View-Controller (MVC) design pattern inspired the structure of the project. The project is separated into two main folders: /server and /client.

The server folder includes the /src folder, where the database schemas, configurations, and REST API logic live; a /test folder, which contains the test cases for the API; app.js, where the Express app is configured; socketManager.js, where socket event management takes place for the server; .env, which contains environmental variables for the database and authentication; and server.js, which is the entry point of the server.

The client folder consists of the /src folder, which contains the logic for the main features of the app like authentication, user interface, and chat message interface; attachEvents.js, which contains socket events for the client; and commander.js, which is the node script for the user interface.

In the next section, I dive into the project's server codebase.