Rabbit-MQ
React
JavaScript
Web Development
Messaging Middleware

Using RabbitMQ with React

Codemia Knowledge Hub ·

React should talk to your backend over HTTP or WebSocket, and the backend should publish to and consume from RabbitMQ. RabbitMQ is a backend message broker that speaks AMQP, and connecting a browser to it directly is the wrong architecture for both security and protocol reasons. This setup gives the browser real-time updates without exposing the broker to untrusted clients.

The three parts

  • React frontend for UI
  • backend service for authentication, business logic, and WebSocket or HTTP endpoints
  • RabbitMQ broker for queueing messages between backend components

React is not a consumer in the queueing sense. Your backend services are the producers and consumers; React is the user-facing client.

Publishing to RabbitMQ from a backend

A minimal Node.js publisher using amqplib:

javascript
1const amqp = require('amqplib');
2
3async function publishOrder(order) {
4  const connection = await amqp.connect('amqp://localhost');
5  const channel = await connection.createChannel();
6  const queue = 'orders';
7
8  await channel.assertQueue(queue, { durable: true });
9  channel.sendToQueue(queue, Buffer.from(JSON.stringify(order)), {
10    persistent: true,
11  });
12
13  await channel.close();
14  await connection.close();
15}
16
17publishOrder({ id: 1, status: 'created' }).catch(console.error);

This code belongs on the server, not in the React app. It opens and closes a connection per call for brevity; in production, reuse connections and channels carefully.

Pushing broker events to React over WebSocket

To update the UI when messages are processed, bridge RabbitMQ events to the browser with WebSocket or Server-Sent Events. A small WebSocket bridge:

javascript
1const amqp = require('amqplib');
2const WebSocket = require('ws');
3
4async function start() {
5  const wss = new WebSocket.Server({ port: 8080 });
6  const connection = await amqp.connect('amqp://localhost');
7  const channel = await connection.createChannel();
8  const queue = 'notifications';
9
10  await channel.assertQueue(queue, { durable: false });
11
12  channel.consume(queue, (msg) => {
13    if (!msg) return;
14    const payload = msg.content.toString();
15    wss.clients.forEach((client) => {
16      if (client.readyState === WebSocket.OPEN) {
17        client.send(payload);
18      }
19    });
20    channel.ack(msg);
21  });
22}
23
24start().catch(console.error);

The backend consumes RabbitMQ messages and forwards them to connected browsers. WebSocket is the browser-facing transport; RabbitMQ stays the backend messaging layer.

Receiving updates in React

The React side only connects to the backend WebSocket endpoint.

javascript
1import { useEffect, useState } from 'react';
2
3export default function App() {
4  const [messages, setMessages] = useState([]);
5
6  useEffect(() => {
7    const socket = new WebSocket('ws://localhost:8080');
8
9    socket.onmessage = (event) => {
10      const payload = JSON.parse(event.data);
11      setMessages((prev) => [...prev, payload]);
12    };
13
14    return () => socket.close();
15  }, []);
16
17  return (
18    <ul>
19      {messages.map((msg, index) => (
20        <li key={index}>{msg.status}</li>
21      ))}
22    </ul>
23  );
24}

The user gets near-real-time updates while RabbitMQ stays inside the backend boundary. The browser wants pushed state changes, not raw broker behavior.

Why not connect the browser to RabbitMQ directly

Even if you find a way to expose RabbitMQ to the browser, it is usually a bad idea:

  • credentials would be exposed to frontend clients
  • the broker becomes part of the public attack surface
  • browser networking constraints are a poor fit for AMQP workflows
  • authorization and message filtering are harder to control

Your backend should own queue access and translate events into client-safe APIs.

Where this pattern fits

  • job progress dashboards
  • notifications
  • order-processing status updates
  • chat or support systems with backend workers
  • event-driven admin panels

In each case RabbitMQ decouples backend components while React provides the interface.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms