First hours with node.js and

Tue 02 July 2013 // posts

At last I spent some time with node.js,, and more precisely http push technique (comet). My motivation was just to experiment, see how the actual http pushing works (it gets much clearer when using Wireshark and Chrome dev tools) and what it takes to setup the system.

I ended up with node.js + on the server side, python + on the client side.

As a really brief explanation,

  • node.js is a server side javascript scripting platform (built around google's V8 engine, known for its great performance)...Well, as of now I think of it as a JS interpreter.

  • is a library, specially designed for Javascript, leverages http push session handling and delivers a nice event driven messaging system. The actual work is based on this library.

While typical http sessions have one way flow, that is, clients make requests (HTTP GET, POST, etc) in order to get data from the server(poll), http push technique allows clients to get virtually real-time updates from the server.

After figuring out what I just described, it was time for experimenting. Considering that I'm a Python boy, I wanted to bring it into the mix. There are a quite number of possibilities, either run a server implemented in Python, or use IPC (inter process communication) between node.js and Python (or whatever) apps. I chose IPC, although, when I tried to install one of the most promising solutions - ZeroRPC - I ran into issues (different dependencies versions, etc). I didn't spent much time fixing this, instead, I wondered if one could also use as a base for IPC....soon to realize that it is possible! EDIT: I've dedicated some time to gevent-socketio. Until now it looks good, well organized and reasonably documented. I'll update the post as soon as I have more details.

Enough with the talking, let's get to the code! But before that, let me conclude what I ended up with:
1. A server (server.js) running with node.js.
2. A Python app ( that connects to server, receives dummy update requests and returns the dummy results back to the server.
3. A real client (client.html) that also connects to the server, receives and displays the results, instantly.



var io = require('').listen(8080);  
var workerio = require('').listen(8081);

Real clients  
io.sockets.on('connection', function (socket) {


Worker client (python)  
workerio.sockets.on('connection', function (socket) {  
    socket.on('disconnect', function(message, callback) {  
        io.sockets.emit('worker_disconnected', "worker disconnected");  

    // When we receive an update from worker.  
    socket.on('update_from_worker', function(message, callback) {  
        io.sockets.emit('new_update', message);  

    io.sockets.emit('worker_connected', "worker connected");  

function askUpdates()  
    workerio.sockets.emit("please_update", "now");  
    setTimeout(askUpdates, Math.random()*3000);  


from socketIO_client import SocketIO  
from time import sleep  
from datetime import datetime

socketIO = SocketIO('localhost', 8081)

def pleaseUpdate_cb(*args):  
    socketIO.emit('update_from_worker', " finished update -
    %s" % (,))

socketIO.on("please_update", pleaseUpdate_cb)

while 1:  


      <link href="static/css/style.css" rel="stylesheet">
      <script type="text/javascript"
         src="//"/>  </script>
      <script type="text/javascript"
         src="//">  </script>
      <script type="text/javascript"
         src="//">  </script>
         var socket = io.connect("//");

         $(window).bind("beforeunload", function() {  

         socket.on('new_update', function (msg) {  
             var el = $('#log').prepend($('<p
             $("#log .update").first().animate({color: '#000000'}, 3500);  

         socket.on('worker_connected', function (msg) {  
             var el =

         socket.on('worker_disconnected', function (msg) {  
             var el =

         $.ready(function () {

      <style type="text/css">  
         #log {  
             display: inline-block;  
             padding: 0 50px;  
             max-height: 300px;  
             overflow-y: scroll;  
         p {  
            margin: 0;  
      <div id="log">  </div>