XMPP & AMQP in Ruby

  • 7,781 views
Uploaded on

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

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

More in: Education , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
7,781
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
358
Comments
2
Likes
45

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide




























































































Transcript

  • 1. XMPP & AMQP Asynchronous, Event-Driven Messaging
  • 2. Technologies
  • 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. Services RabbitMQ http://rabbitmq.com ejabberd http://jabber.im
  • 5. Libraries tmm1-amqp http://github.com/tmm1/amqp xmpp4r-simple http://github.com/blaine/xmpp4r-simple
  • 6. AMQP
  • 7. Purpose
  • 8. Message Process 1 Process 2
  • 9. ? Message Process 1 Process 2
  • 10. Cross-Process Messaging DRb (and Rinda) HTTP MySQL PStore AMQP
  • 11. Cross-Process Messaging Ruby only DRb (and Rinda) client HTTP & server artificial MySQL solution naive PStore AMQP
  • 12. Cross-Process Messaging AMQP designed for multiprocess messaging with a generic, extensible protocol
  • 13. A Message Is an object of communication Any type of transmission, including marshaled Ruby objects (in JSON, for instance)
  • 14. Messages # essentially Base64.encode(m.to_json) require 'json' # message = {quot;namequot; => quot;Bobquot;} payload = [message.to_json].pack(quot;m*quot;)
  • 15. Messages # essentially JSON.parse(Base64.decode(p)) require 'json' # payload = quot;eyJuYW1lIjoiQm9iIn0=quot; message = JSON.load(payload.unpack(quot;m*quot;).first)
  • 16. Usage
  • 17. Libraries require 'rubygems' gem quot;eventmachinequot;, quot;0.12.2quot; gem quot;tmm1-amqpquot;, quot;0.6.0quot; require 'mq'
  • 18. Queueing Messages EM.run do mq = MQ.new q = mq.queue('noises') q.publish('moo') end
  • 19. Queueing Messages (simplified) EM.run do MQ.new.queue('noises').publish('moo') end
  • 20. Subscribing to Queues EM.run do MQ.new.queue('noises').subscribe do |msg| puts msg end end
  • 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. 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. QUEUE MESSAGE! Process 1 Process 1 Msg NOTIFY SUBSCRIBER! RabbitMQ Msg Msg SUBSCRIBE Msg TO QUEUE! Process 2 Process 2
  • 24. XMPP
  • 25. Purpose
  • 26. Message Machine 1 Machine 2
  • 27. ? Message Machine 1 Machine 2
  • 28. XMPP Message Message Delivery Machine 1 Machine 2 Identity Identity Persistent Connections Presence (State) Rosters (Friends)
  • 29. Usage
  • 30. Libraries require 'rubygems' gem quot;eventmachinequot;, quot;0.12.2quot; gem quot;xmpp4r-simplequot;, quot;0.8.8quot; require 'xmpp4r-simple'
  • 31. Logging On client = Jabber::Simple.new(quot;test@example.comquot;, quot;passwordquot;)
  • 32. Sending Messages client.deliver(quot;test@example.netquot;, quot;OH HAIquot;)
  • 33. Receiving Messages client.received_messages do |msg| puts quot;From: %squot; % msg.from.to_s puts msg.body puts end
  • 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. XMPP+AMQP
  • 36. Event-Driven Architecture
  • 37. Purpose
  • 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. Similarities AMQP RabbitMQ XMPP ejabberd Erlang Ruby libraries asynchronous standardized local/remote extensible clusterable offline message queue
  • 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. Usage
  • 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. 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. 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. Summary
  • 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