eureca.io

Author Alaa-eddine K.

a nodejs bidirectional RPC using engine.io or sockjs as transport layer

Project page https://github.com/Ezelia/eureca.io

npm install eureca.io

Server side

express server

var express = require('express')
  , app = express(app)
  , server = require('http').createServer(app);
var EurecaServer = require('eureca.io').EurecaServer;

var eurecaServer = new EurecaServer();

eurecaServer.attach(server);


//functions under "exports" namespace will be exposed to client side
eurecaServer.exports.hello = function () {
    console.log('Hello from client');
}
//------------------------------------------

//see browser client side code for index.html content
app.get('/', function (req, res, next) {
    res.sendfile('index.html');
});

server.listen(8000);

Client side

<!doctype html>
<html>
    <head>
        <title>Eureca.io test</title>
        <script src="/eureca.js"></script>
    </head>
    <body>    
        <script>
            var client = new Eureca.Client();

            client.ready(function (proxy) {
                proxy.hello();
            });
        </script>
    </body>
</html>
var EurecaClient = require('eureca.io').EurecaClient;
var client = new EurecaClient({ uri: 'http://localhost:8000/' });

client.ready(function (proxy) {
    proxy.hello();
});

Server side

// ... server initialisation
eurecaServer.exports.add = function (a, b) {
    return a+b
}

//... other stuff

Client side

client.ready(function (proxy) {
	//the onReady event is triggered when the remote side finishes
	//executing the called function.
	//returned value is stored in result argument
    proxy.add(10, 5).onReady(function(result) {
		console.log('got result from server, 10+5=', result);
	});
});

Server side

//we need to allow bar() function first
var eurecaServer = new EurecaServer({allow:['bar']});

// ... server initialisation
eurecaServer.exports.foo = function () {
	//when a server side function is called
	//we can access the client connection
	//throught this.connection
	
    var conn = this.connection;
    var client = eurecaServer.getClient(conn.id);
    console.log('called foo()');
    client.bar();
}

//... other stuff

Client side

client.exports.bar = function ()
{
    console.log('called bar()');
}

client.ready(function (proxy) {
    proxy.foo();
});

Server side

// ... server initialisation
eurecaServer.exports.math = {
	add = function (a, b) {
		return a+b
	},	
	sub = function (a, b) {
		return a-b
	}
}

//... other stuff

Client side

client.ready(function (proxy) {
	proxy.math.add(10, 5).onReady(function(result) {
		console.log('got result from server, 10+5=', result);
	});

	proxy.math.sub(10, 5).onReady(function(result) {
		console.log('got result from server, 10-5=', result);
	});
	
});

Events

Server side events

EurecaServer exposes four events

var EurecaServer = require('eureca.io').EurecaServer;
var eurecaServer = new EurecaServer();

eurecaServer.onConnect(function (connection) {
    
    console.log('new Client', connection.id);
    var client = eurecaServer.getClient(connection.id);
    
	//if we defined helloclient() function in the client side
	//we can call it like this
    client.helloclient();
    
});

eurecaServer.onMessage(function (msg) {
    console.log('RECV', msg);
});


eurecaServer.onDisconnect(function (connection) {    
    console.log('client %s disconnected', connection.id);
});

eurecaServer.onError(function (e) {    
    console.log('an error occured', e);
});

Client side events

EurecaClient exposes seven events

var EurecaClient = require('eureca.io').EurecaClient;
var client = new EurecaClient({ uri: 'http://localhost:8000/' });

//remote parameter contains the proxy able to call server side functions
client.ready(function(remote) {
	remote.helloServer();
});

client.onConnect(function (connection) {
    console.log('Incomming connection', connection.id);
});

client.onMessage(function (data) {
    console.log('Received data', data);
});

client.onError(function (e) {
    console.log('error', e);
});

client.onConnectionLost(function () {
    console.log('connection lost ... will try to reconnect');
});

client.onConnectionRetry(function (socket) {
    console.log('retrying ...');

});

client.onDisconnect(function (socket) {
    console.log('Client disconnected ', connection.id);

});