building the real time web presentation

77
The Real Time Web (Building It)

Upload: jward5519

Post on 01-Nov-2014

2.051 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Building The Real Time Web Presentation

The Real Time Web(Building It)

Page 2: Building The Real Time Web Presentation

Blaine CookTwitter, OAuth, ?

Page 3: Building The Real Time Web Presentation

Real-Time Web?1 Problems &

SolutionsFirst Steps

JabberBasics

BuildingApplications

Next Steps

BestPractices

ScalingTechniques

JabberTools

2 3

4 5 6

7 8 9

Page 4: Building The Real Time Web Presentation

the real time web?

Page 5: Building The Real Time Web Presentation

Social Objects

• The things we exchange

• Media: Writing, photos, audio, video, …

• Metadata: Location, relationship data, personal data

Page 6: Building The Real Time Web Presentation
Page 7: Building The Real Time Web Presentation
Page 8: Building The Real Time Web Presentation

problems and solutions

Page 9: Building The Real Time Web Presentation

What are our goals?

• Real time

• Low cost

• Asynchronous

• Simple

Page 10: Building The Real Time Web Presentation

• Works fantastically for web browsers.

• Hard to scale for frequent updates.

• Hard to scale for frequent polling.

• Asks the wrong question: “what happened in the past?”

HTTP?

Page 11: Building The Real Time Web Presentation

HTTP Ping/Push?

• NAT traversal breaks desktop clients.

• HTTP time-outs

• Inconsistent APIs

• Authentication

Page 12: Building The Real Time Web Presentation

SMTP?

• No verifiable authentication scheme

• No consistent approach to API design

• Servers not tuned for high volume of low-latency messages

Page 13: Building The Real Time Web Presentation

Comet?

• GMail

• Successful for web-based clients

• One connection per user

• Requires polling

• Stretching the HTTP metaphor

Page 14: Building The Real Time Web Presentation

Jabber

• Fulfills all the goals

• Open

• Simple (if youʼre careful)

Page 15: Building The Real Time Web Presentation

first steps

Page 16: Building The Real Time Web Presentation

architecture

• Not p2p

• Client-to-server

• Clients maintain persistent connections

• Federation looks exactly like email

• Servers communicate directly

Page 17: Building The Real Time Web Presentation

• a jabber session is simply two streaming XML documents

• the spec defines common elements

• but you can extend it at any time

• similar to html, but with a larger vocabulary

itʼs all just xml

Page 18: Building The Real Time Web Presentation

jabber addressing

• addresses look like email addresses: [email protected]

• but you can omit the username (node): domain.com

• or you can include a resource: [email protected]/mydevice

Page 19: Building The Real Time Web Presentation

jabber federation• i.e., why itʼs not spammy

• s2s - server to server

• support for verified authentication of servers using SSL

• dialback authentication

• explicit whitelist by default

Page 20: Building The Real Time Web Presentation

messages• primary jabber payload. email.

• the simplest message is an addressed stanza with a body (the message)

• subject, alternate message types are available but client ui is poorly implemented

• we can send html and atom, too

Page 21: Building The Real Time Web Presentation

presence

• online, offline, chat, away, xa

• the intellectual pivot between optimizing for store and forward (http, smtp) and streams of data

Page 22: Building The Real Time Web Presentation

tracking presence

• we can subscribe and unsubscribe

• also allow, deny, or block requests

Page 23: Building The Real Time Web Presentation

the roster

• your social connections

• maintains presence subscriptions

• maintains your whitelist

• synonomous with your buddy list on MSN / AIM / YahooIM

Page 24: Building The Real Time Web Presentation

jabber basics

Page 25: Building The Real Time Web Presentation

• Nearly 200 specs defining all sorts of behaviour. Ignore them.

• Unless you need them.

• Core & IM: RFCs 3920 & 3921

navigating jabber

Page 26: Building The Real Time Web Presentation

stanzas

• XML Elements

• Shared attributes:

• to, from, id, type

Page 27: Building The Real Time Web Presentation

messages

<message from="[email protected]" to="[email protected]">

<body>Hi!</body></message>

Page 28: Building The Real Time Web Presentation

messages

<message from="[email protected]/orchard" to="[email protected]" id="msg:montague.net,1"> <body>Hi!</body></message>

Page 30: Building The Real Time Web Presentation

presence

<presence from="[email protected]" to="[email protected]"> <show>away</show> <status>swooning</status></presence>

Page 34: Building The Real Time Web Presentation

iq

• Information Query

• Enables the roster, discovery, XEPs

• Should almost always be hidden behind libraries.

Page 35: Building The Real Time Web Presentation

building applications

Page 36: Building The Real Time Web Presentation

• we can send/receive messages

• add / remove contacts

• track presence

• let's build something!

taking stock

Page 37: Building The Real Time Web Presentation

define bot behaviour

• what does your bot do?

• conversational

• informational

• recorder

Page 38: Building The Real Time Web Presentation

define api behaviour

• what does your api look like?

• atom?

• custom xml with namespaces?

• we'll dig in a bit more later.

Page 39: Building The Real Time Web Presentation

write the behaviour

• build a class or interface that handles messages

• test the class with mock xmpp stanzas

• mock out sending functions in your xmpp lib so you don't need an active connection

Page 40: Building The Real Time Web Presentation

behaviourclass MyHandler def on_message(message) puts "Got Message:" puts "from #{message.from}" puts "to #{message.to}" puts "body #{message.body}" out = Jabber::Message.new(message.from, "got it!") yield out endend

Page 41: Building The Real Time Web Presentation

event handlerclient = Jabber::Simple.new('[email protected]', 'pwd')handler = MyHandler.new

client.received_messages do |message| handler.on_message(message) do |out| client.send(out) end end

Page 42: Building The Real Time Web Presentation

event loopclient = Jabber::Simple.new('[email protected]', 'pwd')handler = MyHandler.newloop do client.received_messages do |message| handler.on_message(message) do |out| client.send(out) end endend

Page 43: Building The Real Time Web Presentation

handling presence

client.status(:away, "eating")

client.presence_updates do |update| friend = update[0] presence = update[2] puts "#{friend.jid} is #{presence.status}"end

Page 44: Building The Real Time Web Presentation

handling presence

<presence from="[email protected]"> <show>away</show> <status>eating</status></presence>

Page 45: Building The Real Time Web Presentation

rosters

• should ideally be handled by libraries

• if not, at least aim for being able to fetch your roster using a library call

Page 46: Building The Real Time Web Presentation

rosters

Roster roster = connection.getRoster();Collection<RosterEntry> entries = roster.getEntries();for (RosterEntry entry : entries) { System.out.println(entry);}

Page 47: Building The Real Time Web Presentation

process management

• Very difficult to run Jabber clients from non-persistent connections

• Run a persistent daemon that manages your Jabber connection

Page 48: Building The Real Time Web Presentation

next steps

Page 49: Building The Real Time Web Presentation

• A mechanism for Publishing and Subscribing to feeds

• Like presence subscriptions, but for data

PubSub

Page 50: Building The Real Time Web Presentation

PubSub

• Over-specified

• Don't try to read the spec if you can avoid it

• Thankfully, the concept is simple and the core implementation is easy

Page 51: Building The Real Time Web Presentation

PubSub Subscribe<iq type='set' from='[email protected]/barracks' to='example.com' id='sub1'> <pubsub xmlns='http://jabber.org/protocol/pubsub'> <subscribe node='http://example.com/updates' jid='[email protected]/barracks'/> </pubsub></iq>

Page 52: Building The Real Time Web Presentation

PubSub Confirmation<iq type='result' from='example.com' to='[email protected]/barracks' id='sub1'> <pubsub xmlns='http://jabber.org/protocol/pubsub'> <subscription node='http://example.com/updates' jid='[email protected]/barracks' subscription='subscribed'/> </pubsub></iq>

Page 53: Building The Real Time Web Presentation

PubSub Messages<message from='example.com'

to='[email protected]/barracks' id='foo'>

<body>blah</body>

<event xmlns='http://jabber.org/protocol/pubsub#event'>

<items node='http://twitter.com/xmpp'>

<item id='http://twitter.com/blaine/statuses/324236243'>

<entry>...</entry>

</item>

</items>

</event>

</message>

Page 54: Building The Real Time Web Presentation

PubSub Unsubscribe<iq type='set' from='[email protected]/barracks' to='example.com' id='unsub1'> <pubsub xmlns='http://jabber.org/protocol/pubsub'> <unsubscribe node='http://example.com/xmpp' jid='[email protected]'/> </pubsub></iq>

Page 55: Building The Real Time Web Presentation

PubSub Confirmation

<iq type='result' from='example.com' to='[email protected]/barracks' id='unsub1'/>

Page 56: Building The Real Time Web Presentation

PEP

• Personal Eventing via PubSub

• You can think of it as exactly the same as regular PubSub, except the node becomes relative to a user (full JID)

Page 58: Building The Real Time Web Presentation

Federation

• Social Network Federation

• Use PubSub to allow users on remote services to subscribe to eachother

• Breaking down walled gardens

Page 59: Building The Real Time Web Presentation

best practices

Page 60: Building The Real Time Web Presentation

• keeping the api simple

Page 61: Building The Real Time Web Presentation

• choosing where to use jabber

Page 62: Building The Real Time Web Presentation

• atom over xmpp

Page 63: Building The Real Time Web Presentation

scaling techniques

Page 64: Building The Real Time Web Presentation

• Jabber scales well out of the box for relatively small numbers of contacts.

• Stops working at around 35k contacts, due to roster presence behaviour.

• Come online, find out what everyone's presence is.

scalability?

Page 65: Building The Real Time Web Presentation

components

• In order to work around this, we use the component protocol, XEP-0114

• Horrendously bad documentation

• But thankfully it's simple

Page 66: Building The Real Time Web Presentation

components

• A component allows you to handle everything for a JID, or a whole domain

• You can turn off the roster!

• Without roster management, we now assume that out bot is always online.

Page 67: Building The Real Time Web Presentation

components

• Components work just like client-to-server bots, but we need to handle presence ourselves.

• The easiest way is to do the following…

Page 68: Building The Real Time Web Presentation

componentsclient = Jabber::Component.new('example.com')client.connect("127.0.0.1")client.auth("secret")client.add_presence_callback do |presence| case presence.type.to_s when nil, 'unavailable': save_presence(presence) when 'probe': send_online(presence.from) when 'subscribe': send_subscribed(presence.from) endend

Page 69: Building The Real Time Web Presentation

horizontal scaling

• Many processes across machines

• Need a queuing mechanism

• We use Starling

• ActiveMQ, RabbitMQ, MySQL, local HTTP push are also viable options

Page 70: Building The Real Time Web Presentation

horizontal scalingclient.add_message_callback do |message| incoming_message_queue.push messageend

loop do message = message_queue.pop client.send messageend

Page 71: Building The Real Time Web Presentation

client connections

• If you plan to offer Jabber user accounts, you'll need to scale to many persistent connections.

• Thankfully, most Jabber servers do this part out of the box.

Page 72: Building The Real Time Web Presentation

tools

Page 73: Building The Real Time Web Presentation

• Ruby: xmpp4r & xmpp4r-simple

• Java: Smack

• Python: twisted-words

• Perl: Net::Jabber

• Javascript: JSJaC

Client Libraries

Page 74: Building The Real Time Web Presentation

• ejabberd (recently 2.0)

• openfire

• Jabber XCP

Jabber Servers

Page 75: Building The Real Time Web Presentation

• Debugging: Psi (cross-platform, fully featured)

• PubSub: Idavoll

Other Tools

Page 76: Building The Real Time Web Presentation

• livejournal

• twitter

• jaiku

• gtalk / gmail

• chesspark

• fire eagle (soon!)

Jabber-enabled

Page 77: Building The Real Time Web Presentation

questions?