new Server(settings)
Eureca server constructor
This constructor takes an optional settings object
Parameters:
Name | Type | Argument | Description |
---|---|---|---|
settings | object | optional | have the following properties |
Properties:
Name | Type | Argument | Default | Description |
---|---|---|---|---|
settings.transport | string | optional | engine.io | can be "engine.io", "sockjs", "websockets", "faye" or "browserchannel" by default "engine.io" is used |
settings.authenticate | function | optional | If this function is defined, the client will not be able to invoke server functions until it successfully call the client side authenticate method, which will invoke this function. |
|
settings.serialize | function | optional | If defined, this function is used to serialize the request object before sending it to the client (default is JSON.stringify). This function can be useful to add custom information/meta-data to the transmitted request. |
|
settings.deserialize | function | optional | If defined, this function is used to deserialize the received response string. |
Examples
# default instantiation
var Eureca = require('eureca.io');
//use default transport
var server = new Eureca.Server();
# custom transport instantiation
var Eureca = require('eureca.io');
//use websockets transport
var server = new Eureca.Server({transport:'websockets'});
# Authentication
var Eureca = require('eureca.io');
var eurecaServer = new Eureca.Server({
authenticate: function (authToken, next) {
console.log('Called Auth with token=', authToken);
if (isValidToekn(authToken)) next(); // authentication success
else next('Auth failed'); //authentication fail
}
});
Namespaces
Methods
-
attach()
-
Sends exported server functions to all connected clients
This can be used if the server is designed to dynamically expose new methods.Parameters:
Type Description appServer a nodejs nodejs http server
or expressjs Application -
getClient(id) → {Proxy}
-
This method is used to get the client proxy of a given connection.
it allows the server to call remote client functionParameters:
Name Type Description id String client identifier
Returns:
ProxyExample
//we suppose here that the clients are exposing hello() function //onConnect event give the server an access to the client socket server.onConnect(function (socket) { //get client proxy by socket ID var client = server.getClient(socket.id); //call remote hello() function. client.hello(); }
-
updateClientAllowedFunctions(id)
-
!! Experimental !!
force regeneration of client remote function signatures
this is needed if for some reason we need to dynamically update allowed client functions at runtimeParameters:
Name Type Description id String client identifier
-
updateContract()
-
!! Experimental !!
Sends exported server functions to all connected clients
This can be used if the server is designed to dynamically expose new methods.
Events
-
connect
-
Triggered each time a new client is connected
Properties:
Name Type Description socket ISocket client socket.
-
disconnect
-
triggered when the client is disconneced.
Properties:
Name Type Description socket ISocket client socket.
-
error
-
triggered if an error occure.
Properties:
Name Type Description error String the error message
socket ISocket client socket.
-
message
-
Triggered each time a new message is received from a client.
Properties:
Name Type Description message String the received message.
socket ISocket client socket.