pnxtech / hydra

A light-weight library for building distributed applications such as microservices
https://www.hydramicroservice.com
MIT License
645 stars 54 forks source link

Combine REST with Socket protocol in microservice architecture #157

Closed tybq93-zz closed 6 years ago

tybq93-zz commented 6 years ago

hi, firstly, i must say this is super cool project, and i very love it. But i have some confuse, if you have time, please explain clearly for me.

i want to build a microservice, Client will call to onemain service by HTTP, and then main service call to another service by message (websocket). But how i can skip the HTTP request and wait the message go to all other services, get the response and send back to to client :( i want to use websocket to communication between internal microserivce because it's faster than HTTP protocol

many thank guys

tybq93-zz commented 6 years ago

@cjus do you have any idea about it? :D many thank

cjus commented 6 years ago

@tybq93 since this isn't a bug but rather a question about architecture I invite you to join our slack channel. While you're considering that I can say that HTTP and messages (socket) can be combined at will. You can have an HTTP endpoint which in turns sends another service a message via hydra.sendMessage.

Hydra doesn't offer a messaging synchronization feature to keep track of messages sent and acknowledged. Rather it simply offers a message transmission and routing. To use socket only messaging you can create hydra message listeners in your services:

hydra.on('message', (message) => {
  // handle message / dispatch message
  this.myMessageHandlerAndDispacher(message);
});

When a message is received and you'd like to reply to it you can use hydra.sendMessageReply()

Also, you mentioned that you'd like to use WebSockets. Is that via HydraRouter or some other means?

tybq93-zz commented 6 years ago

yeah, i mean websocket is via HydraRouter. for example: step 1: client ----call http api to get list post---> API Service step 2: API Service ----sendMessage get list post to PostService ----> PostService step 3: PostService --- sendMessageReply back to APIService -----> API Service step 4: API Service ---send result back----> Client

but the problem here is we can not delay the step 1 request and wait util we have the response with result back on step 3 and then send back to user in step 4, so i think this is impossible, right?

I already sent you email to join slack channel, i'm very happy to discuss more about this with your team. :+1:

cjus commented 6 years ago

Correct that doesn't sound possible. It's an issue of whether you close the HTTP connection in step one or leave it open pending a response. The way we do this is that we create websocket connections to HydraRouter (from mobile devices) and then our microservices able to route message responses back out through hydraRouter to specific devices. This isn't fully documented and I need to do a write-up for anyone else wanting to do the same.

I sent you an invite to our Slack channel. See you there.