Need help with setting up comet code

Posted by Saif Bechan on Stack Overflow See other posts from Stack Overflow or by Saif Bechan
Published on 2010-04-03T14:36:08Z Indexed on 2010/04/03 14:43 UTC
Read the original article Hit count: 726

Does anyone know off a way or maybe think its possible to connect Node.js with Nginx http push module to maintain a persistent connection between client and browser.

I am new to comet so just don't understand the publishing etc maybe someone can help me with this.

What i have set up so far is the following. I downloaded the jQuery.comet plugin and set up the following basic code:

Client JavaScript

<script type="text/javascript">

    function updateFeed(data) {
        $('#time').text(data);
    }

    function catchAll(data, type) {
        console.log(data);
        console.log(type);
    }

    $.comet.connect('/broadcast/sub?channel=getIt');
    $.comet.bind(updateFeed, 'feed');
    $.comet.bind(catchAll);

    $('#kill-button').click(function() {
        $.comet.unbind(updateFeed, 'feed');
    });
</script>

What I can understand from this is that the client will keep on listening to the url followed by /broadcast/sub=getIt. When there is a message it will fire updateFeed.

Pretty basic and understandable IMO.

Nginx http push module config

default_type application/octet-stream; sendfile on; keepalive_timeout 65; push_authorized_channels_only off;

server {
  listen       80;
  location /broadcast {
    location = /broadcast/sub {
      set $push_channel_id $arg_channel;
      push_subscriber;
      push_subscriber_concurrency broadcast;
      push_channel_group broadcast;
    }

    location = /broadcast/pub {
      set $push_channel_id $arg_channel;
      push_publisher;
      push_min_message_buffer_length 5;
      push_max_message_buffer_length 20;
      push_message_timeout 5s;
      push_channel_group broadcast;
    }
  }
}

Ok now this tells nginx to listen at port 80 for any calls to /broadcast/sub and it will give back any responses sent to /broadcast/pub.

Pretty basic also. This part is not so hard to understand, and is well documented over the internet. Most of the time there is a ruby or a php file behind this that does the broadcasting.

My idea is to have node.js broadcasting /broadcast/pub. I think this will let me have persistent streaming data from the server to the client without breaking the connection. I tried the long-polling approach with looping the request but I think this will be more efficient.

Or is this not going to work.

Node.js file

Now to create the Node.js i'm lost. First off all I don't know how to have node.js to work in this way.

The setup I used for long polling is as follows:

var sys = require('sys'), 
http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(new Date());
    res.close();
    seTimeout('',1000);
}).listen(8000);

This listens to port 8000 and just writes on the response variable.

For long polling my nginx.config looked something like this:

server {
  listen      80;
  server_name _;

  location / {
    proxy_pass   http://mydomain.com:8080$request_uri;
    include      /etc/nginx/proxy.conf;
  }
 }

This just redirected the port 80 to 8000 and this worked fine.

Does anyone have an idea on how to have Node.js act in a way Comet understands it. Would be really nice and you will help me out a lot.

Recources

used

To use faye I have to install the comet client, but I want to use the one supplied with Nginx. Thats why I don't just use faye. The one nginx uses is much more optimzed.

extra

© Stack Overflow or respective owner

Related posts about node.js

Related posts about comet