0

I am learning RabbitMQ and thought of using it for giving "dynamic message" updates to users very much like facebook give dynamic feeds from friends.

My Idea was :

  1. Whenever a user is created I will create a queue having name is user's userId so queue name could be "100_message_queue" (userId_message_queue).

  2. Producer will push all updates in this queue.

  3. From client side (javascript) , it will call a REST API like "GET http://example.com/getliveupdates/100" , then I will fetch all new updates from 100_message_queue and send it as response.

I read RabbitMQ php tutorials but cannot figure out how this is possible ? Moreover consumer runs forever so its seems I cannot make any REST request. It is giving me timeout.

Any idea how to implement this kind of structure ?

Thanks

pinepain
  • 12,453
  • 3
  • 60
  • 65
voila
  • 1,594
  • 2
  • 19
  • 38

1 Answers1

1

As you planning to deliver that messages to the web client, I would recommend to look on MQTT and STOMP with Web STOMP RabbitMQ plugins. For you it should be perfect solution to employ their power over WebSocket. And it gives you realtime messaging, which is always a pro and probably what you want.

As to dealing with running forever consumers:

If you are using php-amqp extension you can set read_timeout option to some small values, say 1 (sec), so when consumer get all messages from queue it will wait for 1 sec. more for new messages and then throw an exception (I guess AMQPConnectionException, ugly solution, but this is how it's done for now).

Alternatively, you can AMQPQueue::get messages from queue until there will be no messages left.

With php-amqplib things should be the same, at least idea still the same: limit consumer to wait for new messages by time or get messages from queue in iterative manner.

pinepain
  • 12,453
  • 3
  • 60
  • 65