RabbitMQ Node JS Validate User ID

712 views Asked by At

I use RabbitMQ, SocketIO and MongoDB to make private messages for my app. The plan is when new user register, app make new unique RabbitMQ Queue for that user, with that user is able to get messages when it is offline. So user send message through SocketIO and it is passed to RabbitMQ Publisher and then when consumer is online he get that message. My questions now is how to set Validate User ID from sendToQueue (Publisher) function to be able later to read sender ID from consume function?

amqp.connect(CONN_URL, function(err, conn) {
  conn.createChannel(function(err, channel) {
   ch = channel;
   console.log("RabbitMQ channel created...");
  });
});


const publishToQueue = async (queueName, data) => {
  ch.sendToQueue(queueName, Buffer.from(data.message));
};

const consumeToQueue = async queueName => {
  ch.consume(
    queueName,
    function(msg) {
      return msg.content.toString();
    },
    { noAck: true }
  );
};
1

There are 1 answers

0
Uros Kalajdzic On

I worked out...

const publishToQueue = async (queueName, data) => {
  let properties = { headers: {userId: data.to }, timestamp: Date.now() }; 
  ch.sendToQueue(queueName, Buffer.from(data.message), properties);
};

This will send headers with userId information and also timestamp of message