Skip to content

9. IO Module

Jonathan Casarrubias edited this page Oct 5, 2017 · 1 revision

LoopBack SDK Builder

##IO Module

The @mean-expert/loopback-sdk-builder now implements a way to send messages from any direction to any direction.

By using the PubSub functionality, the real-time feature is only triggered when calling a remote method through the REST Interface. This is a limitation that I needed to address.

For that reason I'm introducing a new feature that allows you to send any custom messages by using the same drivers from the PubSub and FireLoop Modules, but with the flexibility for you to manage any very specific behavior you need.

##Sending Messages from Back End Boot Scripts The following is a message sent from a boot script to any client

module.exports = function (app) {
  app.on('started', function () {
    app.mx.IO.emit('test', 'Hello World');
    app.mx.IO.on('test', function (message) {
      console.log('MESSAGE: ', message);
    });
  });
};

##Sending Messages from Models The following is a message sent from a model hook to any client

module.exports = function(Model) {
  Model.observe('after save', (ctx, next) => {
    Model.app.mx.IO.emit('custom-event', ctx.instance);
    next();
  });
};

##Sending and Receiving Messages from Front End Application

import { Component } from '@angular/core';
import { LoggerService } from './shared/sdk/services';
import { RealTime } from './shared/sdk/services';

@Component(...)

export class AppComponent {

  constructor(
    private logger: LoggerService,
    private realTime: RealTime
  ) {
    this.realTime.IO.emit('hello', 'world');
    this.realTime.IO.on('hello').subscribe((msg: any) => this.logger.info('REALTIME: ', msg));
  }
}
Clone this wiki locally