Faye: Bayeaux protocol Comet server for Node.js

In: web resources

9 Feb 2010

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!

Go to Source

Related Blogs

  • Related Blogs on Faye: Bayeaux protocol Comet server for Node.js

Comment Form

About this blog

This blog delivers stylish and dynamic news for designers and web-developers on all subjects of design, ranging from: CSS, Ajax, Javascript, web design, graphics, typography, advertising & much more. Our goal is to help you communicate effectively on the web with an engaging website or functional interface.