open-index/open-npm huggingface dataset:
npm package: @idsa/platform
Description: Microservice cluster client based on gRPC and rabbitMQ
Author: Michal Blaha
Latest version: 2.0.2

## Microservice cluster client
```
  ___ ___  ___   _     ___ _      _    __               
 |_ _|   \/ __| /_\   | _ \ |__ _| |_ / _|___ _ _ _ __  
  | || |) \__ \/ _ \  |  _/ / _` |  _|  _/ _ \ '_| '  \ 
 |___|___/|___/_/ \_\ |_| |_\__,_|\__|_| \___/_| |_|_|_|
```
## Install
To install, use npm.
```bash
npm install -S @idsa/platform
```

You can use flow-typed - *add in `.flowconfig`*
```
[libs]
node_modules/@idsa/platform/flow-typed
```

Add in your code:
```js
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
```js
app.expose({ role: 'math', cmd: 'sum' }, (msg) => {
  return { answer: msg.left + msg.right };
  // Or return Promise
});
```

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

### Send event to the Message Bus
```js
app.publish('info.hello', { message: 'Hello world' })
  .then(() => console.log('ok'));
```

### Consume events
For multiple worker instances - Every message will be delivered exactly once.
```js
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.
```js
app.consumeExclusive('info.*', (routingKey, msg) => {
  console.log(msg);
  return Promise.resolve();
});
```

## API reference
See Typescript [Library Definition](src/types/platform.d.ts)