1. rechardchen
  2. tm-js

Wiki

Clone wiki

tm-js / Home

Welcome

Welcome to Task Manager (tm-js). This project is powered by NodeJS, socket.io.

The project aims to implement a real time application communication framework based on HTML5 websocket. But with socket.io, it also works for web apps which runs in the environment where websocket does not work(with flash or Comet).

Features

Simplicity. Just like the simplicity of HTML5 websocket api, the api of tm-js is easy to use.

Reliability. With socket.io as the transport layer, tm-js supplies reliable real-time and bidirectional connections.

Framework

tm-js flow framework

In tm-js framework, controllers connect to the worker they want through manager. Manager behaves as a gateway and balance the stress of workers. After connection established, controllers has a bidirectional communication channel with the worker.

Examples

A NodeJS Example

In Manager(it behaves as a gateway):

var tm = require('tm-js'), manager = tm.listen(8000);

In worker(which is on the same machine and behaves as a service):

var tm = require('tm-js'),
    server = new tm.Worker('localhost'); //our gateway is on localhost
server.on('connection',function(socket){
    console.log(socket.id,'has connected');
    socket.on('private message',function(msg){
        console.log('received: '+msg);
        socket.emit('private message','this message is from the server');
    });
    socket.on('disconnect',function(){
        console.log(socket.id,'disconnected');
    });

And in controller(coincidently it is on localhost!):

var tm = require('tm-js'),
    c = new tm.Controller({
            'host':'localhost',
            'port':8000
        });

c.on('connect',function(){
    console.log('connected to server');
    c.emit('private message','hello from controller');
});

A Browser Example

Because tm-js uses socket.io as base library, so we need to supply socket.io.js in the webpages. The follow shows a small httpServer written in NodeJS.

In manager :

var fs = require('fs'),
    tm = require('tm-js'),
    app = require('http').createServer(function(req,res){
        fs.readFile('index.html',function(err,data){
            res.writeHead(200,{'Content-Type':'text/html'});
            res.end(data);
        });
    });	

app.listen(8000);
tm.listen(app);

In index.html :

<!DOCTYPE html>
<html>
    <head>	
        <title>Test page</title>
        <script src="/socket.io/socket.io.js"></script>
        <script src="/tm-js/controller.js"></script>
        <script>
            var c = new Controller({
                       'host': 'localhost',
                       'port': 8000
                    });
            c.on('connect',function(){
                console.log('connected');
            });
        </script>
    </head>
    <body>
    </body>
</html>

Just have fun with this little tool!

Api Documentation

Updated