RabbitMQ 与 Websocket 和 Gevent
我期待使用 Websocket 为我的 Web 应用程序开发实时 API。为此,我使用 RabbitMQ 作为代理,我的后端基于 python (gevent + websocket),Pika/Puka 作为rabbitmq 客户端。
我在这里面临的问题是,我们如何使用websocket与rabbitMQ连接。初始websocket连接建立后,socket对象等待来自客户端的新消息,对于rabbitMQ,我们需要为其设置一个消费者,因此它会在收到消息时处理该消息。我们可以这样理解,
- 客户端通过全双工 websocket 与服务器建立连接。
- 在初始 websocket 握手后,所有客户端都应充当 RabbitMQ 的消费者,因此当客户端收到某些消息时,它们都会获得更新。
- 当新消息到达websocket时,该客户端会将其发送到RabbitMQ,因此此时该客户端充当发布者。
问题是 Websocket 等待新消息,而 RabbitMQ 消费者在其通道上等待新消息,我无法链接这两种情况。
我不确定这是否是一个错误的方法......
我无法找到实现这种情况的方法。如果我走错了路或者有任何替代方法吗?请帮我解决这个问题。
谢谢你,
哈里达斯·N.
I'm looking forward to develop a realtime API for my web application using Websocket. For this I'm using RabbitMQ as the broker and My backend is based on python (gevent + websocket),and Pika/Puka as rabbitmq client.
The problem I'm facing here is that, how we can use websocket to connect with rabbitMQ. After the initial websocket connection establishment, the socket object wait for new messages from client, and in the case of rabbitMQ, we need to setup a consumer for it, so it will process the message when it receive one. We can take this in this way,
- Clients are established connection with server via full-duplex websocket.
- All clients should act as RabbitMQ's consumer after initial websocket handshake, so they all get updates when a client gets some message.
- When new message arrives at websocket, that client will send it to RabbitMQ, so at this time this client act as publisher.
The problem is Websocket wait for a new message, and the RabbitMQ consumer wait for new message on its channel, I'm failed to link these two cases.
I'm not sure whether this is a wrong method ...
I'm unable to find a method to implement this scenario.If I'm going wrong way or is there any alternate method ?, please help me to fix this.
Thank you,
Haridas N.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我用 Tornado + websocket + RabbitMQ + Pika 实现了类似的要求。
我认为这是已知的方法。这是我的这个网络聊天应用程序的 git 存储库。
https://github.com/haridas/RabbitChat
与 gevent/twisted 类似的事情似乎很难因为rabbitMQ客户端不支持gevent/twisted的事件循环。
鼠兔有龙卷风适配器,因此设置起来很容易。 Pika 开发团队也在开发扭曲适配器。我希望他们很快就会发布它。
谢谢,
Haridas N.
http://haridas.in。
I implemented similar requirement with Tornado + websocket + RabbitMQ + Pika.
I think this were already known method. Here is my git repo for this web chat application.
https://github.com/haridas/RabbitChat
It seems very difficult to the similar thing with gevent/twisted because the rabbitMQ clients couldn't supporting the event loops of gevent/twisted.
The pika has tornado adapter, so that makes this easy to setup. Pika development team working on the twisted adapter also. I hope they will release it very soon.
Thanks,
Haridas N.
http://haridas.in.
一个简单的解决方案是使用 gevent.queue.Queue 实例进行 greenlet 间的通信。
A simple solution would be to use gevent.queue.Queue instances for inter-greenlet communication.