blip
blip

Reputation: 31

AMQP using Node.js, How Can I publish/subscribe?

I am making a class manipulating the Node-AMQP module available here : https://github.com/postwait/node-amqp

But I am not able to publish/subscribe using this :

  var Queue = require('./Queue.js');
  var queue = new Queue();
  queue.addTaskToQueue('salut', 5);
  queue.subscribeTaskQueue('salut');

Here is the class that I am using (I give the Code in CoffeeScript, and in Node.js for those who doesn't know CoffeeScript) :

Thanks for your help.

In CoffeeScript :

amqp = require('amqp')

class Queue

    constructor: (ip = 'localhost') ->
            @ip = ip
            @receivedObject
            @connection = amqp.createConnection({ host: @ip })
            @queue

    subscribeTaskQueue: (queueToSubscribe) ->
            self = @
            self.connection.on('ready', ->
                    q = self.connection.queue(queueToSubscribe)
                    q.bind('#')

                    q.subscribe({ ack: true }, (message) ->
                            self.receivedObject = message
                            console.log(self.receivedObject)
                    )
            )

    addTaskToQueue: (queue, objectToSend) ->
            @queue = @connection.queue("salut", { durable: true })
            @connection.publish(queue, objectToSend)

module.exports = Queue

In Node.js

(function() {
  var Queue, amqp;
  amqp = require('amqp');
  Queue = (function() {
    function Queue(ip) {
      if (ip == null) {
        ip = 'localhost';
      }
      this.ip = ip;
      this.receivedObject;
      this.connection = amqp.createConnection({
        host: this.ip
      });
      this.queue;
    }
    Queue.prototype.subscribeTaskQueue = function(queueToSubscribe) {
      var self;
      self = this;
      return self.connection.on('ready', function() {
        var q;
        q = self.connection.queue(queueToSubscribe);
        q.bind('#');
        return q.subscribe({
          ack: true
        }, function(message) {
          self.receivedObject = message;
          return console.log(self.receivedObject);
        });
      });
    };
    Queue.prototype.addTaskToQueue = function(queue, objectToSend) {
      this.queue = this.connection.queue("salut", {
        durable: true
      });
      return this.connection.publish(queue, objectToSend);
    };
    return Queue;
  })();
  module.exports = Queue;
}).call(this);

I did what you say + I made little modifications.

Here is my class Queue.coffee :

amqp = require('amqp')

class Queue

    constructor: (ip = "localhost", queueName = "salut") ->
            @ip = ip
            @receivedObject = "test"
            @connection = amqp.createConnection({ host: 'localhost' })
            @queueName = queueName

    subscribeTaskQueue: () ->
            @connection.on('ready', ->
                    q = @connection.queue(@queueName)
                    q.bind('#')

                    q.subscribe({ ack: true }, (message) ->
                            @receivedObject = message
                            console.log(@receivedObject)
                    )
            )

    addTaskToQueue: (objectToSend = "hello") ->
            @connection.publish(@queueName, objectToSend)


queue = new Queue("localhost", "salut")

queue.connection.on 'ready', ->
  queue.addTaskToQueue 'salut', 5
  queue.subscribeTaskQueue 'salut'

And here is rabbituser.coffee :

Queue = require('./Queue.js')
queue = new Queue("localhost", "salut")

queue.addTaskToQueue("hello")
queue.subscribeTaskQueue()

When I do the command : node rabbituser.js

I get :

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
TypeError: Cannot read property '' of undefined
    at Connection.exchange (/home/armand/node_modules/amqp/amqp.js:1242:21)
    at Connection.publish (/home/armand/node_modules/amqp/amqp.js:1258:60)
    at Queue.addTaskToQueue (/home/armand/Desktop/RockSolidProject/coucheAMQP/Queue.js:36:30)
    at Object.<anonymous> (/home/armand/Desktop/RockSolidProject/coucheAMQP/rabbituser.js:5:9)
    at Object.<anonymous> (/home/armand/Desktop/RockSolidProject/coucheAMQP/rabbituser.js:7:4)
    at Module._compile (module.js:402:26)
    at Object..js (module.js:408:10)
    at Module.load (module.js:334:31)
    at Function._load (module.js:293:12)
    at Array.<anonymous> (module.js:421:10)

Upvotes: 2

Views: 3485

Answers (2)

Ivan Fraixedes
Ivan Fraixedes

Reputation: 550

To afford to use amqp as publish/subscriber, right now it is available rabbitmq-nodejs-client

Upvotes: 0

Trevor Burnham
Trevor Burnham

Reputation: 77416

I'm not sure what the error you're encountering is, but I will point out a few things:

  1. In your constructor, when you write @receivedObject and @queue—that doesn't do anything. In JavaScript, every object is a hash, and you can attach properties at any time; if @receivedObject and @queue are initially undefined in a Queue instance, then you don't have to define them in the constructor.

  2. Is it possible that the problem is that you're calling @connection.queue in addTaskToQueue before the connection exists (that is, before the self.connection.on 'ready' callback)?

Perhaps if you changed your code to

queue = new Queue()
queue.connection.on 'ready', ->
  queue.addTaskToQueue 'salut', 5
  queue.subscribeTaskQueue 'salut'

If that doesn't solve your problem, could you please describe the precise error you're encountering and where you're encountering it?

Upvotes: 1

Related Questions