My First Streaming

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:

– jQuery

– Python/Django

– 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:

* app/ you will find the Resource who makes everything possible. This file implements a Django-Piston( BaseHandler class who responds to GET requests with read method and to POST requests with create method.

* static/js/serverpush.js contains javascript/jQuery code for front-end.

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 method, here it will call 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.

See ya!

  1. Andru said:

    Hello! I try do real-time django comments. Do you know how i can do it well?

    • Hello, sorry this little late answer to your question.

      First of all I want to say that I do not consider myself an expert in streaming. But the most important its to follow the concepts of what HTTP streaming is about.
      Answering to your question there is a couple of things you have to think about, how much resources will be involved in what you want to do, network/servers etc, remember that on the server-side you have to keep the connection opened until you get some response content to push back to the browser.

      Particularly in Django i don’t know any module to do this but I guess its not that needed. All you need to do its to yield HttpResponse() objects when you get the content ready on the server. The question here is:

      “When to yield the HttpResponse objects?”

      Some lazy implementations when they receive a client requests they will leave the connection opened at the same time they will make some kind of pooling to some flag to check whether or not there is some new content to response back to the client. Well… it will work its a fact but this implementation its a bit tricky and in its essence not correct.

      The real way to do this its actually making use of interruptions and sockets to signalize that there is new content available to response. So let’s say, when a connection is received if there is no content to response the execution will be halted, meanwhile another process will be checking if there is new content to display for a certain request. Once there is new content it will signalize the request handler process that from here will finally collect the new content and response it back to the client.

      I really don’t know if there is some django module to this. But you might want to have a look on some coroutine library like gevent to help you to do this.

      Thanks for commenting and I hope I’ve been helpful.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: