Real Time Chat Application Use Case
Real Time Chat Application Use Case
AIM
To develop a real-time chat application using the MERN stack and Socket.IO that allows users to
join chat rooms, send and receive instant messages, and store chat history in MongoDB.
PROCEDURE
1. Project Setup
- Create separate folders for frontend (React) and backend (Node.js + Express).
- Install required packages: express, mongoose, socket.io, cors, axios, react, etc.
2. Backend Development
- Create APIs for user registration, login, and fetching chat history.
3. Frontend Development
- Create pages/components for login, chat rooms, and the chat window.
4. Styling
5. Testing
- Run the application and test real-time message sending, user join/leave behavior, and data
storage.
---------------------------------------
app.use(cors());
app.use(express.json());
mongoose.connect('mongodb://localhost/chatDB');
username: String,
text: String,
room: String,
timestamp: Date,
});
socket.join(room);
const message = new Message({ username, text: msg, room, timestamp: new Date() });
await message.save();
});
});
});
-----------------------------------
useEffect(() => {
socket.emit('chatMessage', message);
setMessage('');
};
return (
<div>
<div>
{messages.map((msg, i) => (
))}
</div>
<button onClick={sendMessage}>Send</button>
</div>
);
OUTPUT
RESULT
A real-time chat application was successfully built using the MERN stack and Socket.IO. It supports
multiple users, instant communication, chat room features, and persistent message storage.