Coordinatioin Launguage "Linda" implementation for Node.js and Socket.IO
% npm install linda-socket.io
- Node.js
- Socket.IO
Linda is a coordination launguage for parallel programming.
Shared memory on Node.js server.
- write( tuple , options )
- put a Tuple into the TupleSpace
- options = {expire : 300} # => expire after 300 sec
- take( tuple, callback(err, tuple) )
- get a matched Tuple from the TupleSpace and delete
- read( tuple, callback(err, tuple) )
- get a matched Tuple from the TupleSpace
- watch( tuple, callback(err, tuple) )
- overwatch written Tuples in the TupleSpace
- https://github.com/shokai/linda-socket.io/tree/master/samples
- https://github.com/shokai/linda-job-queue-sample
% git clone https://github.com/shokai/linda-socket.io.git
% cd linda-socket.io
% npm install
% npm install -g grunt-cli coffee-script
% coffee samples/chat/server.coffee 3000
% coffee samples/job-queue/server.coffee 3000
Server Side (node.js)
var http = require('http');
var app_handler = function(req, res){
// your web app code
};
var app = http.createServer(app_handler);
var io = require('socket.io').listen(app);
var linda = require('linda-socket.io').Linda.listen({io: io, server: app});
app.listen(3000);
console.log("server start - http://localhost:3000");
Client Side (web browser)
<script src="/socket.io/socket.io.js"></script>
<script src="/linda/linda-socket.io.js"></script>
var socket = io.connect("http://localhost:3000");
var linda = new Linda().connect(socket);
Client Side (node.js)
var LindaClient = require('linda-socket.io').Client;
var socket = require('socket.io-client').connect('http://localhost:3000');
var linda = new LindaClient().connect(socket);
job client
// connect to tuplespace (shared memory)
var ts = linda.tuplespace("calc");
// request
$("#btn_request").click(function(){
ts.write({type: "request", query: "1-2+3*4"});
});
// wait result
socket.on('connect', function(){
// overwatch Tuple
ts.watch({type: 'result'}, function(err, tuple){
if(err) return;
console.log(tuple.data.result); // => "1-2+3*4 = 11"
});
});
job worker
// connect to tuplespace (shared memory)
var ts = linda.tuplespace("calc");
// calculate
var work = function(){
ts.take({type: 'request'}, function(err, tuple){
if(!err){
var result = eval(tuple.data.query); // => "1-2+3*4"
console.log(tuple.data.query+" = "+result); // => "1-2+3*4 = 11"
ts.write({type: 'result', result: result}); // return to 'client' side
}
work(); // recursive call
});
};
socket.on('connect', function(){ // Socket.IO's "connect" event
work();
});
see more samples
% npm install
% npm install -g grunt-cli coffee-script
% grunt test
watch
% grunt
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request