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.

django , google-app-engine ,

Answer...


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.

Django Push HTTP Response to users的更多相关文章

  1. Django push: Using Server-Sent Events and WebSocket with Django

    http://curella.org/blog/2012/jul/17/django-push-using-server-sent-events-and-websocket/ The goal of ...

  2. Django Request 与Response对象

    Django使用请求和响应对象在系统中传递状态.当请求页面时,Django创建一个HttpRequest对象,该对象包含关于请求的元数据. 然后Django加载适当的视图,将HttpRequest作为 ...

  3. Django中的response

    render_to_response render_to_response('index.html', locals(),context_instance=RequestContext(request ...

  4. Django基础之response对象

    与由DJango自动创建的HttpRequest对象相比, HttpResponse对象是我们的职责范围了. 我们写的每个视图都需要实例化, 填充和返回一个HttpResponse. HttpResp ...

  5. Django Push 的一些资料

    先来中文的: 主要讲Orbited: http://sunsetsunrising.com/2009/django_comet.html#gsc.tab=0 再来英文的: http://www.rkb ...

  6. 《The Django Book》实战--第二章--动态网页基础

    这章演示了一些最基本的Django开发动态网页的实例,由于版本不一样,我用的是Django 1.,6.3,有些地方按书上的做是不行的,所以又改了一些,写出来让大家参考. 这是一个用python写的一个 ...

  7. Django 大文件下载

    django提供文件下载时,若果文件较小,解决办法是先将要传送的内容全生成在内存中,然后再一次性传入Response对象中: def simple_file_download(request): # ...

  8. 跨过Nginx上基于uWSGI部署Django项目的坑

    先说说他们的关系,Nginx和uWSGI都是Web服务器,Nginx负责静态内容,uWSGI负责Python这样的动态内容,二者配合共同提供Web服务以实现提高效率和负载均衡等目的.uWSGI实现了多 ...

  9. Django Middleware简介

    1      前言 Django使用非常熟练了,各种API接口不在话下,全都搞定.为方便定位问题在每个API接口的的开始和返回的地方都加上了log打印,记录入参和返回值. 但是这样有一个问题,需要每个 ...

随机推荐

  1. Atitit.guice3 ioc 最佳实践 o9o

    Atitit.guice3 ioc  最佳实践 o9o 1. Guice的优点and跟个spring的比较 1 2. 两个部分:::绑定and注入@Inject 1 3. 绑定所有的方法总结 2 3. ...

  2. atitit. 统计功能框架的最佳实践(1)---- on hibernate criteria

    atitit. 统计功能框架的最佳实践(1)---- on hibernate criteria 1. 关键字 1 2. 统计功能框架普通有有些条件选项...一个日期选项..一个日期类型(日,周,月份 ...

  3. javaweb学习总结(六)——Servlet开发(二)

    一.ServletConfig讲解 1.1.配置Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为servlet配置一些 ...

  4. Liferay7 BPM门户开发之32: 实现自定义认证登陆(定制Authentication Hook)

    第一步:修改liferay-hook.xml <?xml version="1.0"?> <!DOCTYPE hook PUBLIC "-//Lifer ...

  5. iframeWin For Easy UI. 为 Easy UI 扩展的支持IFrame插件

    iframeWin For Easy UI. 为 Easy UI 扩展的支持IFrame插件 在一个项目中用了Easy UI,但是发现里面的 Dialog .Window.Messager 弹窗都不支 ...

  6. NoteExpress 自定义参考文献输出样式

    最近在写硕士论文,样式中没有符合要求的,于是就自己来编辑下样式文件,下文是网上搜集的关于自定义参考文献的一篇教程,可以参考. 一.分隔符|和连接符*的使用 (1)连接符 * 主要作用是“建立依赖关系” ...

  7. Spring3 整合MyBatis3 配置多数据源 动态选择SqlSessionFactory

    一.摘要 上两篇文章分别介绍了Spring3.3 整合 Hibernate3.MyBatis3.2 配置多数据源/动态切换数据源 方法 和 Spring3 整合Hibernate3.5 动态切换Ses ...

  8. Unity3D Shader入门指南(一)

    动机 自己使用Unity3D也有一段时间了,但是很多时候是流于表面,更多地是把这个引擎简单地用作脚本控制,而对更深入一些的层次几乎没有了解.虽然说Unity引擎设计的初衷就是创建简单的不需要开发者操心 ...

  9. 从《BLAME!》说开去——新一代生产级卡通真实感混合的渲染方案

    <BLAME!>是Polygon Pictures Inc.(以下简称PPI)创业33周年以来制作的第一部CG剧场电影,故事来自于贰瓶勉的同名漫画作品(中文译名为<探索者>或者 ...

  10. [转] Autofac创建实例的方法总结

    1.InstancePerDependency 对每一个依赖或每一次调用创建一个新的唯一的实例.这也是默认的创建实例的方式. 官方文档解释:Configure the component so tha ...