1

I have a following scenario. I want to implement pub/sub model of rabbitmq using amqp client. I have subscribers who subscribe for a particular id. I want to form a queue on teh name of that id. So that all the subscribers who want to subscribe to that particular id should subscribe to that particular queue. So i want to form a unique queue based on a particular id for all the subscribers with that id. But in my case it is forming different queues for same id (i guess). As a result, subscribers of a particular id are able to receive all the messages in round robin manner, inspite of all teh subscribers receiving all the messages. Below is the code i m using.

var express = require('express');
var amqp = require('amqp');
var app = express();
var httpServer = require('http').createServer();
var socketio = require('socket.io');
var io = socketio.listen(httpServer);
httpServer.listen(8000);

console.log("server started at port no 8000");

var rabbitConnection = amqp.createConnection({host:"localhost", port:"5672"});
var chatExchange;
rabbitConnection.on("ready", function()
{
 console.log("RabbitConnection got ready");
 chatExchange = rabbitConnection.exchange("chatExchange", {"type":"topic"});
 //console.log("value of chatExchange is :",chatExchange);
});

io.sockets.on("connection", function(socket)
{
 console.log("socket connected");
 socket.on("metaData", function(data)
 {
  if(data.type=="publisher")
  {
   console.log("publisher connected");
   socket.type="publisher";
  }
  else if(data.type=="subscriber")
  {
   console.log("subscriber connected");
   socket.type="subscriber";
   rabbitConnection.queue(""+data.channelName, function(queue)  //MAIN PROBLEM LIES IN ABOVE LINE I GUESS. IT WILL FORM QUEUE FOR EVERY SOCKET CONNECTION. BUT IT SHOULD NOT FORM QUEUE IF IT ALREADY EXISTS.
   {
    console.log("queue formed");
    console.log("queue is ", queue);
    queue.bind("chatExchange",""+data.channelName);
    queue.subscribe(function(message)
    {
     console.log("message is "+message.data.toString());
     console.log("message emitted");
     socket.emit("message",{"text":message.data.toString()});
    });
   });
  }
 }); 
 socket.on("disconnect",function()
 { 
  console.log("socket disconnected");
 });
 socket.on("message", function(data)
 {
  chatExchange.publish(""+data.channelName, data.text);
  console.log("message came");
 });
})

I have seen documentation of node-amqp module. But i didnot find any option to chek the existence of queue. some 'persistence' option was there. But i dont thing it will work. Please help.... Thanks in advance..

Ankush Sharma
  • 71
  • 1
  • 4
  • 1
    IMPROVEMENT:-The flag is 'passive' instead of 'persistence'. Thanku... – Ankush Sharma Jun 02 '15 at 06:08
  • IMPROVEMENT 2:- I have used gui tool of rabbitmq to check that no of queues are correct. But topic exchange feature is not working. Please discuss the syntax for it... – Ankush Sharma Jun 02 '15 at 11:39

0 Answers0