Real-Time Messenger App with Socket.io, React, and Node.js

Real-Time Messenger App with Socket.io, React, and Node.js

ยท

2 min read

Table of contents

No heading

No headings in the article.

It seems like there might be a small typo in your question. I assume you meant "Socket.io" instead of "sockit.io." If that's the case, I can guide you on how to create a basic messenger application using Socket.io with React and Node.js.

  1. Setup Node.js Server:

    Create a new Node.js project and install the necessary dependencies.

     mkdir messenger-app
     cd messenger-app
     npm init -y
    

    Install required packages:

     npm install express socket.io
    

    Create a basic server.js file:

     const express = require('express');
     const http = require('http');
     const socketIO = require('socket.io');
    
     const app = express();
     const server = http.createServer(app);
     const io = socketIO(server);
    
     io.on('connection', (socket) => {
       console.log('User connected');
    
       socket.on('disconnect', () => {
         console.log('User disconnected');
       });
    
       socket.on('chat message', (msg) => {
         io.emit('chat message', msg);
       });
     });
    
     server.listen(3001, () => {
       console.log('Server is running on port 3001');
     });
    
  2. Create React App:

    In a separate terminal window, create a React app in the same project folder:

     npx create-react-app client
    

    Navigate to the client directory:

     cd client
    
  3. Install Socket.io Client:

    Install the Socket.io client in your React app:

     npm install socket.io-client
    
  4. Update React App:

    Modify the src/App.js file in your React app to use Socket.io for messaging:

     import React, { useState, useEffect } from 'react';
     import io from 'socket.io-client';
    
     const socket = io('http://localhost:3001');
    
     function App() {
       const [messages, setMessages] = useState([]);
       const [message, setMessage] = useState('');
    
       useEffect(() => {
         socket.on('chat message', (msg) => {
           setMessages([...messages, msg]);
         });
    
         return () => {
           socket.disconnect();
         };
       }, [messages]);
    
       const sendMessage = () => {
         socket.emit('chat message', message);
         setMessage('');
       };
    
       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 App;
    
  5. Run Your Application:

    In the root directory of your project, start both the server and the React app:

     # In the root directory
     node server.js
    

    In another terminal window:

     # Navigate to the client directory
     cd client
    
     # Start the React app
     npm start
    

    Open your browser and go to http://localhost:3000. You should be able to see and send messages in real-time.

Remember that this is a basic example, and you may want to add more features and error handling as your application grows. Additionally, consider implementing user authentication and security measures in a production environment.

Did you find this article valuable?

Support Revive Coding by becoming a sponsor. Any amount is appreciated!

ย