the real-time web (and other buzzwords)

157
Chris Wanstrath http://defunkt.github.com hi everyone, i’m chris wanstrath

Upload: err

Post on 10-Dec-2014

20.454 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: The Real-Time Web (and Other Buzzwords)

Chris Wanstrath

http://defunkt.github.com

hi everyone, i’m chris wanstrath

Page 2: The Real-Time Web (and Other Buzzwords)

i love randy rhoads

Page 3: The Real-Time Web (and Other Buzzwords)

and kurt vonnegut

Page 4: The Real-Time Web (and Other Buzzwords)

i live in san francisco

Page 5: The Real-Time Web (and Other Buzzwords)

and work at github

Page 6: The Real-Time Web (and Other Buzzwords)

(which is written in rails)

Page 7: The Real-Time Web (and Other Buzzwords)

but! i’m not gonna talk about any of that stuff

Page 8: The Real-Time Web (and Other Buzzwords)

today i want to talk about python

Page 9: The Real-Time Web (and Other Buzzwords)

and comet

(among other things)

Page 10: The Real-Time Web (and Other Buzzwords)

the real-time web (And other buzzwords)

By Chris Wanstraththis talk is titled “the real-time web (and other buzzwords)”

Page 11: The Real-Time Web (and Other Buzzwords)

?so what is the “real-time” web?

Page 12: The Real-Time Web (and Other Buzzwords)

techcrunch and readwriteweb would have you believe it’s a way to get your RSS stories

Page 13: The Real-Time Web (and Other Buzzwords)

faster

Page 14: The Real-Time Web (and Other Buzzwords)
Page 15: The Real-Time Web (and Other Buzzwords)

but is that it?

Page 16: The Real-Time Web (and Other Buzzwords)

no.

Page 17: The Real-Time Web (and Other Buzzwords)

it’s all about pushing

Page 18: The Real-Time Web (and Other Buzzwords)

instead of polling

Page 19: The Real-Time Web (and Other Buzzwords)

!it’s getting told what’s new

Page 20: The Real-Time Web (and Other Buzzwords)

?instead of asking for what’s new

Page 21: The Real-Time Web (and Other Buzzwords)

instant chat in the browser

Page 22: The Real-Time Web (and Other Buzzwords)

and information the moment it’s available

Page 23: The Real-Time Web (and Other Buzzwords)

Client Server

one persistent connection

Page 24: The Real-Time Web (and Other Buzzwords)

ClientServer

instead of many, short lived connections

Page 25: The Real-Time Web (and Other Buzzwords)

right now when we say “real-time web” we usually mean one of three things

Page 26: The Real-Time Web (and Other Buzzwords)

comet

Page 27: The Real-Time Web (and Other Buzzwords)

flash’s XMLSocket

Page 28: The Real-Time Web (and Other Buzzwords)

or HTML5’s WebSocket

Page 29: The Real-Time Web (and Other Buzzwords)

let’s start with comet

Page 30: The Real-Time Web (and Other Buzzwords)

?how many people know what comet is?

how many people know how it differs from XML socket or WebSocket?

good! for a long time i had no idea what comet was

Page 31: The Real-Time Web (and Other Buzzwords)

!i think it has a big marketing problem.

Page 32: The Real-Time Web (and Other Buzzwords)

see, comet is a cleaning product.

Page 33: The Real-Time Web (and Other Buzzwords)

just like ajax

Page 34: The Real-Time Web (and Other Buzzwords)

so it’s kind of like,

Page 35: The Real-Time Web (and Other Buzzwords)

Me, too!

“me too”

Page 36: The Real-Time Web (and Other Buzzwords)

I, too, am a revolutionary web

technique you didn’t know existed and can start using

today that will forever change the way you imagine the

web experience.

“I, too, am a revolutionary web technique you didn’t know existed and can start using today that will forever change the way you think about the web experience.”

Page 37: The Real-Time Web (and Other Buzzwords)

...

and you’re like

Page 38: The Real-Time Web (and Other Buzzwords)

Uhh...

uh...

Page 39: The Real-Time Web (and Other Buzzwords)

AJAX!!!

Page 40: The Real-Time Web (and Other Buzzwords)

if i had my way...

Page 41: The Real-Time Web (and Other Buzzwords)

i’d call it something else

Page 42: The Real-Time Web (and Other Buzzwords)

like maybe asteroid

Page 43: The Real-Time Web (and Other Buzzwords)

i dunno, there’s some cool imagery there

Page 44: The Real-Time Web (and Other Buzzwords)

i dunno, there’s some cool imagery there

Page 45: The Real-Time Web (and Other Buzzwords)

i dunno, there’s some cool imagery there

Page 46: The Real-Time Web (and Other Buzzwords)

i dunno, there’s some cool imagery there

Page 47: The Real-Time Web (and Other Buzzwords)

i dunno, there’s some cool imagery there

Page 48: The Real-Time Web (and Other Buzzwords)

i dunno, there’s some cool imagery there

Page 49: The Real-Time Web (and Other Buzzwords)

i dunno, there’s some cool imagery there

Page 50: The Real-Time Web (and Other Buzzwords)

anything but comet

Page 51: The Real-Time Web (and Other Buzzwords)

anyway, comet is any standards compliant technique which pushes or streams data to the browser over HTTP

with comet you can essentially fake a socket connection between a browser and a backend server

Page 52: The Real-Time Web (and Other Buzzwords)

how about an example

well, for this year’s django dash

Page 53: The Real-Time Web (and Other Buzzwords)

me

Page 54: The Real-Time Web (and Other Buzzwords)

alex gaynor... whoops...

Page 55: The Real-Time Web (and Other Buzzwords)

alex gaynor

Page 56: The Real-Time Web (and Other Buzzwords)

and leah culver

Page 57: The Real-Time Web (and Other Buzzwords)

( World’s smallest park )

(seen here next to the world’s smallest park)

Page 58: The Real-Time Web (and Other Buzzwords)

built leafychat

irc in your browser using comet

Page 59: The Real-Time Web (and Other Buzzwords)

you can connect to freenode channels

Page 60: The Real-Time Web (and Other Buzzwords)

see who’s online

Page 61: The Real-Time Web (and Other Buzzwords)

and do the irc thing

right in your browser (demo?)

Page 62: The Real-Time Web (and Other Buzzwords)

all without ajax

Page 63: The Real-Time Web (and Other Buzzwords)

?how?

Page 64: The Real-Time Web (and Other Buzzwords)

well, there are a few components at play here

Page 65: The Real-Time Web (and Other Buzzwords)

Browser

first you’ve got the browser (naturally)

Page 66: The Real-Time Web (and Other Buzzwords)

Browser Apache80

Apache80

then we have apache

Page 67: The Real-Time Web (and Other Buzzwords)

Browser Apache80

Django8000

Apache80

Django8000

sitting behind apache is django (via mod_wsgi or whatever)

Page 68: The Real-Time Web (and Other Buzzwords)

Browser Apache80

Django8000

Apache80

Django8000

the browser hits leafychat.com, port 80, which hits apache. apache sees it’s a django request

Page 69: The Real-Time Web (and Other Buzzwords)

Browser Apache80

Django8000

Apache80

Django8000

and hands off the request.

Page 70: The Real-Time Web (and Other Buzzwords)

Browser Apache80

Django8000

Apache80

Django8000

once django generates a response, apache takes it and delivers it

Page 71: The Real-Time Web (and Other Buzzwords)

Browser Apache80

Django8000

Apache80

Django8000

back to the browser. that’s, more or less, our HTTP request cycle

Page 72: The Real-Time Web (and Other Buzzwords)

Browser

comet connections work in a similar fashion

Page 73: The Real-Time Web (and Other Buzzwords)

Browser Orbited8100

Orbited8100

instead of apache, we have Orbited sitting on port 8100

Page 74: The Real-Time Web (and Other Buzzwords)

orbited is an open source, python comet server powered...

Page 75: The Real-Time Web (and Other Buzzwords)

by twisted.

it does all of the comet heavy-lifting for us

Page 76: The Real-Time Web (and Other Buzzwords)

Browser Orbited8100

Orbited8100

so.

Page 77: The Real-Time Web (and Other Buzzwords)

Browser Orbited8100

Orbited8100

Zeddicus8200

sitting behind orbited we have our app-specific comet code. for leafy chat it was a twisted-based daemon named zeddicus. it handled all the IRC stuff - connecting to channels, sending messages, receiving private messages, logging, all that

Page 78: The Real-Time Web (and Other Buzzwords)

Orbited8100

Orbited8100

orbited handles generic browser stuff...

Page 79: The Real-Time Web (and Other Buzzwords)

Zeddicus8200

while our app-specific daemon (zeddicus) deals with the business logic.

in this case, irc stuff

Page 80: The Real-Time Web (and Other Buzzwords)

Browser Orbited8100

Orbited8100

Zeddicus8200

does this look familiar?

Page 81: The Real-Time Web (and Other Buzzwords)

Browser Apache80

Django8000

Apache80

Django8000

no? ok.

Page 82: The Real-Time Web (and Other Buzzwords)

Browser Orbited8100

Orbited8100

Zeddius8200

Zeddicus8200

anyway, the browser (using orbited’s bundled js library) makes a request...

Page 83: The Real-Time Web (and Other Buzzwords)

Browser Orbited8100

Zeddius8200

Orbited8100

Zeddius8200

Zeddicus8200

to port 8100. orbited sees it’s a request for zeddicus and hands it off to our backend

Page 84: The Real-Time Web (and Other Buzzwords)

Browser Orbited8100

Zeddius8200

Orbited8100

Zeddius8200

Zeddicus8200

zeddicus sees a new socket connection open, does some stuff, then writes to the socket

Page 85: The Real-Time Web (and Other Buzzwords)

Browser Orbited8100

Zeddius8200

Orbited8100

Zeddius8200

Zeddicus8200

orbited reads what zeddicus wrote to the socket connection they share

Page 86: The Real-Time Web (and Other Buzzwords)

Browser Orbited8100

Zeddius8200

Orbited8100

Zeddius8200

Zeddicus8200

then sends it back the browser via comet

Page 87: The Real-Time Web (and Other Buzzwords)

Browser Apache80

Django8000

Apache80

Django8000

just like before

Page 88: The Real-Time Web (and Other Buzzwords)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head> <script> // session data var session_nick = "" var session_channels = [] </script> <script type="text/javascript" src="/static/js/soundmanager2/soundmanager2-nodebug-jsmin.js"></script> <script type="text/javascript" src="/static/js/audio-player.js"></script>

<title>Leafy Chat</title>

<link rel="icon" href="/static/img/favicon.ico"/>

<link rel="stylesheet" href="/static/css/reset.css" type="text/css" media="screen" /> <link rel="stylesheet" href="/static/css/facebox.css" type="text/css" media="screen/> <link rel="stylesheet" href="/static/css/base.css" type="text/css" media="screen" />

<script> document.domain = document.domain; </script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script type="text/javascript" src="http://leafychat.com:8100/static/Orbited.js"></script> <script type="text/javascript" src="/static/js/facebox.js" charset="utf-8"></script> <script type="text/javascript" src="/static/js/cookie.js" charset="utf-8"></script> <script type="text/javascript" src="/static/js/leafy.js" charset="utf-8"></script> <script type="text/javascript" src="/static/js/kahlan.js" charset="utf-8"></script>

<div id="main"> <div id="header"> <div id="navigation" class="rounded"> <ul class="nav-list"> <!-- this should be a list --> <li>Hi <span id="welcome-user">there</span>!</li>

but while apache and django deal with html

Page 89: The Real-Time Web (and Other Buzzwords)

Browser Orbited8100

Zeddius8200

Orbited8100

Zeddius8200

Zeddicus8200

zeddicus, our back-end daemon, concerns itself only...

Page 90: The Real-Time Web (and Other Buzzwords)

with json.

orbited supports STOMP, XMPP, and raw IRC, too, but JSON is the way to go.

why do i say that?

Page 91: The Real-Time Web (and Other Buzzwords)

well, unfortunately this is where orbited does not rock

Page 92: The Real-Time Web (and Other Buzzwords)

orbited only cares about giving you a socket, you have to do everything else yourself.

that’s the deal

Page 93: The Real-Time Web (and Other Buzzwords)

so you need to design your own protocol...

Page 94: The Real-Time Web (and Other Buzzwords)

how it interacts with your back-end daemon

Page 95: The Real-Time Web (and Other Buzzwords)

how events are fired and responded to

Page 96: The Real-Time Web (and Other Buzzwords)

all that boring stuff

Page 97: The Real-Time Web (and Other Buzzwords)

we ended up using a combination of django signals and json

Page 98: The Real-Time Web (and Other Buzzwords)

Browser Orbited8100

Zeddius8200

Orbited8100

Zeddius8200

Zeddicus8200

so this is our overview, except with one small change

Page 99: The Real-Time Web (and Other Buzzwords)

Browser Orbited8100

Zeddius8200

Zeddicus8200

these are actually persistent connections

Page 100: The Real-Time Web (and Other Buzzwords)

Browser Orbited8100

Zeddius8200

Zeddicus8200

between orbited and zeddicus it’s a tcpsocket

Page 101: The Real-Time Web (and Other Buzzwords)

Browser Orbited8100

Zeddius8200

Zeddicus8200

between the browser and orbited it’s a comet technique

Page 102: The Real-Time Web (and Other Buzzwords)

Browser Zeddicus8200

we jump through all these hoops because it allows us to write our app as if the browser is connecting DIRECTLY to zeddicus via a tcpsocket

Page 103: The Real-Time Web (and Other Buzzwords)

Browser Zeddicus8200

which is what the “real time” web is all about and what comet gives us

the browser writes to and reads from a socket, our back-end daemon does the same.

Page 104: The Real-Time Web (and Other Buzzwords)

fast communication

Page 105: The Real-Time Web (and Other Buzzwords)

Browser Orbited8100

Zeddius8200

Zeddicus8200

alright, so let’s talk about this part a bit more

the comet part

Page 106: The Real-Time Web (and Other Buzzwords)

Browser Orbited8100

Zeddius8200

Zeddicus8200

there are a few different ways to fake a persistent connection to a server with modern browsers

Page 107: The Real-Time Web (and Other Buzzwords)

xhr long polling

- hang an xhr request until we get a response or 30s, then re-open and wait again

Page 108: The Real-Time Web (and Other Buzzwords)

<script>script tag long polling

- dynamically create a script tag pointing to a url that hangs or times out, rinse and repeat

Page 109: The Real-Time Web (and Other Buzzwords)

<iframe>forever frame

- open a Content-Encoded: chunked url in an iframe, each chunk is a <script> tag that runs

Page 110: The Real-Time Web (and Other Buzzwords)

xhr streaming

- set content-encoding: chunked and trigger onreadystate callback with each chunk

Page 111: The Real-Time Web (and Other Buzzwords)

so now you know our white lie: we don’t have real persistence, or a real socket.

we fake it at both ends.

Page 112: The Real-Time Web (and Other Buzzwords)

or, really, orbited fakes it at both ends

Page 113: The Real-Time Web (and Other Buzzwords)

all that to get irc in your browser

Page 114: The Real-Time Web (and Other Buzzwords)

well, kinda

Page 115: The Real-Time Web (and Other Buzzwords)

Browser Orbited8100

Zeddius8200

Zeddicus8200

freenode6667

see, with orbited, you can connect directly to an irc server

and why not? it’s just a socket connection

Page 116: The Real-Time Web (and Other Buzzwords)

in fact you can demo a (somewhat functional) irc connection on the orbited website

Page 117: The Real-Time Web (and Other Buzzwords)

Browser Orbited8100

Zeddius8200

Zeddicus8200

Zeddius8200

Zeddicus8200

freenode6667

but with leafychat we wrote our own backend daemon that connected to irc.

why?

Page 118: The Real-Time Web (and Other Buzzwords)

logging.

Page 119: The Real-Time Web (and Other Buzzwords)

Browser Orbited8100

Orbited8100

Zeddius8200

Zeddicus8200

if we loaded our django code into zeddicus, we could easily log irc chats you’re interested in based on your session id.

it works very well.

Page 120: The Real-Time Web (and Other Buzzwords)

Browser

Zeddius8200

Browser

Django8000

Apache80

Orbited

at this point i should mention the older tutorials online explaining how to load orbited into django.

Page 121: The Real-Time Web (and Other Buzzwords)

Browser

Apache80

Django8000

Orbited8100

Zeddicus8200

but i the best (and simplest) way is to let each component be its own daemon

it works great for production as well as dev mode - the django dash judges were able to start and run our app locally, despite the number of daemons that needed to run

Page 122: The Real-Time Web (and Other Buzzwords)

if you want to get a comet app up and running locally, check out orbited

it supports a ton of comet transports and is actively maintained

Page 123: The Real-Time Web (and Other Buzzwords)

using java? jetty has comet support

Page 124: The Real-Time Web (and Other Buzzwords)

the ruby world has juggernaut

Page 125: The Real-Time Web (and Other Buzzwords)

while perl has meteor

Page 126: The Real-Time Web (and Other Buzzwords)

there’s the interesting in-progress Ajax Push Engine

Page 127: The Real-Time Web (and Other Buzzwords)

and in erlang there’s erlycomet (built on mochiweb)

Page 128: The Real-Time Web (and Other Buzzwords)

so, flash’s XMLSocket

Page 129: The Real-Time Web (and Other Buzzwords)

Browser Orbited8100

Zeddius8200

Zeddicus8200

it turns this

Page 130: The Real-Time Web (and Other Buzzwords)

Browser Zeddicus8200

into this

Page 131: The Real-Time Web (and Other Buzzwords)

how? flash allows you to make tcp connections in actionscript.

by providing a javascript api to those tcp connections, we can use flash to create persistent, socket connections from the browser

Page 132: The Real-Time Web (and Other Buzzwords)

no lying needed

Page 133: The Real-Time Web (and Other Buzzwords)

of course, it may be non-standard and might not work great across firewalls. but if you can use it, it’s pretty great

Page 134: The Real-Time Web (and Other Buzzwords)

a popular technique is to attempt to open a flash socket, then fall back to standards based comet methods if it fails

it’s good backup

Page 135: The Real-Time Web (and Other Buzzwords)

there are a few nice libraries for xml socket on github

Page 136: The Real-Time Web (and Other Buzzwords)

tmm1 / jssocket

my favorite is tmm1’s jssocket

Page 137: The Real-Time Web (and Other Buzzwords)

defunkt / jssocket

(i have a fork which removes the jquery dependencies)

Page 138: The Real-Time Web (and Other Buzzwords)

finally: HTML5’s WebSocket

Page 139: The Real-Time Web (and Other Buzzwords)

it’s still a proposed draft

but it’ll let you open a socket to any serve that speaks the special WebSocket protocol

Page 140: The Real-Time Web (and Other Buzzwords)

ws://servers need to speak the WebSocket protocol - you can’t open arbitrary connections to irc or xmpp gateways

this plugs up the obvious security holes but also makes it a bit harder to implement than something like XMLSocket

your server needs to speak ws

Page 141: The Real-Time Web (and Other Buzzwords)

?so, in review

Page 142: The Real-Time Web (and Other Buzzwords)

comet

Page 143: The Real-Time Web (and Other Buzzwords)

should be called asteroid

Page 144: The Real-Time Web (and Other Buzzwords)

flash’s XMLSocket

Page 145: The Real-Time Web (and Other Buzzwords)

is non-standard but nice

Page 146: The Real-Time Web (and Other Buzzwords)

HTML5’s WebSocket

Page 147: The Real-Time Web (and Other Buzzwords)

not yet here

but futuristic

Page 148: The Real-Time Web (and Other Buzzwords)

you should give orbited a shot

Page 149: The Real-Time Web (and Other Buzzwords)

with json

Page 150: The Real-Time Web (and Other Buzzwords)

\0probably null terminated

Page 151: The Real-Time Web (and Other Buzzwords)

Django8000

Orbited

don’t load orbited in django

Page 152: The Real-Time Web (and Other Buzzwords)

Orbited8100

use them alongside each other

Page 153: The Real-Time Web (and Other Buzzwords)

?is that it?

Page 154: The Real-Time Web (and Other Buzzwords)

!that’s it

Page 155: The Real-Time Web (and Other Buzzwords)

Thank You

Page 156: The Real-Time Web (and Other Buzzwords)
Page 157: The Real-Time Web (and Other Buzzwords)

http://www.flickr.com/photos/mojombo/3785549701/sizes/l/http://www.flickr.com/photos/voteprime/2361330726/sizes/o/http://www.flickr.com/photos/johnkerr/2371310025/sizes/l/http://www.flickr.com/photos/h19/182898904/http://www.flickr.com/photos/ukinindia/3595042998/sizes/l/http://www.flickr.com/photos/scott1027/3189137578/sizes/l/http://www.flickr.com/photos/foxypar4/2124673642/sizes/l/http://www.flickr.com/photos/nickwheeleroz/2166114756/sizes/l/http://www.flickr.com/photos/nickwheeleroz/2178146080/sizes/l/http://www.flickr.com/photos/diyromarcade/3006368260/sizes/o/http://www.flickr.com/photos/boopsiedaisy/3611187885/sizes/o/http://www.flickr.com/photos/bastispicks/2834869959/sizes/l/http://www.flickr.com/photos/courtenay/2536259393/sizes/l/http://www.flickr.com/photos/jardinle/3335907363/sizes/o/http://www.flickr.com/photos/tim-miley/3569809538/http://www.flickr.com/photos/dexterousartisan/3209508363/sizes/l/http://www.flickr.com/photos/equanimity/3763158824/sizes/l/http://www.flickr.com/photos/24617281@N04/2329660856/sizes/o/http://farm4.static.flickr.com/3555/3767120120_8f43f885e1.jpghttp://www.flickr.com/photos/raffaella/64701476/http://www.flickr.com/photos/monicareyes/405402858/sizes/o/http://www.flickr.com/photos/11016633@N07/2232831953/

flickr