RabbitMQ
JavaScript
Message Queues
Web Development
Programming Tips

how to connect to rabbitmq using javascript without nodejs

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Connecting to RabbitMQ using JavaScript typically involves using libraries that support AMQP protocols. In browser environments, without Node.js, the connectivity poses some challenges due to the lack of direct AMQP support and security restrictions imposed by most browsers. Below we explore a practical solution using WebSockets and a bridge technology such as Web-Stomp.

Understanding RabbitMQ and AMQP

RabbitMQ is a widely-used open-source message broker that supports various messaging protocols, one of which is AMQP (Advanced Message Queuing Protocol). AMQP is the core protocol used by RabbitMQ for messaging.

The Challenge with Browsers

Browsers do not natively support AMQP. Furthermore, browsers impose security limitations that restrict certain types of network connections, which makes direct communication with RabbitMQ servers using standard AMQP complicated. To circumvent these restrictions, we can use WebSocket as a means to interact with RabbitMQ.

Using Web-Stomp

Stomp is a simple text-oriented messaging protocol. RabbitMQ supports Stomp via plugins, which translates Stomp messages to AMQP. Using the Web-Stomp plugin, RabbitMQ can accept connections over WebSockets, allowing web applications to communicate with RabbitMQ using Stomp over WebSockets.

Setting Up RabbitMQ for Web-Stomp

  1. Enable the Web-Stomp Plugin Firstly, make sure your RabbitMQ server has the Web-Stomp plugin enabled. You can enable this plugin by running the following command on the server:
bash
   rabbitmq-plugins enable rabbitmq_web_stomp
  1. Configure RabbitMQ for Web-Stomp By default, the Web-Stomp server runs on port 15674. You can configure it in the RabbitMQ configuration file (often located under /etc/rabbitmq/rabbitmq.conf):
plaintext
   web_stomp.port = 15674
   web_stomp.ws_frame_size = 4096

Connecting from the Browser

To connect to RabbitMQ from the browser, you'll need a JavaScript client that supports Stomp over WebSockets. One popular choice is stomp.js.

  1. Include Stomp.js in Your HTML Page
html
   <script src="https://cdn.jsdelivr.net/npm/@stomp/stompjs"></script>
  1. Connect to RabbitMQ Using Stomp.js
    Here’s an example of how to use stomp.js to connect to RabbitMQ:
javascript
1   var client = Stomp.client('ws://localhost:15674/ws');
2
3   // Callback for when the connection is established
4   function connectedCallback() {
5     console.log('Connected to RabbitMQ Web-Stomp');
6     // Subscribe to a queue
7     client.subscribe('/queue/test', function(message) {
8       alert("Received message: " + message.body);
9     });
10
11     // Send a message
12     client.send('/queue/test', {}, 'Hello, RabbitMQ!');
13   }
14
15   // Connect with login and passcode
16   client.connect('guest', 'guest', connectedCallback, function(error) {
17     console.log('Connection error: ' + error);
18   });

Key Summary Points

Here is a summary table highlighting the key points of connecting to RabbitMQ from JavaScript without Node.js:

ElementDescription
RabbitMQMessage broker that supports various protocols including AMQP, MQTT, and Stomp.
AMQPAdvanced Message Queuing Protocol, not directly supported in browsers.
Web-StompEnables connecting to RabbitMQ via the Stomp protocol over WebSockets.
Stomp.jsJavaScript library that enables Stomp protocol communication over WebSockets.

Additional Considerations

  • Security: Always use secured versions of WebSockets (wss://) in production environments to encrypt communication.
  • Reconnection Strategy: Implement strategies in your client application to handle disconnects and retries.
  • Performance: Be aware of the limitations of using WebSockets and RabbitMQ. Tune your configuration based on your specific performance needs.

By leveraging Web-Stomp and stomp.js, you can effectively connect to a RabbitMQ server from a JavaScript client running in a browser without relying on Node.js or other server-side technologies.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.