54
Comet The Rise of Highly Interactive Websites Joe Walker DWR Lead Developer Director, Support and Development, SitePen UK

Comet and the Rise of Highly Interactive Websites

Embed Size (px)

DESCRIPTION

Comet talk from the Ajax Experience 2008

Citation preview

Page 1: Comet and the Rise of Highly Interactive Websites

CometThe Rise of Highly Interactive Websites

Joe WalkerDWR Lead DeveloperDirector, Support and Development, SitePen UK

Page 2: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

IntroductionPerspectives

TechnicalArchitectures

Page 3: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Pushing data to the browser without needing the browser to ask

Page 4: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Long lived HTTP (i.e. not polling)A pattern not a protocol

Reverse Ajax == Comet + Polling

Page 5: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Page 6: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Page 7: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Why now?

Ajax made individual pages interactive

The web is getting more social == more transient

Page 8: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Time people spend on a

page

Time before a

page changes

Evolution of the Web

Page 9: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

IntroductionPerspectives

TechnicalArchitectures

Page 10: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Comet is for ...keeping me up to date

... with ...everyone else, andeverything else

Page 11: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

What are the other guys are doing?• Editing things that interest you• Talking to / about you• Saying and doing interesting things• Playing games

Everyone else

Page 12: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

What is System X doing?• Data streaming• Async processing• Transaction fulfillment

Everything else

Page 13: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Chat is everywhere: GMail, Meebo, Facebook ...Collaborative Editing: GDocs, ThinkatureStreaming Financial Data: Lightstreamer, CaplinAsynch Updates: GMail, Yes.comOnline Gaming: GPokr, Chess.comAsync Server Processing: Polar Rose

Page 14: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

The web was designed connectionlessComet gives you the connection back

Page 15: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

The web was designedclient-server

Comet starts to make it lookfar more peer-to-peer

Page 16: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Performance

Page 17: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

IntroductionPerspectives

TechnicalArchitectures

Page 18: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

BUT

Page 19: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Connection Options

Long polling: XHRForever Frame: iframeScript tagsWebSocketsActiveXObject(“htmlfile”)Mime MessagingFlash Remoting

Page 20: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Long Polling

‘Slow’ XHR requestBut:

• Restart required• XHR.responseText on IE

Page 21: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Client How-to: Forever Frame

Open a slow iframe, poll for contentIE lies about the content

• Send text/plain + 4k whitespace to flush IE• Flush with <script> tag for each data block

iframe needs restarting to avoid memory leakBut IE clicks when iframe starts

Page 22: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Client How-to: htmlfile

‘htmlfile’ is an ActiveX control like XHR:

hf = new ActiveXObject("htmlfile");hf.open();hf.write("<html><iframe/></html>");hf.close();hf.parentWindow.dwr = dwr;

Avoids ‘clicking’, but not IE/Server 2003Not supported in Firefox, Safari, Opera, etc.

Page 23: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Client How-to: Callback Polling

Dynamic <script> blocksCan point to any domainDoes not stream

Page 24: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Client How-to: Web Standards

HTML 5 makes it better:• WebSockets• DOM Storage provides way to synchronize across tabs

and frames

Page 25: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Other Options

Mime Messaging• Multipart Mime: x-multipart-replace• Not IE• Excellent performance

Flash Remoting

Page 26: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

2 Connection Issue

Coordination using:• window.name• cookies

Multi-home DNSAll modern browsers raise number of connections

Page 27: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Other Issues

HTTP streaming is download only (except WebSockets)Detection of failed connections

Page 28: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Server Tricks

Stream-stoppers:• Apache: mod_jk• Buggy network proxies• Application firewalls

Thread starvation (Servlet 3.0)Multi-threading

Page 29: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Does that stop us?

So it’s a hack ...• But that hasn’t stopped Ajax•And Comet does work

Page 30: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Library Solutions

Open Solutions:• DWR http://directwebremoting.org• Jetty http://jetty.mortbay.org (Cometd)• Orbited http://orbited.org• Ice Faces http://icefaces.org• Atmosphere http://atmosphere.dev.java.net

Page 31: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Comet vs. Polling

For Polling:• More efficient for very low latencies• Simpler to implement

For Comet:• More efficient in general case• More adaptable

Page 32: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

IntroductionPerspectives

TechnicalArchitectures

Page 33: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Inboard vs Outboard

Page 34: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Inboard Comet

Comet is part of the mainweb server infrastructure

Page 35: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

• Simpler for new development• Harder scaling• Comet is just part of the infrastructure

Inboard (e.g. DWR)

Page 36: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Outboard (e.g. Cometd)

Comet is deployed to aseparate set of servers

Page 37: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

• Add-on that doesn’t affect the server• Easier to add to existing apps• Harder to get started

Outboard (e.g. Cometd)

Page 38: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

API Styles:

Page 39: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

API Styles:Level 0: P2P Data

Page 40: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Level 0: P2P Data with WebSocket

var s = new WebSocket("ws://eg.com/demo");

s.onOpen = function(e) { alert("Open"); }s.onRead = function(e) { alert(e.data); }s.onClose = function(e) { alert("Close"); }

s.send("Hello World");

Page 41: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

API Styles:Level 1: Pub / Sub

Page 42: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Level 1: Pub/Sub with Bayeux / Jetty

// Bayeux Example// Get a channeldojox.cometd.Channel c = bayeux.getChannel(chanName, true);

// Subscribe to things publishedc.subscribe(client);

// Publish something ourselvesc.publish(fromClient, data, msgId);

Page 43: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Level 1: Pub/Sub with Atmosphere

@Grizzlet(Grizzlet.Scope.APPLICATION)@Path("myGrizzlet")public class MyGrizzlet { @Broadcaster(Grizzlet.Scope.VM) private Broadcaster bc; @Suspend(6000) @GET @Push public String onGet() { bc.brodcast("New user"); return "Suspending the connection"; }}

Page 44: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Pub / Sub Advantages

• Low coupling• Inter-language interoperability

Page 45: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

API Styles:Level 2: API Based

Page 46: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Level 2: API Based

<span id="price"></span>

<script> dwr.util.setValue("price", 42);</script>

Page 47: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Level 2: API Based

<span id="price"></span>

// on the serverimport org.directwebremoting.ui.dwr.Util;Util.setValue("price", 42);

Page 48: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Level 2: API Based

<span id="price"></span>

// on the serverimport org....scriptaculous.Effect;Effect.shake("price");

Page 49: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Level 2: API Based

import org.dojotoolkit.dijit.Dijit;import org.dojotoolkit.dijit.Editor;

Editor e = Dijit.byId("price",Editor.class);e.setValue(42);

Page 50: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Level 2: API Based

• Tighter coupling• Richer API

Page 51: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

API Styles:Level 3: Data Sync Based

Page 52: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

Level 3: Data Sync API

dataStore.add(person);

Page 53: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

• Trivial to understand• Enables smart network control• Very hard to get 100%

Level 3: Data Sync Based

Page 54: Comet and the Rise of Highly Interactive Websites

© SitePen, Inc. 2008. All Rights Reserved

CometThe Rise of Highly Interactive Websites

http://directwebremoting.org/http://cometdaily.org/

http://sitepen.com/