This repo contains code for a real-time chat app. The app is divided into 2 sections frontend and backend.
To use the app on your local environment
- clone the github repo into an appropriate directory.
- cd to that directory.
- cd to frontend and run
npm i
- Similarly , cd to backend and run
npm i
- Make sure to create your own .env file in the backend, which should look something like this:
JWT_SECRET=your_key
JWT_EXPIRES_IN=1d
JWT_COOKIE_EXPIRES_IN=1
- To start the app, cd to frontend ,run
npm run dev
and cd to backend,
npm run dev
and go to site to open the app.
- Make sure to add your .env file in the backend to handle all the secret keys.
(will try to fix in future)
- Due to the very nature of the Web sockets , the app works only if both the users are connected to the socket at the same time. To improve on this feature, we need to integrate messages to the database as well and load the messages whenever the user is logged in.
- There is a latency in loading of friend requests. The user needs to login again to refresh the friend requests. This might be due to some error in state management.
- Message statuses are not functional yet.
The frontend is entirely written in React in jsx using vite as the bundler. The styling is done using tailwind.css . Other services and packages such as react-router-dom, axios and mainly socket.io. The entire client-side is majorly divided into 3 routes,
- Signup
- Login
- Chat
The Signup is to create new users, Login is to authenticate the user and provide them with a jwt token to enter the chat app.
The server of the app is written using Express.js and socket connections are managed using socket.io. The database is the local server of mongodb which is managed using mongoose.
The app can be opened in the browser and user can login or signup accordingly. The sidebar in the app displays the list of other online users that are available for chat, The user can click on any online user and start a chat with them.
As mentioned earlier , server is written in express. There are mainly 2 routes linked with the server:
- api/auth → AuthRouter
- api/user → UserRouter
Auth router is linked with authentication and authorisation of the user including SignUp and Login. The file AuthController.js manages the entire functionality of Signup and Login. Signup is to register new users while Login is to authenticate existing users and provide them with a session token to enter the app. The jsonwebtoken library has been used to provide jwt tokens to the users. The utility file handles the process to generate a web token for the authenticated user.
User router is linked with registered users in the database and is used to fetch them as required by the app.
App.jsx has defined 4 frontend routes.
- '/' → Home(Chat) Page
- 'login' → Login Page
- '/signup' → Signup Page
- '/profile' → Profile Page