pnxtech/hydra

How to send UFM message from non-hydra server to hydra micraservice?

emjimadhu opened this issue · 9 comments

HI all, First of all thanks for this awesome service.

I have a expressjs app (non hydra), and two microservice (hydra-express). i want to send a request to microservice from express app. i tried sample message from documentation. but it gives error. can you guys point me out how to implement message from non-hydra app to hydra microservice?

cjus commented

@emjimadhu You can simply send an HTTP RESTful request from an expressjs app to any hydra-express app which has exposed an endpoint. The body of that message (assuming POST) can be a UMF message. Sending messages between hydra/hydra-express apps only needs the hydra.sendMessage call.

If you'd like to send websocket messages to hydra-express apps from other services you can also do that via our Hydra-Router service. See: https://www.hydramicroservice.com/docs/tools/hydra-router/

If you'd like to engage with others using Hydra consider joining our Slack channel. More info here: https://github.com/flywheelsports/hydra#join-us-on-slack

@sjmcdowall @cjus thanks for the reply guys.

@sjmcdowall here is what i am doing

in my routes

`const hydra = require('hydra')
const consts = require(__base + 'utils/consts')

module.exports = (req, res) => {
hydra.init(consts.hydra)
.then(() => {
console.log('hydra initiated')
return hydra.findService('firebase-service')
})
.then((service) => {
console.log(service)
res.send(service)
})
.catch((error) => {
console.log(error)
res.send(error)
})
}`

and this is my config file

module.exports = { port: process.env.PORT || 1337, hydra: { serviceName: '', serviceDescription: '', serviceIP: '', servicePort: 0, serviceType: '', redis: { host: '127.0.0.1', port: 6379, db: 15 } } }

@sjmcdowall @cjus i followed this guide to intialize hydra in non-service app https://www.hydramicroservice.com/docs/hydra-core/getting-started.html

as you guys stated in their i left all the service related fields as empty strings except redis.

when i run this code i get this message.

Error: Config missing serviceName or servicePort at Promise (/home/batman/Projects/finball/finball-server/finball-parse-server/node_modules/hydra/index.js:185:16) at Promise._execute (/home/batman/Projects/finball/finball-server/finball-parse-server/node_modules/hydra/node_modules/bluebird/js/release/debuggability.js:300:9) at Promise._resolveFromExecutor (/home/batman/Projects/finball/finball-server/finball-parse-server/node_modules/hydra/node_modules/bluebird/js/release/promise.js:483:18) at new Promise (/home/batman/Projects/finball/finball-server/finball-parse-server/node_modules/hydra/node_modules/bluebird/js/release/promise.js:79:10) at IHydra.init (/home/batman/Projects/finball/finball-server/finball-parse-server/node_modules/hydra/index.js:115:25) at IHydra.init (/home/batman/Projects/finball/finball-server/finball-parse-server/node_modules/hydra/index.js:1892:18) at config.init.then (/home/batman/Projects/finball/finball-server/finball-parse-server/app/routes/general/ufm-message/index.js:11:17) at tryCatcher (/home/batman/Projects/finball/finball-server/finball-parse-server/node_modules/fwsp-config/node_modules/bluebird/js/release/util.js:16:23) at Promise._settlePromiseFromHandler (/home/batman/Projects/finball/finball-server/finball-parse-server/node_modules/fwsp-config/node_modules/bluebird/js/release/promise.js:512:31) at Promise._settlePromise (/home/batman/Projects/finball/finball-server/finball-parse-server/node_modules/fwsp-config/node_modules/bluebird/js/release/promise.js:569:18) at Promise._settlePromise0 (/home/batman/Projects/finball/finball-server/finball-parse-server/node_modules/fwsp-config/node_modules/bluebird/js/release/promise.js:614:10) at Promise._settlePromises (/home/batman/Projects/finball/finball-server/finball-parse-server/node_modules/fwsp-config/node_modules/bluebird/js/release/promise.js:693:18) at Async._drainQueue (/home/batman/Projects/finball/finball-server/finball-parse-server/node_modules/fwsp-config/node_modules/bluebird/js/release/async.js:133:16) at Async._drainQueues (/home/batman/Projects/finball/finball-server/finball-parse-server/node_modules/fwsp-config/node_modules/bluebird/js/release/async.js:143:10) at Immediate.Async.drainQueues (/home/batman/Projects/finball/finball-server/finball-parse-server/node_modules/fwsp-config/node_modules/bluebird/js/release/async.js:17:14) at runCallback (timers.js:789:20) at tryOnImmediate (timers.js:751:5) at processImmediate [as _immediateCallback] (timers.js:722:5)
i dont know how to proceed from here.

@cjus yes, i can use normal api request from non hydra app. but i chose random ports for microservices, so i need to know the port number assigned for that particular service.

Thanks.

cjus commented

@emjimadhu serverName cannot be blank.

@cjus hey thank you for the reply. the server i am communicating from is not a service. just like it said in the below image, i made it blank. or am i understood wrong.

screenshot from 2018-02-17 11-30-29

if i am wrong, what i have to do next? my scenario is i have a server and i want to communicate with hydra microservice.

cjus commented

@emjimadhu I see. I also went back to take a closer look at your sample program above. So you're initializing hydra in an express route? and trying to use the hydra. findService call to locate the firebase-service?

I recommend initializing hydra in your app initialization (main js file) and then importing hydra in your route file using:

const hydra = require('hydra');

Then later you can call hydra.findService('firebase-service')

I'll write a small test program to ensure this still works and I'll share that with you.

cjus commented

@emjimadhu So there is an issue with the documentation or perhaps I'll fix in an updated hydra release. serviceName is required and can't currently be blank. So here is a sample config.json file which does contain a serviceName.

{
  "hydra": {
    "serviceName": "caller",
    "serviceDescription": "",
    "serviceIP": "",
    "servicePort": 0,
    "serviceType": "",
    "redis": {
      "url": "redis://localhost:6379/15"
    }
  }
}

This Sample program shows a basic express app which loads hydra and attempts to locate information about hydra-routers which may be present. In your case that would be your firebase-service. In your earlier example you used the hydra.findService() call. But I think you probably need to use the hydra.getServicePresence() call which returns an array of all present service instances along with ip and port addresses. At the end of this post I shared the output of the program below.

const express = require('express');
const app = express();
const hydra = require('hydra');
const config = require('./config.json');

app.get('/', (req, res) => res.send('Hello World!'));
app.listen(3000, () => console.log('Example app listening on port 3000!'));

hydra.init(config)
  .then(() => {
    console.log('hydra initiated');
    return hydra.getServicePresence('hydra-router');
  })
  .then((service) => {
    console.log(service);
  })
  .catch((error) => {
    console.log(error)
  });

Output:

Array(1) [Object]
index.js:15
length:1
__proto__:Array(0) [, …]
0:Object {serviceName: "hydra-router", serviceDescription: "Service Router", version: "1.6.0", …}
  hostName:"AdministorsMBP2.fios-router.home"
  instanceID:"46bb1d0037264eb0898adde39cdd257e"
  ip:"192.168.1.221"
  port:1552
  processID:10750
  serviceDescription:"Service Router"
  serviceName:"hydra-router"
  updatedOn:"2018-02-19T03:55:52.114Z"
  updatedOnTS:1519012552114
  version:"1.6.0"
__proto__:Object {constructor: , __defineGetter__: , __defineSetter__: , …}

@cjus Thank you so much. It worked.

cjus commented

You're welcome! I'm closing out this issue.