LivelyKernel/LivelyKernel — pull request #126 by levjj (2013-03-20 03:29:05+00:00)
Title: Running LivleyKernel with NodeJS

This patch makes it possible to run parts of the Lively Kernel codebase on NodeJS. In contrast to other solutions, this approach create a pseudo-browser environment and runs the exact same code as the web browser. This makes it possible use the same test suite and to maintain compatibility.

Currently supported Lively features on NodeJS:

* Classes
* Modules
* Traits
* Persitence
* OMeta
* AST
* Bindings
* TestFramework

Morphic and other browser-dependent features might work to a certain degree but are not tested.

## Using Lively in a NodeJS application

### 1. Install LivelyKernel from Github

$ npm install https://github.com/LivelyKernel/LivelyKernel/tarball/master

Alternatively, this dependency can also be added to ``package.json`` and installed with ``npm install``.

### 2. Create Lively modules and classes

For example file could be called ``counter.js``:

```javascript
Object.subclass('MyCounter', {
initialize: function() {
this.i = 0;
},
inc: function() {
return ++this.i
}
});
```

### 3. Create NodeJS Entry Point

The following example creates a Lively context, loads the ``counter.js`` file above in the context and uses it as part of a simply webservice.

```javascript
var http = require('http'),
lively = require('livelykernel'),
counter;

lively.JSLoader.require('./counter').toRun(function() {
counter = new lively.MyCounter();
});

http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(String(counter.inc()));
}).listen(9102);
```