Django Comet/Long Pollling application - django

I am working a real-time lighting control system.
User can turn on/off lights through web interface, but when another person turn on/off light switch manually the web interface should be updated immediately. That's the real-time goal I want.
The structure of the system is like this.
I have my own server at home, which control/monitor light status real-time. Django project is on my cloud server communicating with my home server real-time.
I have searched some similar posts and found many tools that confuse me a lot. All I want is to do long polling. User's browser will send http request to django server periodically(say 30s). If nothing comes up, server will hold the response until next one come. If there is an event, server will just reply immediately so real-time change at the browser side.
I know websocket is a better way, but I just want to make it work, the simpler the better. As long as it's reliable, compatible with existing browsers, performance is not a primary concern for me now. I can change it later if there is performance issue when scaling up. If it's possible, I still want to use Apache server to do this.
Does anyone knows a good tutorial or example that shows me how to implement it?
Thanks so much
I use Django 1.4, wsgi, Apache.

Related

How to make Django pass cookies when communicating with Node.js server using socket.io?

I am currently developing an instant messaging feature for my apps (ideally cross platform mobile app/web app), and I am out of ideas to fix my issue.
So far, I have been able to make everything work locally, using a Node.js server with socket.io, django, and redis, following what most tutorials online suggest.
The step I am now at consists in putting all that in the cloud using amazon AWS. My Django server is up and running, I created a new separate Node.js server, and I am using Elasticache to handle the Redis part. I launch the different parts, and no error shows up.
However, whenever I try using my messaging feature on the web, I keep getting an error 500:
handshake error
I then used the console to check the request header, and I observed that the cookies are not in there, contrary to when I am on localhost. I know it is necessary to authorize the handshake, so I guess that's where my error is coming from..
Furthermore, I have also checked that the cookies do exist, they are just not set in the request header.
My question is then: How can I make sure Django or socket client (not sure who's responsible here..) puts the cookies in the header??
One of my ideas was that maybe I am supposed to put everything on the same server, with different ports, instead of 2 separate servers? Documentation on that specific architecture problem is surprisingly scarce, compared to the number of tutorials describing how to make it work on local.
I hope I described the problem accurately enough! :)
Important note: I am using socket.io v0.9.1-1, only one compatible with a titanium mobile app.
Thank you for any help!
All right, so I've made some progress.
The cookie problem came from the fact I was making cross-domain request, adding a few lines enabled CORS, which didn't solve the cookie issue, but allowed me to communicate between servers (basically I set the headers of the response using express. I then passed necessary data in the query, even if not the most secure way to do it, I'm just building an MVP, and it's enough for now.
I haven't been able to make the chat work from my Titanium mobile app, but since I can use a webview to handle it, I will be fine.
Hopefully that will help someone.. If anyone needs me to post some code snippets I will gladly do so upon request!
Cheers

Cloning PyQt app in django framework

I've designed a desktop app using PyQt GUI toolkit and now I need to embed this app on my Django website. Do I need to clone it using django's own logic or is there a way to get it up on website using some interface. Coz I need this to work on my website same way it works as desktop. Do I need to find out packages in django to remake it over the web or is there way to simplify the task?
Please help.
I'm not aware of any libraries to port a PyQT desktop app to a django webapp. Django certainly does nothing to enable this one way or another. I think, you'll find that you have to rewrite it for the web. Django is a great framework and depending on the complexity of your app, it might not be too difficult. If you haven't done much with web development, there is a lot to learn!
If it seemed like common sense to you that you should be able to run a desktop app as a webapp, consider this:
Almost all web communication that you likely encounter is done via HTTP. HTTP is a protocol for passing data between servers and clients (often, browsers). What this means is that any communication that takes place must be resolved into discrete chunks. Consider an example flow:
You go to google in your browser.
Your browser then hits a DNS server (or cache) that resolves the name google.com to some IP address.
Cool, now your browser makes a request to that IP address and says "get me some stuff".
Google decides to send you back a minimal amount of HTML and lots of minified JavaScript in the page.
Your browser realizes that there are some image links in the HTML and so it makes additional requests to google to get each of the images so that it can display them.
Now all the content is loaded on your browser so it starts to execute the JavaScript code, and that code needs some more data from google so it starts sending requests to google too.
This is just a small example of how fundamentally different a web application operates than how a desktop application does. On a desktop app you have the added convenience that any operation doesn't need to be "packaged up" and sent, then have an action taken, etc (unless you're using a messaging architecture, but that's relatively uncommon outside of enterprise apps).

How github auto updating comments work?

As mentioned here
https://github.com/blog/1174-auto-updating-comments
What is the technology behind this? If I've to add this feature in a Django powered web app, what should I use and study?
Looking at network tab it looks like they (at least with chrome) are using HTML5 Server Sent Events.
So practically the browser subscribes to a event stream and the web server just sends messages back.
I am not an expert but I guess on the server side you need to be able to keep an open connection that streams the events to the client.
I found an implementation of SSE for python here: https://github.com/niwibe/sse and a django implementation on top of that: https://github.com/niwibe/django-sse
I did not use them (yet) on any production so I suggest them only as study / poc material :)

Best practices to update a live web service

I am always curious about how large-scale live web application updates are done. Since the application is live, that is why it complicates everything -you should not down your service and at the same time you should recover the activity/changes (in database etc.) made on your site to the new version during the update later on.
The first and most natural technique comes to the mind is that redirecting all the requests to some other replicated server, so that you can update original server without shutting-down your service.
I just wonder is there any other smarter techniques to handle updates in a live web service. Please, share your experience and opinions guys!
I am facing the same challenge myself.
What i did was to recreate the web page on another server [lets name it Test server] (with a different domain of course), import the scripts/database from live server and adjust them on the new domain.
Now I am experimenting on the Test server and after i make sure that everything is working OK, i am pushing the changes to the live server.
Unfortunately, i dont know if this is the correct way to do this. You have to be careful but it works.

Real time web services

I have a little (maybe the answer could require a book) question about web services and server side programming.
But first, a little preamble.
Recently we have seen new kind of applications & games using some kind of real-time interaction with a database, or more generally, with other users. I'm talking about shared drawing canvas, games like this , or simple chats, or the Android app "a World of Photo", where in real time you see who is online, to share your photos, etc.
Now my question:
Are all these apps based on classic TCP client/server architectures or is there a way to make them in a simpler way, like a web platform like LAMP?
What I'm asking, in other words is:
Can PHP+MySQL (or JSP, or RoR, or any other server language) provide a way to make online users communicate in real time and share data? Is there a way to do that without the ugly and heavy mechanism of temporary tables?
Thank you! I hope I've been clear.
There is this crazy black magiccy thing called P2P (peer-to-peer) that makes all of this possible.
I don't know, but I don't think it will catch on.
8-D
EDIT: CRAP....I didn't see the date the question was posted.
I can't believe it. This is my third consecutive question that involves XHR Long Polling aka Comet programming. Daliz, what you want is possible, easy and been done by various websites using XHR Long Polling.
The concept: Normally, web browsers send a request to the server, server sends a response back and closes the connection to the browser. Instead of closing the connection at the end, Comet programming keeps the connection open. So, the connection between the browser and the server is still open. So, in simple terms, when we want to connect to Alice, we find the thread connecting to her browser and send the message to Alice.
Facebook chant uses the same concept. Please read This.
If your client is not a web browser but some web service, then it is more easy. Web services unlike web browsers have an address. So, client service calls the main service with a callback URL and registers to a particular event. When that event happens, like someone drawing something, main service will use the callback url of the client service to notify about that action.
On sharing data like Photos or another binary data: it is also possible as the input stream from one user is send to all other related users. So a simple String or a photo can be shared. But it also means the user on the receiving end of the photo will not have a message like "xxx has sent you a photo. Click here to download it". They will simply get a "Do you want to open or save this file" option.