RABBITMQ Learning: (12) using RABBITMQ in the node. JS Environment

Source: Internet
Author: User
Tags rabbitmq

Learn to use. Looking for a half-day node. JS under the RABBITMQ Library, looks not to take the hand, until finally found the AMQP library, it looks good, according to the example, wrote the first RABBITMQ client.

First, install the NODE-AMQP library using NPM install AMQP--save, although he recommends using a global installation, but don't do it!

The following is the client code:

[JavaScript]View Plaincopyprint?
  1. var AMPQ = require (' amqp ');
  2. var util = require (' util ');
  3. var connection = ampq.createconnection ();
  4. var bstop = false;
  5. Connection.on (' ready ', function () {
  6. Connection.queue (' moneyqueue ', {durable: true, Autodelete: false}, function (queue) { /c4>
  7. Console.log (' Queue ' + queue.name + ' is open! ');
  8. Queue.subscribe (function (message, header, Deliveryinfo) {
  9. if (message.data) {
  10. var messagetext = message.data.toString ()
  11. Console.log (MessageText);
  12. if (MessageText = = = "Quit") bstop = true;
  13. }
  14. });
  15. });
  16. });
  17. (function keepitrunning () {
  18. if (!bstop)
  19. SetTimeout (keepitrunning, 1000);
  20. Else
  21. Connection.end ();
  22. })();


This is an echo client, which comes in, prints out, and waits silently for the next message. If the message is quit, then graceful exit ~

Where the code creates a persistent queue:moneyqueue, the queue is automatically bound to the default Exchange, which is provided by RMQ, so it's persistent, so there's no problem.

Because this is just a consumer, unable to work alone, to verify, you can send messages through the RMQ Administration page.

http://blog.csdn.net/puncha/article/details/8452017

RABBITMQ Learning: (12) using RABBITMQ in the node. JS Environment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.