Making asynchronous calls with Ruby

745 views
Skip to first unread message

Siddharth Ravichandran

unread,
Nov 11, 2011, 6:00:09 AM11/11/11
to BANGALORE RUG-Ruby Users Group
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.

1. User types message (Browser Chat Client)
2. Message arrives to the server via XMPP
3. Message is sent to the API
4. Response is received
5. Response is sent back to the chat client.

My code for this is as follows

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

My concern here is that since the callback is event based 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?
Another question is am I right to be concerned of this being a
bottleneck considering the requests arriving maybe be every 10 seconds
if not more?

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.

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.

Sidu Ponnappa

unread,
Nov 11, 2011, 7:45:52 AM11/11/11
to bangal...@googlegroups.com

Depending on the total volume of the async calls, you may want to evaluate Wrest. http://github.com/c42/wrest

Best,
Sidu.
Sent from my phone

Pradeep Elankumaran

unread,
Nov 11, 2011, 11:54:31 AM11/11/11
to bangal...@googlegroups.com
Look into using XMPP components. You can write them in any language and attach them to the XMPP server, waiting for particular stanzas. Once you get a matching stanza, fire the API call, then publish another stanza to the client directly once the call is done.
Reply all
Reply to author
Forward
0 new messages