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