yatish mehta
yatish mehta

Reputation: 915

Parallel request from rails controller action

I have a usecase where when an action of the controller is called, it triggers 3 http request to other urls. I need to parse the response of those requests , combine them and then render the view. How do I trigger 3 request in parallel and not sequentially so that it saves time?

Upvotes: 3

Views: 882

Answers (1)

AnkitG
AnkitG

Reputation: 6568

I use Typhoes for handling parallel calls.

Jump to making parallel calls section to see how you can make parallel calls.

Example

 # Assume these were your two HTTP calls
 first_request = Typhoes::Request.new('http://jsonplaceholder.typicode.com/posts?userId=2')
 second_request = Typhoes::Request.new('http://jsonplaceholder.typicode.com/posts?userId=3')

 # Initialize a Hydra queue (this holds typhoes requests)
 hydra = Typhoeus::Hydra.hydra
 hydra.queue [first_request, second_request]
 hydra.run # this triggers the calls

 # Getting response, once the run is complete

 first_request.response 
 second_request.response 

Upvotes: 2

Related Questions