Activate your free membership today | Log-in

Tuesday, February 9th, 2010

Faye: Bayeaux protocol Comet server for Node.js

Category: Comet

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:
  1.  
  2. <script type="text/javascript" src="/comet.js"></script>
  3.  
  4. <script type="text/javascript">
  5.     CometClient = new Faye.Client('/comet');
  6.     CometClient.connect();
  7. </script>
  8.  
JAVASCRIPT:
  1.  
  2.   CometClient.subscribe('/path/to/channel', function(message) {
  3.     // process received message object
  4.   });
  5.  
  6.   CometClient.publish('/some/other/channel', {foo: 'bar'});
  7.  

And the backend....

JAVASCRIPT:
  1.  
  2.   var http  = require('http')
  3.       faye  = require('./faye');
  4.  
  5.   var comet = new faye.NodeAdapter({mount: '/comet', timeout: 45});
  6.  
  7.   http.createServer(function(request, response) {
  8.     if (comet.call(request, response)) return;
  9.  
  10.     response.sendHeader(200, {'Content-Type': 'text/plain'});
  11.     response.sendBody('Hello, non-Comet request!');
  12.     response.finish();
  13.  
  14.   }).listen(9292);
  15.  

Nice!

Posted by Dion Almaer at 6:24 am
Comment here

+++--
3.8 rating from 16 votes

Comments Here »

Comments feed TrackBack URI

Leave a comment

You must be logged in to post a comment.