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:

  1. <script type="text/javascript" src="/comet.js"></script>
  2.  
  3. <script type="text/javascript">// < !&#91;CDATA&#91;
  4.    CometClient = new Faye.Client('/comet');
  5.    CometClient.connect();
  6. // &#93;&#93;></script>
javascript

  1. CometClient.subscribe('/path/to/channel', function(message) {
  2. // process received message object
  3. });
  4.  
  5. CometClient.publish('/some/other/channel', {foo: 'bar'});

And the backend….

javascript

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

Nice!

Posted by Dion Almaer at 6:24 am
Comment here

+++--
3.8 rating from 20 votes

Comments Here »

Comments feed TrackBack URI

Leave a comment

You must be logged in to post a comment.