XMPP & AMQP in Ruby

8,677 views

Published on

An introductory look at XMPP and AMQP in the Ruby context. Includes a look at using them together.

Published in: Education, Technology
2 Comments
46 Likes
Statistics
Notes
No Downloads
Views
Total views
8,677
On SlideShare
0
From Embeds
0
Number of Embeds
97
Actions
Shares
0
Downloads
369
Comments
2
Likes
46
Embeds 0
No embeds

No notes for slide




























































































  • XMPP & AMQP in Ruby

    1. 1. XMPP & AMQP Asynchronous, Event-Driven Messaging
    2. 2. Technologies
    3. 3. XMPP Extensible Messaging and Presence Protocol Core of Jabber IM technology http://xmpp.org AMQP Advanced Message Queuing Protocol An Open Standard for Messaging Middleware http://amqp.org
    4. 4. Services RabbitMQ http://rabbitmq.com ejabberd http://jabber.im
    5. 5. Libraries tmm1-amqp http://github.com/tmm1/amqp xmpp4r-simple http://github.com/blaine/xmpp4r-simple
    6. 6. AMQP
    7. 7. Purpose
    8. 8. Message Process 1 Process 2
    9. 9. ? Message Process 1 Process 2
    10. 10. Cross-Process Messaging DRb (and Rinda) HTTP MySQL PStore AMQP
    11. 11. Cross-Process Messaging Ruby only DRb (and Rinda) client HTTP & server artificial MySQL solution naive PStore AMQP
    12. 12. Cross-Process Messaging AMQP designed for multiprocess messaging with a generic, extensible protocol
    13. 13. A Message Is an object of communication Any type of transmission, including marshaled Ruby objects (in JSON, for instance)
    14. 14. Messages # essentially Base64.encode(m.to_json) require 'json' # message = {quot;namequot; => quot;Bobquot;} payload = [message.to_json].pack(quot;m*quot;)
    15. 15. Messages # essentially JSON.parse(Base64.decode(p)) require 'json' # payload = quot;eyJuYW1lIjoiQm9iIn0=quot; message = JSON.load(payload.unpack(quot;m*quot;).first)
    16. 16. Usage
    17. 17. Libraries require 'rubygems' gem quot;eventmachinequot;, quot;0.12.2quot; gem quot;tmm1-amqpquot;, quot;0.6.0quot; require 'mq'
    18. 18. Queueing Messages EM.run do mq = MQ.new q = mq.queue('noises') q.publish('moo') end
    19. 19. Queueing Messages (simplified) EM.run do MQ.new.queue('noises').publish('moo') end
    20. 20. Subscribing to Queues EM.run do MQ.new.queue('noises').subscribe do |msg| puts msg end end
    21. 21. Full Example mq = MQ.new q = mq.queue('noises') q.subscribe do |msg| puts quot;Got: %squot; % msg.inspect end q.publish('moo')
    22. 22. Starting the EventMachine Reactor # required Thread.new{ EM.run{} } # optional for clean shutdown %w(INT TERM).each{ |sig| old = trap(sig){ EM.stop old.call if old.respond_to? :call } }
    23. 23. QUEUE MESSAGE! Process 1 Process 1 Msg NOTIFY SUBSCRIBER! RabbitMQ Msg Msg SUBSCRIBE Msg TO QUEUE! Process 2 Process 2
    24. 24. XMPP
    25. 25. Purpose
    26. 26. Message Machine 1 Machine 2
    27. 27. ? Message Machine 1 Machine 2
    28. 28. XMPP Message Message Delivery Machine 1 Machine 2 Identity Identity Persistent Connections Presence (State) Rosters (Friends)
    29. 29. Usage
    30. 30. Libraries require 'rubygems' gem quot;eventmachinequot;, quot;0.12.2quot; gem quot;xmpp4r-simplequot;, quot;0.8.8quot; require 'xmpp4r-simple'
    31. 31. Logging On client = Jabber::Simple.new(quot;test@example.comquot;, quot;passwordquot;)
    32. 32. Sending Messages client.deliver(quot;test@example.netquot;, quot;OH HAIquot;)
    33. 33. Receiving Messages client.received_messages do |msg| puts quot;From: %squot; % msg.from.to_s puts msg.body puts end
    34. 34. Handling Status Updates client.presence_updates do |jid, presence, msg| puts quot;%s is now %s (%s)quot; % [jid.to_s, presence, message] end
    35. 35. XMPP+AMQP
    36. 36. Event-Driven Architecture
    37. 37. Purpose
    38. 38. AMQP XMPP Process queue Msg 1 Machine Machine message 1 2..n subscriber AMQP notified RabbitMQ Process Process 2 XMPP 3..n message ejabberd message sent delivered
    39. 39. Similarities AMQP RabbitMQ XMPP ejabberd Erlang Ruby libraries asynchronous standardized local/remote extensible clusterable offline message queue
    40. 40. Differences AMQP RabbitMQ XMPP ejabberd binary transfer XML transfer faster slower push/pull push no presence presence subscribers members/rosters message fidelity persistent conn.
    41. 41. Usage
    42. 42. Rails Plugin require 'json' require 'mq' Thread.new{ EM.run{} } class Foo # queues message to be handed to external process def self.queue(message) payload = [message.to_json].pack(“m*”) MQ.new.queue('messages').publish(payload) end end
    43. 43. Background Process/Task require 'mq' require 'xmpp4r-simple' Thread.new{ EM.run{} } class Foo # redirect payload to recipient for processing def self.run @client ||= Jabber::Simple.new(quot;test@example.comquot;, quot;passwordquot;) MQ.new.queue('messages').subscribe do |payload| @client.deliver(@recipient, payload) end end end
    44. 44. External Process require 'json' require 'xmpp4r-simple' class Foo # periodically checks for and handles any messages def self.run @client ||= Jabber::Simple.new(quot;test@example.comquot;, quot;passwordquot;) EM.add_periodic_timer(3) do @client.received_messages do |msg| payload = msg.body message = JSON.load(payload.unpack(quot;m*quot;).first) # do something with message end end end end
    45. 45. Summary
    46. 46. use whichever you prefer and meets your needs use existing solution, don’t write what you don’t have to don’t be afraid to combine them have fun, experiment, fuck up

    ×