Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

about long-lived connection and message to send #46

Closed
jbasttdi opened this issue Mar 5, 2014 · 5 comments
Closed

about long-lived connection and message to send #46

jbasttdi opened this issue Mar 5, 2014 · 5 comments

Comments

@jbasttdi
Copy link

jbasttdi commented Mar 5, 2014

Thanks for your great work.
I am a newbie of node & rabbitmq,now i want to use rabbitmq to send message and receive. I got your example code and updated it. I have two questions about it.
1>can i keep a long-lived connection to send message and don't close it?
2>how can i send message with my data?

senddao.js,

var amqp = require('amqplib');
var when = require('when');
var config=require('../config');
//var msgtosend='';

var     url='amqp://'+config.mq.username+':'+config.mq.password+'@'+config.mq.host+':'+config.mq.port;
var connection = amqp.connect(url);
var channelaction=function (sendchannel) {
    var q = 'task_queue';
    var ok = sendchannel.assertQueue(q, {durable: true});
    return ok.then(function () {
        var msg = "Hello World!"
      //  msg=msgtosend;  //can i do this? Isn't there any async issue?
        sendchannel.sendToQueue(q, new Buffer(msg), {deliveryMode: true});
        console.log(" [x] Sent '%s'", msg);
        return sendchannel.close();
    });
};
var channel = function (conn) {
    var trans = when(conn.createChannel().then(channelaction));
    trans.ensure(function () {
       // conn.close();
       // need to close?if i close this connection
       // ,when i need to send another msg then i need to connect the rabbitmq again.
       //So i want to keep the connection long-lived to avoid connecting again.
       //can i do this?
    });
};

exports.sendtomq=function(msg){
//    msgtosend=msg;  //if i do this,isn't there any async issue?
    //how can i send this msg to the queue instead of "Hello World!"?
    connection.then(channel).then(null, console.warn);
};
@michaelklishin
Copy link

  1. RabbitMQ connections are supposed to be long-lived
  2. See amqp.node port of RabbitMQ tutorials.

@squaremo
Copy link
Collaborator

squaremo commented Mar 5, 2014

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.

@jbasttdi
Copy link
Author

jbasttdi commented Mar 6, 2014

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!

@jbasttdi jbasttdi closed this as completed Mar 6, 2014
@jbasttdi
Copy link
Author

jbasttdi commented Mar 6, 2014

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();  //? 
        });
 };

@jbasttdi jbasttdi reopened this Mar 6, 2014
@squaremo
Copy link
Collaborator

squaremo commented Mar 6, 2014

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.

@squaremo squaremo closed this as completed Mar 6, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants