In this tutorial, we will be creating a real-time chat application using the MERN (MongoDB, Express, React, Node.js) stack. We will start by setting up the backend with Express, creating routes and setting up a real-time chat functionality using Socket.io. Then, we will create the frontend using React to interact with the backend.
Step 1: Setting up the backend with Express
- Install Express generator:
To start, we will use the Express generator to create our backend. Install the Express generator globally using npm:
npm install -g express-generator
- Create a new Express project:
Create a new Express project using the following command:
express chat-backend
This will create a new project with the necessary Express setup.
- Install dependencies:
Navigate into the newly created project directory and install the necessary dependencies:
cd chat-backend
npm install
- Create routes for chat functionality:
Create a new file namedchatRoute.js
inside theroutes
directory. In this file, we will define the routes for our chat functionality.
const express = require('express');
const router = express.Router();
router.get('/messages', (req, res) => {
res.json({ messages: [] });
});
module.exports = router;
- Configure the Express app:
In theapp.js
file, import the chat routes file and use it in the Express app:
const chatRoute = require('./routes/chatRoute');
app.use('/chat', chatRoute);
Step 2: Setting up real-time chat functionality with Socket.io
- Install Socket.io:
Install Socket.io and Socket.io-client dependencies using npm:
npm install socket.io socket.io-client
- Configure Socket.io in Express:
In theapp.js
file, require thehttp
module and create a new instance of the HTTP server. Then, create a Socket.io instance and pass the server as an argument:
const http = require('http').createServer(app);
const io = require('socket.io')(http);
io.on('connection', (socket) => {
console.log('A user connected!');
});
http.listen(3001, () => {
console.log('Socket.io server running on port 3001');
});
- Implement real-time chat functionality:
Inside theio.on('connection')
event, we will implement the logic for handling real-time messages. Emit events for sending and receiving messages.
io.on('connection', (socket) => {
console.log('A user connected!');
socket.on('chat message', (msg) => {
io.emit('chat message', msg);
});
});
Step 3: Setting up the Frontend with React
- Create a new React project:
Create a new React project usingcreate-react-app
:
npx create-react-app chat-frontend
- Install Socket.io-client:
Install Socket.io-client in the React project:
npm install socket.io-client
- Create a chat component:
Create a new component namedChat
inside thesrc/components
directory. Implement the logic for sending and receiving messages using Socket.io-client.
import React, { useEffect, useState } from 'react';
import io from 'socket.io-client';
const socket = io('http://localhost:3001');
const Chat = () => {
const [messages, setMessages] = useState([]);
const [message, setMessage] = useState('');
useEffect(() => {
socket.on('chat message', (msg) => {
setMessages([...messages, msg]);
});
}, [messages]);
const sendMessage = () => {
socket.emit('chat message', message);
};
return (
<div>
<ul>
{messages.map((msg, index) => (
<li key={index}>{msg}</li>
))}
</ul>
<input type="text" value={message} onChange={(e) => setMessage(e.target.value)} />
<button onClick={sendMessage}>Send</button>
</div>
);
};
export default Chat;
- Render the chat component in the App component:
Import theChat
component and render it inside theApp
component in thesrc/App.js
file.
import React from 'react';
import Chat from './components/Chat';
function App() {
return (
<div>
<Chat />
</div>
);
}
export default App;
- Run the application:
Run both the Express backend and React frontend using the following commands:
For the backend:
npm start
For the frontend:
npm start
You should now have a real-time chat application up and running using the MERN stack! Users can send and receive messages in real-time using Socket.io. Happy coding!
he is provide a Quality content, but this channel is being underrated its hurt now
Why i can not access next videos
please complete your nestjs series
Anna, can you please make videos docker & kubernates