nodejs/javascript client for Rabbitmq

In previous section, we have created our node project, Now we will discuss development of rabbitmq-nodejs-client.

rabbitmq-nodejs-client project helps you to write nodejs-client for rabbitmq connection.

Requirements:
  • Running RabbitMQ server
  • nodejs
  • amqp.node

To install amqp.node using npm:
npm install amqplib
Development:

`touch index.js` will create a index.js file, and update file with following code,
var amqp = require('amqplib/callback_api');
console.log("amqp", amqp);
and run index.js as,
node index.js
if your output looks like,
amqp { connect: [Function: connect],
  credentials: { plain: [Function], amqplain: [Function], external: [Function] },
  IllegalOperationError: { [Function: IllegalOperationError] super_: { [Function: Error] stackTraceLimit: 16 } } }
then you have ready to start development, if you got error like `Error: Cannot find module 'amqplib/callback_api'` then install amqplib globally like,
npm install amqplib -g
Update your index.js file like,
//To create a connection object
var open = require('amqplib').connect('amqp://localhost');
var q = 'tasks';
open.then(function(conn) {
    //To create a channel
    return conn.createChannel();
})
.then(function(ch) {
    // creates queue
    return ch.assertQueue(q).then(function(ok) {
        // message consumer
        return ch.consume(q, function(msg) {
            if (msg !== null) {
                console.log(msg.content.toString());
                ch.ack(msg);
            }
        });
    });
})
.catch(console.warn);
For updated working code, look into the github project https://github.com/hmudimi/rabbitmq-nodejs-client.

Comments

Popular posts from this blog

conditional subschemas (writing conditions inside a JSON Schema)

JSON Schema validation keywords with more explanation