ProgrammingWebSocketsSeanSullivanOSCONJuly22,2010AboutmeWebapplicationdevelopersHTML5!improvedJavaScriptimplementations!WebSockets!WebSockets?WebSocketsatechnologythatenablesbidirectionalcommunicationbetweenwebbrowsersandserver-sideprocesses“TCPfortheweb”PushtechnologyCometWebSocketsAJAXClient-servercommunicationExampleMulti-playergameRockPaperScissorsLizardSpockSpecifications•WebSocketsAPI•WebSocketsprotocolJavaScriptexamplevarsock=newWebSocket('wss://);sock.onopen=function(evt){alert(‘open!’);};sock.onmessage=function(evt){alert(‘Message:‘+evt.data);}sock.onerror=function(evt){alert(‘error!’);};sock.onclose=function(evt){alert(‘closed!’);}sock.send(“HelloOSCON!”);sock.close();WebSocketURLs•ws://example.com/demo•wss://example.com/demoeventhandlers•onopen•onmessage•onerror•oncloseSpecifications•WebSocketsAPI•WebSocketsprotocol51pages!WebSocketsprotocol•ports80and443•HTTP“upgrade”handshakeBrowserrequestGET/testHTTP/1.1Upgrade:WebSocketConnection:UpgradeOrigin:::WebSocketConnection:UpgradeServer:FooServer/1.5WebSocket-Location:ws://::0Date:Fri,08May201007:23:58GMTtheWebSocketnetworkprotocolisstillchanging!Protocolevolution•draft-hixie-thewebsocketprotocol-75•draft-hixie-thewebsocketprotocol-76•draft-ietf-hybi-thewebsocketprotocol-00DevelopersshouldbeawarethatstartingfromWebKitnightlybuildr59903andChrome6.0.414.0(r47952),theclientwilltalktoaserverusing-76versionoftheprotocol,soitwillfailtoopenWebSocketconnectionswithaWebSocketserverbasedondraft-hixie-thewebsocketprotocol-75.Since-75versionoftheprotocolisobsoletedandnolongersupportedinfutureversionofbrowsers,tosupportnewclientsyouneedtoupdatetheserverimplementation.(NotethatChrome5uses-75versionofprotocol)source::•GoogleChrome4.0.249.0andhigher•Safari5.0•Firefox:4.0beta1•InternetExplorer9:TBDWebSocketsontheserver-side=47485Nostandardserver-sideJavaAPIOtherJavaprojects•Glassfish/Grizzly•jWebSocket•JBossNetty•CauchoResinConclusion•WebSocketsisanemergingtechnology•JavaScriptAPIiseasytolearn•JavaScriptAPIiseasytouse•Lotsofchoicesontheserver-sideThankyouBonusslides!jQuery•••Flashsockets•Silverlightduplexservices•Comet•WebSocketsWebSocketsandHTML5“Atlastweek'stelecon,whilediscussingISSUE-64,itwasproposedthatwedeclareWebSocket(bothAPIandprotocol)outofscopeforHTML5.SincetheAPIandprotocolhavebeeninseparatespecsforsometime,thiswouldhavenoimmediatematerialeffect.However,itwouldpreventusfromputtingWebSocketbackinthemainHTML5specinthefuture,unlessnewinformationcametolightwhichwouldallowustoreopenthedecision.”September92009“Sincetherewasnoobjection,theresolutionhasnowpassed.”September232009•same-originpolicyapplies•usewss://ifyouwantasecureconnectionDemo:HTML5+WebSockets