Using Message Queues in JavaScript with Node.js and RabbitMQ

1. If you have not already done so, download and install Node.js. Details are given in a previous post. Make sure that the directory containing the executable is present in the operating system’s PATH environment variable.

2. Download and install Erlang. Erlang is the programming language in which (for some reason) the RabbitMQ message broker is implemented. As of this writing, the installer for Erlang is available at the URL “http://www.erlang.org/download.html“. If you are using Windows, click one of the “Windows Binary File” links on that page. (UPDATE 2015/04/22 – According to one of the commenters, this step may no longer be necessary, as Erlang is now supposedly included in the RabbitMQ install. I have not verified this independently.)

3. Download and install RabbitMQ. RabbitMQ is an open-source program that implements the Advanced Message Queuing Protocol (AMQP). As of this writing, the latest version of the RabbitMQ Windows installer is available at the URL “http://www.rabbitmq.com/install-windows.html“.

4. In any convenient location, create a new directory named “NodeAMQPTest”.

5. Open a command prompt window, navigate to the new NodeAMQPTest directory, and install the “amqp” package for Node.js by entering the command “npm install amqp”. Wait for the package to completely install.

6. In the newly created NodeAMQPTest directory, create a new text file named “Producer.js”, containing the following text. Note that this code is adapted from a similar program found at the URL “https://github.com/rabbitmq/rabbitmq-tutorials/tree/master/javascript-nodejs“.

// Adapted from:
// https://github.com/rabbitmq/rabbitmq-tutorials/blob/master/javascript-nodejs/send.js

var amqp = require('amqp');

var connection = amqp.createConnection
(
	{host: 'localhost'}
);

connection.on
(
	'ready', 
	function()
	{
		var messageToSend = "Hello, MessageQueue!";
		var queueToSendTo = "testMessageQueue";

		connection.publish
		(
			queueToSendTo, 
			messageToSend
		);

		console.log
		(
			"Sent message: "
			+ messageToSend
		);
	}
);

7. Still in the NodeAMQPTest directory, create a new text file named “Consumer.js”, containing the following text. Note that this code is adapted from a similar program found at the URL “https://github.com/rabbitmq/rabbitmq-tutorials/tree/master/javascript-nodejs“.

// Adapted from:
// https://github.com/rabbitmq/rabbitmq-tutorials/blob/master/javascript-nodejs/receive.js

var amqp = require('amqp');

var connection = amqp.createConnection
(
	{host: 'localhost'}
);

var queueToReceiveFrom = "testMessageQueue";

connection.on
(
	'ready', 
	function()
	{
		connection.queue
		(
			queueToReceiveFrom, 
			{autoDelete: false}, 
			function(queue)
			{
				console.log('Waiting for messages...');
				queue.subscribe
				(
					function(messageReceived)
					{
            					console.log
						(
							"Received message: " 
							+ messageReceived.data.toString()
						);
					}
				);
			}
		);
	}
);

8. Still in the NodeAMQPTest directory, create a new text file named “Producer-Run.bat”, containing the following text.

node Producer.js

9. Still in the NodeAMQPTest directory, create a new text file named “Consumer-Run.bat”, containing the following text.

node Consumer.js

10. Execute Consumer-Run.bat. A console window will appear, and a message that says “Waiting for messages…” will appear within it.

11. Execute Producer-Run.bat. A console window will appear, and a message that says “Sent message: Hello, MessageQueue!” will appear within it. Back in the console window for “Consumer-Run.bat”, another message that says “Received message: Hello, MessageQueue!” will appear.

This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

6 Responses to Using Message Queues in JavaScript with Node.js and RabbitMQ

  1. samshiles says:

    Perfect, just what I was looking for. This works equally well on MAC (minus the bat files). Just a note for other finding this. RMQ now includes the Erlang runtime so no need to install manually. Also, mac users, make sure that your host name (computer name) has been add to your host file. See the following: http://codingiscoding.wordpress.com/2011/04/17/rabbitmq-on-os-x/

  2. Luciano says:

    Finally a clear tutorial! Thanks

  3. Martin says:

    Worked like a charm 🙂 Thanks for a great tutorial!

  4. Roberta says:

    Thanks so much!

  5. I have a question: How do you get these things to stop without using a keyboard? I want the bat files to run and exit.

  6. How would you terminate these bat files without using a keyboard? They just sit there after they are done.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s