Hi all! I know i’ve been quite far from this “Blog life” but in these last days I’ve been thinking to write something and today i did something that for me it’s one proper thing to post here.
It is my first HTTP streaming.
Streaming is one word that for those who spend some time on the internet heard or read a lot of times and it always caught my attention and curiosity so i decided to go a little further with the research and not only to know what it really is but in fact do something or in other words, put the hands to the work.
So, the method i used was the old but still used AJAX Pooling Streaming Technique wich is quite simple. After the server receives the request from a browser , the server checks if there is something to response, otherwise it olds the connection until it has something and then response the information back to the browser, after his process the browser starts a new connection with the server again and so on.
After figuring out what was the concept about i decided to do a little experience. So the point was to have a small interface where there should be a textarea to write something and it would be streamed to a content box in the same layout.
To do this i used:
– Django PISTON REST framework
So what i did was a really simple and stupid application where the textarea content would be stored in a file and AJAX pooling would be reading the content of the same file.
For those who wanna implement this i must advice this is impossible to do using djangos development server due to the impossibilitie to handle two requests at the same time, Djangos development server is a single user, single process and single thread server remember so configure Apache or any other multi thread HTTP server in order to get the things working.
The code is available here: http://bitbucket.org/andrefsp/serverpush/src
* app/handlers.py you will find the Resource who makes everything possible. This file implements a Django-Piston(http://bitbucket.org/jespern/django-piston/wiki/Home) BaseHandler class who responds to GET requests with read method and to POST requests with create method.
How does this work??
After a user writes something in the textarea, this content will be transmitted by HTTP POST request, this one will be handled by Streaming.create() and will store the information received into a file.
After the user clicks on streaming button it will trigger an HTTP GET request that will be handled by Streaming.read() method, here it will call streaming.stream() and this one olds the connection until it has something to response back.
As i said before, this is a really simple way to make streaming and if you want to do something really cool perhaps its better to take advantage of the new WebSockets introduced in HTML5.
I believe in a few years a lot of web applications will be running using this new feature of HTML5 wich avoids a lot of complications and “tricks” to make the things done.