Real-time Web with Railsand XMPPMünchner Ruby on Rails User Group, 2010/12/2Li Cai (ca168168@gmail.com)founder: reliwa.dep...
Motivation: 16ds.com• B2B portal and business network for the  chemical industry• Market indexes for 30+ bulk chemicals  b...
Multi-party price negotiation•   User self-organized•   Access control based on organizational structure of the    company...
Server push in Rails•   Polling: simple (but...)•   Comet: setup external server for server push (Jetty,    glassfish, come...
XMPP•   eXtensible Messaging and Presence Protocol•   Data exchanged over a pair of XML streams    •   extensibility: very...
XMPP•   BOSH (XEP-0124, XEP-0206)    •   long-lived bidirectional streams over HTTP connection    •   hide user credential...
Extend XMPP with our newfeatures•   Build multi-party price negotiation on top of MUC•   Create XMPP account for every use...
Rails and XMPP                                Application                        Background  Web Client   HTTP Server     ...
XMPP servers•   ejabberd    •   written in Erlang, well-known for its scalability    •   http://www.process-one.net/en/eja...
XMPP libraries - server side•   xmpp4r    •   feature-rich, threaded ruby    •   http://home.gna.org/xmpp4r/•   blather   ...
XMPP libraries - client side•   strophe.js    •   JavaScript    •   http://code.stanziq.com/strophe/•   xmpp4js    •   Jav...
Example: user submits a bidXMPP Comp.                                                          User M                   Us...
Example: user submits a bidRails:# class RoomBid < ActiveRecord:Basedef submit  # ... authorization check and application ...
Example: user submits a bidXMPP Component:# handling of the queue event: “bid.submitted”send_msg do |xml|  # send broadcas...
Example: user submits a bidClient (with strophe.js):// broadcast messageson_broadcast_message: function (message) {     va...
Conclusions•   Pros:    •   powerful toolkits for application    •   server push for free    •   scalability    •   transp...
Demo       Münchner Ruby on Rails User Group, 2010/12/2
Upcoming SlideShare
Loading in...5
×

Real-time Web with Rails and XMPP

7,683

Published on

Published in: Technology
0 Comments
12 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
7,683
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
86
Comments
0
Likes
12
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Real-time Web with Rails and XMPP

    1. 1. Real-time Web with Railsand XMPPMünchner Ruby on Rails User Group, 2010/12/2Li Cai (ca168168@gmail.com)founder: reliwa.departner & ex-CTO: 16ds.com
    2. 2. Motivation: 16ds.com• B2B portal and business network for the chemical industry• Market indexes for 30+ bulk chemicals based on prices submitted by diverse market players• Real-time multi-party price negotiation Münchner Ruby on Rails User Group, 2010/12/2
    3. 3. Multi-party price negotiation• User self-organized• Access control based on organizational structure of the company• Customizable quotation templates for each product• Configurable admission control and protection of sensitive data (customer identities, price details, etc.)• Different negotiation modes: fixed price, bargaining, bidding, moderated bidding• Real-world near auctions by utilizing “Counting-Down- Hammer” Münchner Ruby on Rails User Group, 2010/12/2
    4. 4. Server push in Rails• Polling: simple (but...)• Comet: setup external server for server push (Jetty, glassfish, cometd, ErlyComet, node.js, etc.)• FlashXML Socket: full-duplex TCP stream socket connection (extra work for firewall traversal)• XMPP/BOSH: bidirectional streams over HTTP connection• WebSockets: full-duplex over single TCP socket is being standardized by the W3C (HTML5) Münchner Ruby on Rails User Group, 2010/12/2
    5. 5. XMPP• eXtensible Messaging and Presence Protocol• Data exchanged over a pair of XML streams • extensibility: very easy to add new features to the protocol• XMPP Toolkits • presence • instant messaging • Multi User Chat (XEP-0045) • Publish/Subscribe (XEP-0060) • and many more... Münchner Ruby on Rails User Group, 2010/12/2
    6. 6. XMPP• BOSH (XEP-0124, XEP-0206) • long-lived bidirectional streams over HTTP connection • hide user credentials by session attachment • many XMPP servers have BOSH connection manager build-in• XMPP Component (XEP-0114) • stand-alone server process written with whatever language you like • easily extend the server with your own application logic Münchner Ruby on Rails User Group, 2010/12/2
    7. 7. Extend XMPP with our newfeatures• Build multi-party price negotiation on top of MUC• Create XMPP account for every user, while hiding the credentials by using session attachment• Create MUC-rooms on demand, and manage the member list by our application• Implement an XMPP component to interact with our application as well as the clients• Extend name space and let our application handle stuffs• Configure default settings of the XMPP server for security Münchner Ruby on Rails User Group, 2010/12/2
    8. 8. Rails and XMPP Application Background Web Client HTTP Server Instances (Rails) Jobs (Rails) BOSH Conn. Message Queue Manager XMPP Server XMPP Server Component Münchner Ruby on Rails User Group, 2010/12/2
    9. 9. XMPP servers• ejabberd • written in Erlang, well-known for its scalability • http://www.process-one.net/en/ejabberd/• Openfire • written in Java, very easy to install and configure • http://www.igniterealtime.org/projects/openfire/• Tigase • written in Java • http://www.tigase.org/• and much more... Münchner Ruby on Rails User Group, 2010/12/2
    10. 10. XMPP libraries - server side• xmpp4r • feature-rich, threaded ruby • http://home.gna.org/xmpp4r/• blather • lightweight, DSL-style, evented ruby • http://sprsquish.github.com/blather/• skates • lightweight, MVC-style, evented ruby • http://skates.rubyforge.org/• strophe-ruby • ruby binding for libstrophe, can be run within a Rails-app • https://github.com/yong/stropheruby Münchner Ruby on Rails User Group, 2010/12/2
    11. 11. XMPP libraries - client side• strophe.js • JavaScript • http://code.stanziq.com/strophe/• xmpp4js • JavaScript • http://xmpp4js.sourceforge.net/• XIFF • Flash/ActionScript • http://www.igniterealtime.org/projects/xiff/• and many more... Münchner Ruby on Rails User Group, 2010/12/2
    12. 12. Example: user submits a bidXMPP Comp. User M User A User B XMPP MUC Web App (Spider) (Moderator) (Participant) (Participant) submit bid room_jid, user_jid, [bid] groupchat:x.spider_bid.submitted [bid w. public info] update bid_list update bid_list update bid_list private? && msg:x.spider_bid.submitted [bid w. all info] update bid_list update bid_list Münchner Ruby on Rails User Group, 2010/12/2
    13. 13. Example: user submits a bidRails:# class RoomBid < ActiveRecord:Basedef submit # ... authorization check and application level processing ... Spider.notify "bid.submitted", { :room_jid => room.jid, :bid => public_info.to_json }).merge(public? ? {} : { :private_recipient_jids => [room.moderator.jid, submitter.jid], :bid_full => all_info.to_json })end# class Spiderdef notify key, options = {} mq.exchange(spider, :type => :topic).publish(Marshal.dump(options), :key => key)end Münchner Ruby on Rails User Group, 2010/12/2
    14. 14. Example: user submits a bidXMPP Component:# handling of the queue event: “bid.submitted”send_msg do |xml| # send broadcast message xml.message(:to => options[:room_jid], :from => SPIDER_JID, :type => :groupchat) do xml.x :xmlns => NS_SPIDER_BID do xml.submitted options[:bid] end end # send private messages options[:private_recipient_jids] && options[:private_recipient_jids].each do |jid| xml.message(:to => jid, :from => SPIDER_JID, :type => :normal) do xml.x :xmlns => NS_SPIDER_BID do xml.submitted options[:bid_full], :in => options[:room_jid] end end endend Münchner Ruby on Rails User Group, 2010/12/2
    15. 15. Example: user submits a bidClient (with strophe.js):// broadcast messageson_broadcast_message: function (message) { var from = $(message).attr(from); var room = Strophe.getNodeFromJid(from); msg = $(message).find("x[xmlns=" + NS.SPIDER_BID + "] > submitted"); if (msg.length > 0) { Client.on_bid_submitted(room, JSON.parse(msg.text(), true); } // other events ...},// update local db, render the guion_bid_submitted: function (room, bid, broadcasted) { Room.set_bid(room, bid, broadcasted); Renderer.render_bid(room, bid);}, Münchner Ruby on Rails User Group, 2010/12/2
    16. 16. Conclusions• Pros: • powerful toolkits for application • server push for free • scalability • transparent for users• Cons: • a bit admin work Münchner Ruby on Rails User Group, 2010/12/2
    17. 17. Demo Münchner Ruby on Rails User Group, 2010/12/2
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×