@idsa/platform
TypeScript icon, indicating that this package has built-in type declarations

2.0.2 • Public • Published

Microservice cluster client

  ___ ___  ___   _     ___ _      _    __               
 |_ _|   \/ __| /_\   | _ \ |__ _| |_ / _|___ _ _ _ __  
  | || |) \__ \/ _ \  |  _/ / _` |  _|  _/ _ \ '_| '  \ 
 |___|___/|___/_/ \_\ |_| |_\__,_|\__|_| \___/_| |_|_|_|

Install

To install, use npm.

npm install -S @idsa/platform

You can use flow-typed - add in .flowconfig

[libs]
node_modules/@idsa/platform/flow-typed

Add in your code:

import platform from '@idsa/platform';
const app = platform({
  bases: process.env.BASES || 'tcp://app1.idsa.vpn:39000,app1.idsa.vpn:39001',
  amqp: process.env.AMQP || 'amqp://rabbit1.idsa.vpn:5672',
});

// Here you have to call app.expose

app.connect()
  .then(() => console.log('Started!'));

Expose method into the cluster

app.expose({ role: 'math', cmd: 'sum' }, (msg) => {
  return { answer: msg.left + msg.right };
  // Or return Promise
});

Call method

app.request({ role: 'math', cmd: 'sum' }, { left: 1, right: 2 })
  .then(result => console.log(result.answer));
}

Send event to the Message Bus

app.publish('info.hello', { message: 'Hello world' })
  .then(() => console.log('ok'));

Consume events

For multiple worker instances - Every message will be delivered exactly once.

app.consume('info.*', (routingKey, msg) => {
  console.log(msg);
  return Promise.resolve();
});

Consume events for BFF

For multiple worker instances - It creates personal queue for each instance. Every message will be delivered to each of the instances. Queue will be destroyed when instance stopped.

app.consumeExclusive('info.*', (routingKey, msg) => {
  console.log(msg);
  return Promise.resolve();
});

API reference

See Typescript Library Definition

Readme

Keywords

none

Package Sidebar

Install

npm i @idsa/platform

Weekly Downloads

0

Version

2.0.2

License

MIT

Last publish

Collaborators

  • michalb