How to Use WebSockets for Real-Time Communication in a JavaScript App with Express and React

Jul 6, 2024·

3 min read

How to Use WebSockets for Real-Time Communication in a JavaScript App with Express and React

In the era of real-time applications, having a seamless and efficient communication channel between the server and the client is crucial. WebSockets provide a powerful solution for real-time, bidirectional communication. This article will guide you through setting up a WebSocket server using Express and integrating it with a React frontend.

What Are WebSockets?

WebSockets are a protocol that enables interactive communication between a browser and a server. Unlike HTTP, WebSockets provide full-duplex communication channels over a single TCP connection. This means that the server can send messages to the client without the client having to request them first, making it ideal for real-time applications like chat applications, live notifications, and online gaming.

Setting Up the WebSocket Server with Express

First, let's set up a simple Express server and integrate WebSockets using the ws library.

Step 1: Initialize Your Project

Start by creating a new project directory and initializing it with npm:

mkdir websocket-demo
cd websocket-demo
npm init -y

Step 2: Install Necessary Packages

Install Express and the ws library:

npm install express ws

Step 3: Create the Server

Create a file named server.js and add the following code:

const express = require('express');
const http = require('http');
const WebSocket = require('ws');

const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });

wss.on('connection', (ws) => {
    console.log('New client connected');

    ws.on('message', (message) => {
        console.log(`Received: ${message}`);

        // Echo the message back to the client
        ws.send(`Server says: ${message}`);
    });

    ws.on('close', () => {
        console.log('Client disconnected');
    });
});

app.get('/', (req, res) => {
    res.send('WebSocket server is running');
});

const PORT = process.env.PORT || 8080;
server.listen(PORT, () => {
    console.log(`Server is listening on port ${PORT}`);
});

This code sets up an Express server with a WebSocket server attached to it. When a new client connects, it logs the connection and sets up listeners for incoming messages and disconnections.

Creating the React Frontend

Now, let's create a simple React application that connects to our WebSocket server.

Step 1: Set Up the React App

If you don't already have create-react-app installed, you can install it globally:

npm install -g create-react-app

Create a new React application:

npx create-react-app websocket-client
cd websocket-client

Step 2: Create the WebSocket Component

Inside the src directory, create a new file named WebSocketComponent.js and add the following code:

import React, { useState, useEffect } from 'react';

const WebSocketComponent = () => {
    const [socket, setSocket] = useState(null);
    const [message, setMessage] = useState('');
    const [messages, setMessages] = useState([]);

    useEffect(() => {
        const socket = new WebSocket('ws://localhost:8080');

        socket.onopen = () => {
            console.log('WebSocket connection established');
        };

        socket.onmessage = (event) => {
            setMessages((prevMessages) => [...prevMessages, event.data]);
        };

        socket.onclose = () => {
            console.log('WebSocket connection closed');
        };

        setSocket(socket);

        return () => {
            socket.close();
        };
    }, []);

    const sendMessage = () => {
        if (socket) {
            socket.send(message);
            setMessage('');
        }
    };

    return (
        <div>
            <h1>WebSocket Chat</h1>
            <input 
                type="text" 
                value={message} 
                onChange={(e) => setMessage(e.target.value)} 
                placeholder="Type a message..."
            />
            <button onClick={sendMessage}>Send</button>
            <div>
                <h2>Messages:</h2>
                {messages.map((msg, index) => (
                    <p key={index}>{msg}</p>
                ))}
            </div>
        </div>
    );
};

export default WebSocketComponent;

This component establishes a WebSocket connection to the server when it mounts and sets up handlers for opening, receiving messages, and closing the connection. It also allows the user to send messages to the server.

Step 3: Integrate the Component

Finally, integrate the WebSocketComponent into your React app. Modify src/App.js to include the component:

import React from 'react';
import WebSocketComponent from './WebSocketComponent';

function App() {
    return (
        <div className="App">
            <WebSocketComponent />
        </div>
    );
}

export default App;

Step 4: Run the React App

Run your React app:

npm start

Your React application should now be able to connect to the WebSocket server, send messages, and display messages received from the server.