Activate your free membership today | Log-in

Tuesday, February 9th, 2010

Faye: Bayeaux protocol Comet server for Node.js

Category: Comet, Node

James Coglan has ported a Ruby/EventMachine Comet server to offer a new Node.js server on the Bayeux protocol. The project is Faye and you can check out the code on GitHub.

On the client side:

HTML:
<script type="text/javascript" src="/comet.js"></script>

<script type="text/javascript">
    CometClient = new Faye.Client('/comet');
    CometClient.connect();
</script>
 

JAVASCRIPT:
  CometClient.subscribe('/path/to/channel', function(message) {
    // process received message object
  });

  CometClient.publish('/some/other/channel', {foo: 'bar'});
 

And the backend....

JAVASCRIPT:
  var http  = require('http')
      faye  = require('./faye');

  var comet = new faye.NodeAdapter({mount: '/comet', timeout: 45});

  http.createServer(function(request, response) {
    if (comet.call(request, response)) return;

    response.sendHeader(200, {'Content-Type': 'text/plain'});
    response.sendBody('Hello, non-Comet request!');
    response.finish();

  }).listen(9292);
 

Nice!

Posted by Dion Almaer at 6:24 am Comment here

Comments Here »

Comments feed TrackBack URI

Leave a comment

You must be logged in to post a comment.