惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
IT之家
IT之家
博客园 - 【当耐特】
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
MyScale Blog
MyScale Blog
WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Hugging Face - Blog
Hugging Face - Blog
I
InfoQ
B
Blog RSS Feed
腾讯CDC
云风的 BLOG
云风的 BLOG
N
Netflix TechBlog - Medium
Apple Machine Learning Research
Apple Machine Learning Research
GbyAI
GbyAI
雷峰网
雷峰网
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
D
DataBreaches.Net
The Cloudflare Blog
V
V2EX
S
SegmentFault 最新的问题

博客园 - BoyeeStudio

FLEX自定义事件 Flex Array 与 ArrayCollection.转自网络 PyAMF and django ForeignKey How to sync your github repository after fork from others'. N年没写博客了,手生锈了,准备再来磨磨! CAsyncSocket,CSocket内幕及其用法 Socket中如何设置连接超时(转载 可能用的到) 最近在我的python博客上花时间! P2P相关软件源代码下载 写个perl程序自动下载《南方周末》(2005年12月最后一期,38版,值得一看) wxWidgets开发工具及Makefile的书写!(待续。。。) 用wxWindows编程 -----第一步!(翻译)(系列1) 编VC网络程序遇到一个问题,原来是少了一个LIB。 如何优化C语言代码(程序员必读) 重温经典排序思想--C语言常用排序全解(转载--值得一看) 常用windows API WAP开发FAQ c++资源之不完全导引(全文) (转载) GNU编码标准
Django Push HTTP Response to users
BoyeeStudio · 2013-06-30 · via 博客园 - BoyeeStudio

Django Push HTTP Response to users

I currently have a very simple web application written in Django, and I would like to implement something like a callback/push notification service in my application.

For example: When one user(client) uploads a photo to the server, the server notifies all other connected users about that photo.

I suppose I can use Django signals to produce a callback when a user uploads a photo, but how do I get Django to post a notification to the other users? This notification could either be alerts or simply redirecting the other users to a new html that displays the uploaded picture. I would prefer the latter.

I am a beginner in web programming, so I am not sure if this fits the bill as needing a 'real-time web application' that implements things like comet or long-polling. My application is similar to that of a chat application, except that I am not submitting text files but image files. Because of that I thought comet solutions would work. I have tried looking at Orbited and Twisted for a very long time now but had no luck in implementing it with Django, probably because I do not understand how to accomplish what I want with comet solutions. I would like the more experienced programmers to point to me what is it exactly that I need in order to accomplish this, or if I am heading into the right direction or not (with comet).

I would really appreciate it if someone could give me some tips and hints as to how to proceed, as well as tutorial links or guides.


16

HTTP is inherently a "pull" protocol--i.e., a client pulls data from a server, waits around for a while and then pulls more data later. There's actually no strictly HTTP way to "push" data to a client from a server.

You have basically three options when you need to "push" to a client.

(1) Do polling--use Ajax/javascript to poll the server every X amount of time. The smaller the X the more it "feels like" a push, but also the more overhead your server experiences having to constantly respond to these requests.

(2) Use websockets. Part of the HTML5 spec is something called websockets. Websockets allows a browser to open up a persistent connection to a server. Once this connetion has been opened data can be pushed back and forth from client to server and server to client just like with more traditional TCP sockets. The trouble with websockets (last I heard) was that they can still be a bit temperamental between browsers, and of course wont work at all in older browsers.

(3) Use Flash with a Javascript interface. Flash has the capability of setting up persistent TCP connections, which can be used to push/pull data just like with a 'normal' TCP connection. (See this SO question as well: HTTP push examples in Flex)


If you were starting this project from scratch I would recommend you write your backend in Node.js with Socket.io. Socket.io is "socket-like" framework that you can program to and then the Javascript client (which runs in your webbrowser) intelligently determines the best "persistent connection" to use--first it tries to use Websockets, then Flash, then long polling of various types.


But since you've said you want to use Python/Django then you should check out Django-Websockets--a framework for using websockets with Django. But be sure to read the Disclaimer the author puts on the page, there are some significant difficulties/limitations associated with using it, primarily because Django wasn't designed with websockets in mind.

I think your best bet will end up being using Websockets with an intelligent fallback to Ajax Polling when the user's browser doesn't support it.

4

If ever you use nginx, which is a good choice :), you may use the push module http://pushmodule.slact.net/, I found it rather easy to use. You have one URL to publish messages on a channel (that can be done easily in python, with httplib for example), and one URL to pull messages from a channel (and a channel may be used by more than one user). See also http://blog.jamieisaacs.com/2010/08/27/comet-with-nginx-and-jquery/ for a jquery integration.