title: Need help with setting up comet code
body: <p>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.</p>
  
  <p>I am new to comet so just don't understand the publishing etc maybe someone can help me with this.</p>
  
  <p>What i have set up so far is the following. I downloaded the jQuery.comet plugin and set up the following basic code:</p>
  
  <blockquote>
    <h2>Client JavaScript</h2>
  </blockquote>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
      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');
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p>Pretty basic and understandable IMO.</p>
  
  <blockquote>
    <h2>Nginx http push module config</h2>
  </blockquote>
  
  <p>default_type  application/octet-stream;
  sendfile        on;
  keepalive_timeout  65;
  push_authorized_channels_only off;</p>
  
  <pre><code>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;
      }
    }
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <blockquote>
    <p><strong><em>My idea is to have node.js broadcasting <code>/broadcast/pub</code>. I think this will let me have <code>persistent streaming data</code> 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.</em></strong></p>
  </blockquote>
  
  <p>Or is this not going to work.</p>
  
  <blockquote>
    <h2>Node.js file</h2>
  </blockquote>
  
  <p>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.</p>
  
  <p>The setup I used for long polling is as follows:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>This listens to port 8000 and just writes on the response variable.</p>
  
  <p>For long polling my <code>nginx.config</code> looked something like this:</p>
  
  <pre><code>server {
    listen      80;
    server_name _;
  
    location / {
      proxy_pass   http://mydomain.com:8080$request_uri;
      include      /etc/nginx/proxy.conf;
    }
   }
  </code></pre>
  
  <p>This just redirected the port 80 to 8000 and this worked fine.</p>
  
  <blockquote>
    <p><strong><em>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.</em></strong>  </p>
  </blockquote>
  
  <h2>Recources</h2>
  
  <p><strong><em>used</em></strong></p>
  
  <ul>
  <li><a href="http://www.igvita.com/2009/10/21/nginx-comet-low-latency-server-push/" rel="nofollow noreferrer">An example where this is done with ruby instead of Node.js</a></li>
  <li><a href="http://github.com/SeanOC/jquery.comet" rel="nofollow noreferrer">jQuery.comet</a></li>
  <li><a href="http://pushmodule.slact.net/" rel="nofollow noreferrer">Nginx HTTP push module homepage</a></li>
  <li><a href="http://blog.jcoglan.com/2010/02/02/faye-a-comet-client-and-server-for-node-js-and-rack/" rel="nofollow noreferrer">Faye: a Comet client and server for Node.js and Rack</a></li>
  </ul>
  
  <p>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.</p>
  
  <p><strong><em>extra</em></strong></p>
  
  <ul>
  <li><a href="http://gist.github.com/258696" rel="nofollow noreferrer">Persistant connections</a></li>
  <li><a href="http://www.nakedjavascript.com/going-evented-with-nodejs" rel="nofollow noreferrer">Going evented with Node.js</a></li>
  </ul>