Generative Artificial Intelligence: How generative AI works.pdf
Real-time Web with Rails and XMPP
1. Real-time Web with Rails
and XMPP
Münchner Ruby on Rails User Group, 2010/12/2
Li Cai (ca168168@gmail.com)
founder: reliwa.de
partner & ex-CTO: 16ds.com
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. 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. 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. 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. 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. Extend XMPP with our new
features
• 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. 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. 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. 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. 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. Example: user submits a bid
XMPP 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. Example: user submits a bid
Rails:
# class RoomBid < ActiveRecord:Base
def 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 Spider
def 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. Example: user submits a bid
XMPP 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
end
end
Münchner Ruby on Rails User Group, 2010/12/2
15. Example: user submits a bid
Client (with strophe.js):
// broadcast messages
on_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 gui
on_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. 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. Demo
Münchner Ruby on Rails User Group, 2010/12/2