title: Amqp, rabbit mq and socket.io reconnect to a queue even if client is closed
body: <p>I'm coding a browser notification using rabbitMQ and socket.io. My configuration is working fine except for one case.</p>
  
  <p>When i login to my system with a user it creates a notification-UID-<em>userid</em> queue (For now the queueName is sent by query oaraeter, i will implement more sofisticated method as soon as i will solve the problem)</p>
  
  <p>If i login with another user on another browser it creates another queue notification-UID-<em>seconduserid</em>. </p>
  
  <p>If i logout one of the user the queue will disappear (as it's not durable).</p>
  
  <p>The problem is that when i refresh or load another page on the other session it recreates the second queue even if the paramater queuename isn't sent.</p>
  
  <p><strong>server.js</strong></p>
  
  <pre><code>var amqp = require('amqp');
  var app = require('express')();
  var http = require('http').Server(app);
  var io = require('socket.io')(http);
  
  var rabbitMqConnection = null;
  var _queue = null;
  var _consumerTag = null;
  
  
  io.use(function (socket, next) {
      var handshakeData = socket.handshake;
      // Here i will implement token verification
      console.log(socket.handshake.query.queueName);
      next();
  });
  
  
  // Gets the connection event form client
  io.sockets.on('connection', function (socket) {
  
      var queueName = socket.handshake.query.queueName;
  
      console.log("Socket Connected");
  
      // Connects to rabbiMq
      rabbitMqConnection = amqp.createConnection({host: 'localhost', reconnect: false});
  
      // Update our stored tag when it changes
      rabbitMqConnection.on('tag.change', function (event) {
          if (_consumerTag === event.oldConsumerTag) {
              _consumerTag = event.consumerTag;
              // Consider unsubscribing from the old tag just in case it lingers
              _queue.unsubscribe(event.oldConsumerTag);
          }
      });
  
      // Listen for ready event
      rabbitMqConnection.on('ready', function () {
          console.log('Connected to rabbitMQ');
  
          // Listen to the queue
          rabbitMqConnection.queue(queueName, {
                  closeChannelOnUnsubscribe: true,
                  durable: false,
                  autoClose: true
              },
              function (queue) {
                  console.log('Connected to ' + queueName);
                  _queue = queue;
  
                  // Bind to the exchange
                  queue.bind('users.direct', queueName);
  
                  queue.subscribe({ack: false, prefetchCount: 1}, function (message, headers, deliveryInfo, ack) {
                      console.log("Received a message from route " + deliveryInfo.routingKey);
                      socket.emit('notification', message);
                      //ack.acknowledge();
                  }).addCallback(function (res) {
                      // Hold on to the consumer tag so we can unsubscribe later
                      _consumerTag = res.consumerTag;
                  });
              });
      });
  
  
      // Listen for disconnection
      socket.on('disconnect', function () {
          _queue.unsubscribe(_consumerTag);
          rabbitMqConnection.disconnect();
          console.log("Socket Disconnected");
      });
  
  });
  
  http.listen(8080);
  </code></pre>
  
  <p><strong>client.js</strong></p>
  
  <pre><code>var io = require('socket.io-client');
  
  $(document).ready(function () {
  
      var socket = io('http://myserver.it:8080/', {
           query:  { queueName: 'notification-UID-' + UID},
          'sync disconnect on unload': true,
          });
  
      socket.on('notification', function (data) {
          console.log(data);
      });
  })
  </code></pre>
  
  <p>Any idea?</p>