Search code examples
rabbitmqstompjs

RabbitMQ StompJs recieving just one Message


I'm trying to recieve a Message from the RabbitMq-Que with the Stomp-plugin. This works fine but the Problem is that I get every Message from the Queue. So if the Queue has 13 Messages, I get 13. The Key is that I just want to get 1 Message and after sending Ack or Nack the next one. Do somebody got any Idea how to get only one Message? Thanks for Help.

Here the code I got:

    GetMessage()
  {
    this.GetRabbitMqClient().then((client)=> 
    {

    var headers ={ack:'client', 'x-max-priority': '10'};
    var subscription = client.subscribe("/queue/TestQue",this.messageCallback,headers);    
  });
  }

private messageCallback = function(Message :IMessage)
  {
    console.log(Message.body);
    setTimeout(()=> {Message.ack();},100000000000000);        
  }

 private GetRabbitMqClient( ):Promise<Client> {
    var promise = new Promise<Client>((resolve,reject)=>{
    var client = new Client(
      {
        brokerURL: "ws://localhost:15674/ws",
        connectHeaders:
        {
          login: "guest",
          passcode: "guest"
        },
        // debug: function (str) {
        //   console.log(str);
        // },
        reconnectDelay: 5000,
        heartbeatIncoming: 4000,
        heartbeatOutgoing: 4000
      });

    client.onConnect = function (frame) {   
      resolve(client);
    };

    client.onStompError = function (frame) {
      // Will be invoked in case of error encountered at Broker
      // Bad login/passcode typically will cause an error
      // Complaint brokers will set `message` header with a brief message. Body may contain details.
      // Compliant brokers will terminate the connection after any error
      reject(frame);
      console.log('Broker reported error: ' + frame.headers['message']);
      console.log('Additional details: ' + frame.body);
    };
    client.activate();    
  });
  return promise;
  }

Solution

  • I found the solution: you just need to set in the headers the attribute: 'prefetch-count':'1'