Giter Site home page Giter Site logo

Comments (5)

michaelklishin avatar michaelklishin commented on May 20, 2024
  1. RabbitMQ connections are supposed to be long-lived
  2. See amqp.node port of RabbitMQ tutorials.

from amqplib.

squaremo avatar squaremo commented on May 20, 2024

Yes, as Michael says, connections are supposed to be long-lived, as are channels.

In your code above, I think you're trying to have a single exported function that will send a message, and being stymied because you have promises rather than "real" values. You can dereference a promise more than once, so the easiest way to fix it is to phrase your function like this:

var q = 'task-queue';

var channel = connection.then(function(conn) {
    var ok = conn.createChannel();
    ok = ok.then(function(ch) {
        return ch.assertQueue(q, {durable: true})
            .then(function() { return ch; }); // swap
    });
    return ok;
});

exports.sendtomq=function(msg) {
    channel.then(function(ch) {
        ch.sendToQueue(q, new Buffer(msg), {persistent:true});
    });
};

The only tricky bit really is the swapping the assertQueue result for the channel itself (marked // swap), which I need to do because I want the channel value, but to synchronise on the queue being declared.

from amqplib.

jbasttdi avatar jbasttdi commented on May 20, 2024

Wow!thank you very much! what you said is really I want. After using your code,I can send my real message well.
Thanks again!

from amqplib.

jbasttdi avatar jbasttdi commented on May 20, 2024

Sorry,I have just a question.Need I to close the channel after sending the message?

exports.sendtomq=function(msg) {
        channel.then(function(ch) {
           ch.sendToQueue(q, new Buffer(msg), {persistent:true});
          }).ensure(function(){
              channel.close();  //? 
        });
 };

from amqplib.

squaremo avatar squaremo commented on May 20, 2024

Need I to close the channel after sending the message?

No; in fact you don't want to, usually, because it will stop you sending more messages with the channel. The original example code closes the channel because it's only sending one message before exiting.

from amqplib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.