hemerajs / hemera

🔬 Writing reliable & fault-tolerant microservices in Node.js https://hemerajs.github.io/hemera/
MIT License
806 stars 70 forks source link

client call code must in hemera.ready() method? #72

Closed arden closed 7 years ago

arden commented 7 years ago

client call code must in hemera.ready() method?

hemera.ready(() => {
  hemera.act({
    topic: 'math',
    cmd: 'add',
    a: 1,
    b: 2
  }, function (err, resp) {
    this.log.info(resp, 'Result')
  })
})

client call can't as:

  hemera.act({
    topic: 'math',
    cmd: 'add',
    a: 1,
    b: 2
  }, function (err, resp) {
    this.log.info(resp, 'Result')
  })

directly?

StarpTech commented 7 years ago

Your question is: "Can I send a message before the connection is ready? “ the answer is No. But this time is very short you just have to care about it if you call instantly.

arden commented 7 years ago

like senecajs can do it . call instantly. if i'm use senecajs in express or koa is very simple. because the client call have many time, but not only one time.

arden commented 7 years ago

like this it's work ok too: server.js

'use strict'

const Hemera = require('./../packages/hemera')
const nats = require('nats').connect()

const hemera = new Hemera(nats, {
  logLevel: 'info'
})

  hemera.add({
    topic: 'math',
    cmd: 'add1'
  }, function (req, cb) {
    cb(null, req.a * req.b)
  })

client.js

'use strict'

const Hemera = require('./../packages/hemera')
const nats = require('nats').connect()

const hemera = new Hemera(nats, {
  logLevel: 'info'
})

  hemera.act({
    topic: 'math',
    cmd: 'add1',
    a: 2,
    b: 2
  }, function (err, resp) {
    this.log.info(resp, 'Result')
  })
StarpTech commented 7 years ago

@arden seneca doesn't rely on a messaging bus it doesn't need an agreement with third party. If you use seneca rabbitmq plugin you have to wait too until the connection is ready.

According to your example above: It can work but it is not guaranteed because its a timing issue.