This page is dedicated to those with a bit of prior knowledge regarding how flows and services in microServiceBus.com works. If you are not familiar with these concepts yet, please visit the Getting started first.

When you as a developer is writing a service to use in a flow, you are extending the microservice object microServiceBus.com is exposing. This object has a number of functions and properties that could help you develop your code. First we will go through what is required in your own service, then what is available to you and lastly som best practices.

Required functions

  • Start()

    This function will be called when your node starts and your service has been downloaded.

    @parameters : none

    @returns : void

    @example

      Start : function(){
          let timerEvent = setInterval(function(){
              //start your reoccuring logic
          }, 10000)
      }
    

Tip! Here it is great to add all your NPM packages.

  • Stop()

    This function will be called when you disable or restart your node.

    @parameters : none

    @returns : void

    @example

      Stop : function(){
          ClearInterval(timerEvent);
      }
    

Tip! Here it is recommended to stop your timers or clear your processes.

  • Process()

    This function will be called when you send messages from another service to this

    @parameters : message , context

    @returns : void

    @example

      Process : function(){
          let newData = JSON.stringify(message);
      }
    

Tip! Here is where you can integrate your services to share data with each other.

Properties on the microservice object

    this.timezone // The current timezone of the device
    this.NodeName // The id of the node running the service
    this.Name // The name of the service
    this.settingsHelper // Has a lot of useful properties such as paths
    this.Com // Com includes many functions for making sure your device is connected as it should be 

Tip! These are just some examples of properties you can access. Check the source code at GitHub to find more. Search for new MicroService ;)

Functions on the microservice object

  • this.GetCurrentState()

    Returns the “device twin” or “shadow” of the device from the connected IoT Hub.

    @parameters : none

    @returns : Object

    @example

      Start : function(){
          //Fetch latest state
          let state = this.GetCurrentState();
          if(state.desired.temperature > state.reported.temperature){
              //Turn on the heater
          }
      }
    
  • this.AddNpmPackage(npmPackages, logOutput, callback)

    Downloads the packages in run time and calls the callback when download and installation is completed

    @parameters : npmPackages (string), logOutput (Boolean), callback (function)

    @returns : Void

    @example

          this.AddNpmPackage('request,serialport@7.0.0', true, function(err){
          if(!err){
              request = require('request');
          }
          else{
              self.ThrowError(null, '00001', 'Unable to install the npm packages');
              return;
          }
      });
    
  • this.SubmitMessage(msg, format, headers)

    Sends the message to the next service in the flow in a specified format with or without headers

    @parameters : msg (object or binary), format (string), headers (needs to be formatted as following : [{Variable : “[key]”, Value : “[value]”}])

    @returns : Void

    @example

         this.SubmitMessage({Sensor : "mySensor", Value : 22}, 'application/json', [{Variable : "messageType", Value : "tempSensor"}]);
    

This section will be updated with more in depth regarding services.

In the meantime check out our repo at GitHub if you want to explore the source yourself. Do not hesitate to send any question or open any issue on something that is unclear or not working as expected.

Back to home page: Home