zoukankan      html  css  js  c++  java
  • Django Push HTTP Response to users

    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.
  • 相关阅读:
    STM32 串口DMA方式接收(转)
    STM32 串口功能 库函数 详解和DMA 串口高级运用(转载)
    内存泄露调试心得
    Android 5.0 5.1 webview 闪退问题
    ios 接入微信开发 新版
    ios 获取app版本号
    ios alamofire4.x网络框架url 中文问题
    微服务监控druid sql
    kotlin 单例模式
    mysql 数据库保存 微信分享时不能换行
  • 原文地址:https://www.cnblogs.com/BoyeeStudio/p/3163763.html
Copyright © 2011-2022 走看看