title: Asynchronous methods calls with Ruby like with Ajax
body: <p>I am working with XMPP and I have a message callback which is activated on the event of every message being sent. My aim is to send the data arriving by the message to an API within the callback and based on the response send something back using the XMPP client.</p>
  
  <ol>
  <li>User type message (Browser Chat Client)</li>
  <li>Message arrives to the server via XMPP </li>
  <li>Message is sent to the API</li>
  <li>Response is received </li>
  <li>Response is sent back to the chat client.</li>
  </ol>
  
  <p>My code for this is as follows</p>
  
  <pre><code>admin_muc_client.activate_message_callbacks do |m|            
    sender    = m.from.resource
    room_slug = m.from.node
    message   = m.body                  
  
    r = HTTParty.get('http://example.com/api/v1/query?msg=message')
    Rails.logger.debug(r.inspect)
    admin_muc_client.send_message("Message #{r.body}") if m.from.resource != 'admin'
  end
  </code></pre>
  
  <p>My concern here is that since the callback is evented and the request to the API would be a blocking call this could become a bottleneck for the entire application. 
  I would prefer to use something like AJAX for Javascript which would fire the request and when the response is available send data. How could I implement that in Ruby?</p>
  
  <p>I have looked at delayed_job and backgroundrb which look like tools for fire and forget operations. I would need something that activates a callback in an asynchronous manner with the response. </p>
  
  <p>I would really really appreciate some help on how to achieve the asynchronous behavior that i want. I am also familiar with message queues like RabbitMQ which I feel would be addition of significant complexity. </p>