Search code examples
phprabbitmqamqpphp-amqplib

RabbitMq: Replace duplicated messages


I'm using RabbitMq for submitting data for registered web hooks.

base info: If a contact is created in the system, the message is put in the queue and the consumer is sending later the hook data to the registered url.

To my question: Its possible, that a contact is updated in 5 seconds twice and that both messages are still in the queue. But I'd like, if the second message is queued, that the first message will be removed.

I know that i can't delete the message manually. But is it somehow possible to set an id on the message and if two messages with the same id are in the same queue, that the first one is automatically removed/replaced? That only one request is sent to the url. I know you can set a message id on the message self. But I have nothing found to replace the old one.

My PHP Code (simplified):

    $connection = new AMQPConnection('localhost', 5672, 'test', 'test');
    $channel = $connection->channel();
    $channel->queue_declare(self::QUEUE_NAME, false, true, false, false);

    $data = array(
        'model' => get_class($subject),
        'id' => $subject->getId(),
        'event' => $event->getName()
    );
    $messageProperties = array(
        'message_id' => get_class($subject) . '-' . $subject->getId()
    );
    $channel->basic_publish(new AMQPMessage(json_encode($data), $messageProperties), '', self::QUEUE_NAME);

    $channel->close();
    $connection->close();

Btw i'm using the the php amqplib https://github.com/videlalvaro/php-amqplib.

Thanks for the help Flo


Solution

  • RabbitMQ doesn't delete/filter messages that way. You have to do that at the app level, probably using something like a bloom filter.