在django直播通知/聊天 [英] Live notification/chat in django

查看:508
本文介绍了在django直播通知/聊天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在和django建立一个网站,我想实现一个在Facebook或SE上的实时通知功能。

I am making a website with django now and I want to implement a live notification feature like the one on facebook or SE.

我做了一些研究,似乎虽然有两个选择:ajax长轮询和websockets,后者是要走的路。

I did some research and it seems although there's two options: ajax long polling and websockets, the latter is the way to go.

但是,如你所知,websocket'socket.io'的插件可能是一个node.js插件,而django端口似乎只支持python 2这个项目似乎已经死了。我使用python 2.7作为我的项目解释器,但是我想要将来证明自己,所以如果我稍后升级到python3,我不觉得自己无法使用这个功能。

However, as you know the go to plugin for websocket 'socket.io' turns out to be a node.js plugin and the django port only seems to support python 2 and the project seems pretty much dead. I am using python 2.7 as my project interpreter but I want to future proof myself so that if I upgrade to python3 later, I don't find myself not being able to use this functionality.

所以我的问题是这样的:
是否有一个直接和未来的准备好的方式来实现websocket,将用于在django env中发送直播通知和聊天?

So my question is this: Is there a straight forward and future ready way to implement websocket which will be used to send live notifications and chats in django env?

推荐答案

Django本身是以阻止方式构建的,即使用同步方法。所以,你不能打开持久的websocket与django应用程序,因为它将阻止整个django线程。

Django itself is build in blocking manner, i.e. with synchronous approach. So, you cannot open persistent websocket with django app, as it will block entire django thread.

如果你想启用通知/聊天django项目环境,我建议您使用离心机。它是用python编写的,但是使用了异步(非阻塞)框架:龙卷风

If you want to enable notification/chat within django project environment, i would recommend to use centrifuge. It is written in python, but async (non-blocking) framework is used: tornado.

但是,您不需要甚至知道它是如何工作的,因为它提供了简单的REST API来与之通信。

But, you don't need to even know how it works, as it provides simple REST api to communicate with it.

简化工作流程,请查看文档了解更多详情:

Simplified workflow, check docs for more details:


  1. 在相同的服务器上开始离心机,作为您的django项目(或另一个项目,但它们之间的延迟低)

  2. 您的前端将使用离心机打开websocket,而不是使用django项目。

  3. 当您需要发送通知时,通过REST api将其发送到django离心机,离心机将提供给需要的客户端!

  1. Start centrifuge at same server, as your django project (or on another but with low latency between them)
  2. Your front-end will open websocket with centrifuge, not with django project.
  3. When you need to send notification, send it to centrifuge from django via REST api, and centrifuge will deliver it to needed clients!

我已经尝试了,它的工作原理!

I've already tried it and it works!

这篇关于在django直播通知/聊天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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