thorin.Interface.Transport

You can write your own transport layer by implementing thorin.Interface.Transport and calling thorin.addTransport()

Since Thorin transports are designed to abstract away incoming connections and connection handling, they must provide a common interface to make switching from one transport to another seamless and maintain your application transport agnostic. The transport interface also extends the native EventEmitter class, transforming your transport into an event emitter by default.

The configuration of a transport should be placed under the transport key of the application's root configuration object and under the transport's name property. A small example is available below.

'use strict';
// config/app.js file
module.exports = {
   "transport.http": {  // the "http" key is the transport's name property
      port: 12345
   }
};
Interface functionality
Properties
  • typeenum the type of a transport, see below
  • namestring the name of the transport when constructed.
A transport can have the following types:
  • Transport.TYPE.BI_DIRECTIONAL [1], the transport can process both incoming and outgoing traffic (eg. websocket)
  • Transport.TYPE.RECEIVER [2], the transport will only process inbound traffic, acting as a receiver (eg. http)
  • Transport.TYPE.SENDER [3], the transport will only process outbound traffic, acting as a sender (eg. push notifications)
constructor()
It is mandatory that you call the super() constructor from your class, so that it can initiate any other dependencies of the transport class.
static publicName() : string
Returns the publicly available name of your transport, eg. Web Server
setName(name)
Manually set the name of a transport's instance. By default, it will change the name property.
  • namestring the name of the transport instance
sendIntent(intentObj) : Promise
The function should be used by bi-directional or sender transports, that implement their custom logic for sending intents from the server to the client. The configuration of an intent and its settings is specific for each transport that implements it. The function should return a promise.
  • intentObj instance of thorin.Intent the intent that encapsulates data that will be sent.
routeAction(actionObj)
The function is called whenever an action is registered in the application's dispatcher. A receiver or bi-directional transport therefore must implement its custom functionality to handle the internal action routing. Transports that do not process actions can simply override the function and return undefined
  • actionObjinstance of thorin.Action the action object that contains the call stack definition of an action processing.
disableAction(name)
The function is called when an action is disabled programatically from within the application and should temporarily stop the handling of any incoming event that matches the given action. This should be implemented by receiver and bi-directional transports.
  • namestring the action name that should be disabled.
enableAction(name)
The function is called when an action was previously disabled programatically from within the application and should be enabled again. This should be implemented by receiver and bi-directional transports.
  • namestring the action name that should be enabled.
Example
'use strict';
class ThorinTransportHttp extends thorin.Interface.Transport {
   // Our constructor function
   constructor() {
      super();
      this.name = 'http';
      this.server = null;
   }
   publicName() { return "Web server"; }
   
   // Called when transport configuration is available
   init(config) {
      this.config = config;
   }
   
   // This is where we should bind our server and listen to a port
   run(done) {
      // initiate server and bind it.
      done();
   }
   
   // Register an action with the server's handlers
   routeAction(actionObj) {
      // server specific functionality for routing.
   }
   
   // Toggle an action processing
   disableAction(actionName) {
      // remove or disable the action from the server's handlers
   }
   enableAction(actionName) {
      // enable the processing of the given action
   }
}
thorin.addTransport(ThorinTransportHttp, 'http');
Do you have a question or is something missing?

You can always create a new issue on GitHub or contact one of the core founders by chat.