Wiki

Clone wiki

tm-js / FullApi

API Documentation


In the following context, we use the convention:

tm = require('tm-js');

Module Methods


tm.listen(app_or_port):

  • This method needs one parameter: app or port. It can be passed in an app instance which is returned by http.createServer() or express.createServer(), or just a port, for example 8000.
  • It returns nothing!

Worker Instance


Worker(host):

  • This constructor accepts a parameter that specifies the hostname of the gateway/manager.
  • Returns a Worker Object


Worker.profile:

  • The profile object of this worker. Manager choose connections for controllers according to this field of workers.
  • If changed during worker running, you must call Worker.updateProfile to let Manager know that.


Worker.updateProfile():

  • This method update the profile of worker in Manager's database.


Worker.broadcast(evtType,...):

  • Broadcast an event to all connections.

Worker Events


connection:

  • Emitted when a connection from a remote controller is built.
  • Handler: function handler(conn)


disconnect:

  • Emitted when the worker is disconnected from the manager server
  • Handler: function handler()


connect:

  • Emitted when the worker is connected to the manager server
  • Handler: function handler()

Connection Instance


Connection.emit(evtType,...):

  • Emit an event(other than 'connect' or 'disconnect') to the controller
  • The evtType can be any customized string, and following arguments can be anything that can be serialized as JSON.


Connection.on(evtType,handler):

  • Register event handler


Connection.broadcast(evtType,...):

  • broadcast an event to all the controllers except this one


Connection.disconnect():

  • Break the connection

Connection Events


connect:

  • Emitted when connection is built
  • Handler: function handler()


disconnect:

  • Emitted when disconnected
  • Handler: function handler()

Controller Instance


Controller(hostDetails [,options]):

  • The parameter hostDetails is an object that specifies the host and port of the manager server
  • The second parameter options is not required, it is an object which specifies which worker to connect


Controller.disconnect():

  • Break connection from server


Controller.emit(evtType,...):

  • Emit an event to worker


Controller.on(evtType,handler):

  • Register event handler for the controller


Controller Events


connect:

  • Emitted when connection is built
  • Handler: function handler()


disconnect:

  • Emitted when disconnected
  • Handler: function handler()

Updated