# Chat Application using Socket.io

*Originally published on December 25, 2023 on* [*my previous blog*](https://somesh-banerjee.github.io/blogs/blog/12)*.*

---

This comprehensive guide will create a simple chat application where users can login and join chat rooms. Users will be able to send messages to each other in the chat rooms. We will use socket.io to create a real time chat application. JWT will be used for authentication. The frontend will be created using React and the backend will be created using NestJS. The database will be MongoDB.

Every code cannot be covered in this blog. So, I have created a repository with the complete code. You can find the repository [here](https://github.com/somesh-banerjee/chat-app-nestjs).

## Getting Started: Environment Setup

We will start by creating project folders for the frontend and backend. For this project I will use a single repository for both the frontend and backend. You can use separate repositories if you want. I will use the following folder structure:

```
chat-app
├── backend
└── client
```

We will start by creating the frontend. We will use vite to create the frontend. Vite is a build tool that is used to create frontend applications. It is similar to create-react-app but it is much faster. You can read more about vite [here](https://vitejs.dev/).

To create the frontend, run the following commands. When prompted, select react as the framework and typescript as the language, and enter 'client' as the project name.

```bash
yarn create vite
```

In the frontend, I have also used tailwindcss for styling. You can read more about tailwindcss [here](https://tailwindcss.com/). This is optional and you can use any other styling method you want.

Next, we will create the backend. We will use nestjs to create the backend. Nestjs is a framework for creating backend applications. It is similar to express but it is much more structured. You can read more about nestjs [here](https://nestjs.com/).

To create the backend, run the following commands

```bash
yarn global add @nestjs/cli
nest new backend
```

This will create a nestjs project in the backend folder. We will use mongodb as the database for this project. You can read more about mongodb [here](https://www.mongodb.com/).

We will use docker to run mongodb. You can use any other method to run mongodb if you want. To run mongodb using docker, run the following command

```bash
docker run -d -p 27017:27017 --name mongodb mongo
```

This will run mongodb on port 27017. We will use this port in the backend to connect to mongodb.

This completes the environment setup. Next, we will start with the functionality of the application.

## Building User Functionality: User Module Creation

We will start by creating the user module. User module will contain all the functionality related to users such as schema, user creation, updating user details, etc. User creation is not same as user registration. We will see user registration later.
We will use mongoose to create the user schema. We will use nest cli to create the user module, service, and controller.

```bash
nest g module user
nest g service user
nest g controller user
```

Next we will create schemas and interfaces directory in the user module. We will create a user schema and a user interface. The user schema will contain the user details and the user interface will be used to type check the user details. Follow the repository for the code of each file.

## Implementing JWT Authentication: Authentication Module Integration

Now that we have the user module, we will create the authentication module. The authentication module will contain all the functionality related to authentication such as login, registration, etc. We will use nest cli to create the authentication module, service, and controller.

```bash
nest g module auth
nest g service auth
nest g controller auth
```

We will also create jwt directory in the auth module. This directory will contain the `jwt.service.ts` file which will contain the logic for creating and verifying jwt tokens. We will use the `jsonwebtoken` library to create and verify jwt tokens.

## Designing the User Interface: Frontend Design

Using the user module and authentication module, we will create simple login and registration pages. We will use react router to create the routes for the login and registration pages. The pages will have a single form with email and password fields, and on submit, the form will call the login or registration api.

This step will complete the Authentication functionality. Next, we will start with the chat functionality.

## Building the Chat Rooms: Room Module Implementation

We will start by creating the room module. The room module will contain all the functionality related to chat rooms such as creating rooms, adding messages to rooms, etc. We will use nest cli to create the room module, service, and controller.

```bash
nest g module room
nest g service room
nest g controller room
```

Next, we will create the schemas and interfaces directory in the room module. In this we will add the schema and interface for room and message.

In the frontend, we will create a page to display all the rooms. The rooms page will display all the rooms and will have a button to create a new room. We will also create a page to display the messages in a room. The messages page will display all the messages in a room and will have a form to add a new message to the room.

## Facilitating Real Time Communication: Integrating Socket.io

Till now whatever we have created can be considered a chat application. But the messages are not real time. A new message will only be displayed when the user refreshes the page. So, we will use socket.io to make the messages real time. We will use socket.io in the frontend and backend.

For backend, lets create a chat module and gateway. The chat gateway will be used to send and receive messages. We will use nest cli to create the chat gateway.

```bash
nest g module chat
nest g gateway chat
```

The chat gateway will contain the logic for sending and receiving messages. We will use socket.io to send and receive messages. It will use JwtService and RoomService to verify the user and add messages to the room. All socket related functionality will be in the chat gateway. The chat gateway will emit the messages to all the users in the room when a new message is added to the room.

Similarly, we will use socket.io-client in the frontend to receive messages. We will use the useEffect hook to connect to the socket and receive messages.

This completes the whole scope of this project. You can fork my repository and
add more functionality to this project if you want.

Some of the features that you can add are creating private rooms, adding user status like online or offline, adding user profile, etc.

