Share via


Node JS Bindings for RxJS

Today during my presentation at JSConf, I showed that RxJS is not only about the browser. RxJS can be used anywhere JavaScript is available. To showcase that, the webserver I used for my dictionary suggest sample, was entirely written in JavaScript, using node.js as the engine.

Attached to this post are the Rx bindings for node.js. With them you can use Rx to write asynchronous webservers. E.g. the Hello World sample from the homepage of the node.js project can be rewritten as follows:

var sys = require("sys");
var http = require('./rx_http');
var serverData = http.createServer();serverData.Delay(2000).Subscribe(function(details)
{
details.response.writeHead(200, { 'Content-Type': 'text/html' });
details.response.write("Hello World");
details.response.end();
});
serverData.server.listen(8000);
sys.puts('Server running at https://127.0.0.1:8000/');

of course, this is just a simple hello world. With operators like SelectMany and the bindings to all the file system operators, we can write great asynchronous webservers, but that is for a future post. Please let me know if you like these bindings. I’ll try to keep them up-to-date as both Rx.JS and node.js development continue.

To use these bindings, copy rx.js from the official Rx download and add the files from this zip to the same directory.

Comments

  • Anonymous
    April 17, 2010
    I really like how you are focusing on adding support for node.js as I think its a very interesting project with a bright future (maybe someone at MS can support the windows port?). But, I'm not liking this added un-predictable wrapper API around his server API. The request and response are now also hidden on the callback under the details object. This effectively forces users to read docs to find out how to use Rx node.js wrappers, which unless they have a natural desire to Rx-things won't bother. rx_http.createServer(); != http.createServer(); it's more like http.createRxWrappedServer(); //If its possible I would consider the following API if possible var server = http.createServer(); $Rx(server).Delay(2000).Subscribe(function(req,res) {  res.writeHead(200, {'Content-Type': 'text/plain'});  res.end('Hello Worldn'); }); server.listen(8000); On a side note, how much of Rx is open-source? Are the JavaScript libs all open source? I've read that the server libs are freely available but not open-source, is this still the case?

  • Anonymous
    April 19, 2010
    Hi Demis, sorry for the late reply, the JSConf was pretty much 24/7 :) I'd love to see a node.js windows port, will talk to our Open Source lab to give them a nice gentle push in that direction :) I'll see if I can change the createServer api to become a bit nicer. The subscribe won't work as there is too many operators that need to deal with a single value per source such as Zip, Join, ForkJoin etc... Rx has its own license which can be found on the Rx project page. Like Rx, the license is also under continuous development, so give feedback on the forums if there is anything blocking you from using it so we can make a stronger argument for potential changes. The nodejs wrappers are a sample that you should feel free to do with whatever you want. Jeffrey

  • Anonymous
    April 19, 2010
    The comment has been removed

  • Anonymous
    April 19, 2010
    Hi Demis, What is blocking Rx from running on mono? Rx doesn't use p/invokes or COM interop so we were assuming it would fine. Let me know if there is anything we need to change to get it running. Yup aware of perf requirements of mono, enjoyed Ryan's talk on the subject at jsconf. Will try to make the sample a little nicer this week. Thanks for your feedback! Jeffrey

  • Anonymous
    April 25, 2010
    Hey Jeff, I think it's a legal problem, the licence seems very restrictive and seems to forbid distribution of it. The mono crew are more than happy to announce any new shiny tech that comes their way (e.g. Silverlight toolkit, ASP.NET MVC, etc). When there is some cool like PLINQ there are usually efforts to implement it. Otherwise there are libraries like WPF which are just too big and require too much effort that they'll probably never be implemented which i sadly how I think Rx will go unless its donated. Unfortunately there's not a whimper on usage of Rx on Mono and with the license the way it is, I think it will need an official statement from Microsoft for it to ever be considered to run on that platform. I hear the codeplex foundation now provides an opportunity for Microsoft to be able to freely share some of their tech under a viable legal framework, maybe its worth exploring this option. Anyway the RxJs stuff looks pretty safe and am looking forward to your contributions in this area.

  • Anonymous
    April 16, 2011
    Hi The zip file no longer works. Is this binding no longer supported?

  • Anonymous
    May 26, 2011
    jeffrey.reactiveextensions.net/rx_nodejs.zip is no longer a valid link.

  • Anonymous
    May 29, 2011
    Bump.  It will be great if the link to rx_nodejs.zip can be fixed.  If there's a reason the file's no longer available it will be helpful (in a pedagogoical sense) to know why.

  • Anonymous
    August 15, 2011
    The comment has been removed