使用Socket IO(或Strophe.js),XMPP和Django实时服务器推送 [英] Realtime server push with Socket IO (or Strophe.js), XMPP and Django

查看:226
本文介绍了使用Socket IO(或Strophe.js),XMPP和Django实时服务器推送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个Android和iOS本地移动应用程序,我写的是直接连接到我主持的XMPP服务器。他们通过XMPP推拉实时数据。我还使用一些 XMPP XEP扩展程序。对于其他操作,我有一个django应用程序运行在所有移动应用程序通过HTTP REST接口使用的同一台服务器上。我使用Celery和Redis进行django方面的异步执行某些操作(比如对我的数据库进行大量的批处理)。



这一切都很好,很漂亮。 Yay。



但是现在我想为所有这些写一个Web前端,所以我开始研究我的选择,很好 - 有很多方法来皮肤我想要与SO社区检查的猫。



有一个js库的想法给我一个统一的API用于套接字通信(即尝试不同的web实现套接字或回退闪光)吸引我,所以我提到Socket IO。要运行一个nodejs服务器的想法好,不是太多了(还有一件事要学习),但如果我必须,我一定会。我知道有些人使用 gevent as a替换节点服务器。其他人决定写一个他们连接到其余部分的小节点他们的堆栈。我可能会这样做。



另一个选择是使用像 Strophe ,我不认为有闪回的回退。此外,我需要研究这对我的服务器意味着什么。



我已经阅读了几个关于如何做彗星和django的Stackoverflow答案 - 因此为什么似乎有几个选项。



问题是:



如果我想拥有套接字IO行为(带回退),我想将实时数据推送到Web客户端(通过XMPP将其提供给服务器),并使用Django我最好的选择是什么?



更新:我使用的XMPP服务器是 ejabberd ,它还支持 BOSH 。我意识到我可以使用 Strophe.js ,因此我的通信将通过一种类型的长轮询http连接而不是websockets。据我所知,有一些在Websockets上的XMPP打开源库,但AFAIK社区并不像SocketIO那样活跃。



更新2:我需要的浏览器支持只是现代浏览器。我想这意味着Flash的回退不会那么重要,这正在倾向于strophe.js。

解决方案

不知道为什么如果您要做BOSH,则需要Flash后备( XEP-0124 XEP-0206 ),这是strophe.js所做的。如果您不需要支持IE7,则可以从strophe.js执行 CORS ,而不要甚至需要同源的代理。 IE6将工作,因为它是不安全的,IE8 +支持一个只是几乎不工作的CORS。



要从django通过XMPP获取信息到您的客户端,进行组件连接( XEP-0114 )到您的服务器使用您最喜欢的 Python XMPP库,例如您的Django应用程序中的 SleekXMPP 。安排该连接相对较长寿命,以便执行(即,不要为每个客户端连接创建一个新的)。根据需要发送协议。



您没有提到使用的XMPP服务器。不支持BOSH的XMPP服务器变得越来越少了,但是如果有的话,您可能需要旁遮普作为BOSH- to-XMPP代理,或者您可能需要切换到较新的服务器,例如 Prosody


I have a couple of Android and iOS native mobile application that I wrote which connect directly to an XMPP server that I host. They push and pull realtime data through XMPP. I also use some of the XMPP XEP extensions. For other operations, I have a django application running on the same server which all the mobile applications consume through an HTTP REST interface. I use Celery and Redis for the django side to do some operations asynchronously (like doing heavy batched writes to my db).

This all works fine and dandy. Yay.

But now I want to write a web front-end to all of this, so I started researching my options and well - there are so many ways to skin the cat that I wanted to check with the SO community first.

The idea to have a js library that gives me a unified API for socket communications (i.e try different implementations of web sockets or fall back to flash) appeals to me hence why I mention Socket IO. The idea of having to run a nodejs server, well, not so much (one more thing to learn), but if I have to, I definitely will. I know that some people use gevent as a replacement of the node server. Others, decide to write a small nodejs which they connect to the rest of their stack. I would probably do this.

Another option, is to use an js XMPP library like Strophe which I don't think has a flash fallback. Also, I would need to research what this means for my server.

I have read several of the Stackoverflow answer on how to do comet and django - hence why it seems that there are several options.

The question is:

If I want to have the advantage of Socket IO behavior (with the fallbacks) and I want to push realtime data to the web client (which is being fed to the server through XMPP), and use Django what is my best option?

Update: The XMPP server that I use is ejabberd, which also supports BOSH. I realize that I could use Strophe.js and thus my communication would go over a type of long polling http connection instead of websockets. As far as I can tell, there are some XMPP over Websockets open source library, but AFAIK the community is not as active as the SocketIO one.

Update 2: Browsers that I need to support are only modern browsers. I guess this means that Flash fallback will not be that important, which is leaning me towards strophe.js.

解决方案

Not sure why you'd need Flash fallback if you're going to do BOSH (XEP-0124, XEP-0206), which is what strophe.js does. If you don't need to support IE7, you can do CORS from strophe.js, and you don't even need a proxy for same-origin. IE6 will work because it's insecure, and IE8+ support a just-barely-working form of CORS.

To get information from django through XMPP to your client, make a component connection (XEP-0114) to your server using your favorite Python XMPP library, such as SleekXMPP from your Django app. Arrange for that connection to be relatively long-lived, for performance (i.e. don't create a new one for each client connection). Send protocol as needed.

You didn't mention what XMPP server you're using. XMPP servers that don't support BOSH are getting rare, but if you've got one, you might need Punjab as a BOSH-to-XMPP proxy, or you might want to switch to a newer server, such as Prosody.

这篇关于使用Socket IO(或Strophe.js),XMPP和Django实时服务器推送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆