phprabbitmqamqpphp-amqplib

How to return response from RabbitMQ to browser


I am trying to streamline my app to reduce server load by sending complex database tasks to a RabbitMQ queue for processing by a worker dyno.

For INSERTs and UPDATEs, this has been relatively straightforward. However, I have a couple of intensive SELECTs which I would to send to the queue. The problem I'm having, however, is how to return the response to the user's browser when it's ready.

I understand I should be sending the response to a second queue, and creating a consumer in the client's browser. However, I'm hitting some practical problems in doing this, such as how to select the correct response from the queue, rather than simply the next response (e.g. if two people were on the page at once).

Does anybody have any experience or advice?

Thanks


Solution

  • In the end, I used a different method than that described by @IMSoP; I would be interested to hear any views on which is more efficient or considered better practice.

    At the end of the queued task, a JSON response is pushed onto a Redis store (the key is the unique user id and the value is the JSON response). In the browser, AJAX is used to run a PHP file every two seconds, with this file checking if the Redis store has a key matching the user id yet. Once found, it returns the value to the browser, then deletes the key=>value pair on Redis.