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.