dongseshu0698 2014-10-03 04:06
浏览 117

RabbitMQ PHP用于动态消息更新

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

  • 写回答

1条回答 默认 最新

  • douyinghuo8874 2014-10-03 09:02
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件