Recallthat HTTP is request/response protocol · •Recallthat HTTP is request/response protocol...

13

Transcript of Recallthat HTTP is request/response protocol · •Recallthat HTTP is request/response protocol...

Page 1: Recallthat HTTP is request/response protocol · •Recallthat HTTP is request/response protocol •AJAXallows you to do HTTP request/response within a page, but it is still request/response
Page 2: Recallthat HTTP is request/response protocol · •Recallthat HTTP is request/response protocol •AJAXallows you to do HTTP request/response within a page, but it is still request/response

• Recall thatHTTPisrequest/responseprotocol• AJAX allowsyoutodoHTTPrequest/responsewithinapage,butitisstillrequest/response

• Aclientalwayshastoinitiatecommunication.– I.e.Maketherequest

• Whatifyouwanttheservertobeableto"push"informationdowntotheclient,forexample:– Newemail– Newnews– Updatestoasharedchat/drawingcanvas– Gameevents

©JoeMertz– MobiletoCloud:BuildingDistributedApplications

Page 3: Recallthat HTTP is request/response protocol · •Recallthat HTTP is request/response protocol •AJAXallows you to do HTTP request/response within a page, but it is still request/response

• Workarounds havebeendevised• E.g.Polling

– Client continuouslypollstheserver• E.g.LongPolling

– Clientsendsarequest,whichtheserverdoesnotrespondtountilithassomethingtosend

– Ifsomethingcomesalongtosend,itcompletestheresponseandtheclientwillimmediatelysendanewrequesttoholdontoforlater

– Iftherequesttimesout,theclientsendsanewresponse.– Analogy(fromthedaysofpostalmail)…

• Cheapfriendcan'taffordstamps.• Sendfriendstamp,heusesittosendyoualetter• Whenyoureceivemessage,immediatelysendhimanotherstampforthenexttimehewantstosendaletter.

©JoeMertz– MobiletoCloud:BuildingDistributedApplications

Page 4: Recallthat HTTP is request/response protocol · •Recallthat HTTP is request/response protocol •AJAXallows you to do HTTP request/response within a page, but it is still request/response

• Providesfortrue two-way ongoing communicationbetweenaclientandserver.

• Eachsidecan"send"messages.• Eachsidehaslistener"onmessage"• WebSockets arenotrestrictedbytheSameOriginPolicy– It isuptotheservertochecktheOriginheaderanddecidewhetheritshouldreplyornot.

• Note:– Someold browsersdon't implementWebSockets

©JoeMertz– MobiletoCloud:BuildingDistributedApplications

Page 5: Recallthat HTTP is request/response protocol · •Recallthat HTTP is request/response protocol •AJAXallows you to do HTTP request/response within a page, but it is still request/response

// Create a new WebSocketvar wSocket = new WebSocket("ws://www.example.com/socketserver")

// When the WebSocket is connected and readywSocket.onopen = function (event) {

// Send a message to the serverwSocket.send("Initial message to the server.");

};

// Handler for when a message arrives from the serverwSocket.onmessage = function (event) {

console.log(event.data);};

©JoeMertz– MobiletoCloud:BuildingDistributedApplications

Page 6: Recallthat HTTP is request/response protocol · •Recallthat HTTP is request/response protocol •AJAXallows you to do HTTP request/response within a page, but it is still request/response

©JoeMertz– MobiletoCloud:BuildingDistributedApplications

• RequireaWebSockets module– ws isverypopular– https://www.npmjs.com/package/ws

• CreateanewWebSockets Server• Definehandlersfor– onconnection– onmessage

• Andsendmessagestotheclient

Page 7: Recallthat HTTP is request/response protocol · •Recallthat HTTP is request/response protocol •AJAXallows you to do HTTP request/response within a page, but it is still request/response

• Library forfull-duplexcommunicationbetweenclientsandservers

• Builtonengine.io,socket.io providesanabstractionabovewhatevertransportmethodsareavailable inthebrowser:–WebSockets– LongPolling

©JoeMertz– MobiletoCloud:BuildingDistributedApplications

Page 8: Recallthat HTTP is request/response protocol · •Recallthat HTTP is request/response protocol •AJAXallows you to do HTTP request/response within a page, but it is still request/response

• Alsoprovidesasimpleabstractionfor event-basedprogrammingacrosstheserverandmultipleclients

• Clientscan– emiteventsbacktotheserver– listenforeventsfromtheserver

• Servercan– listen foreventsfromclients– emiteventsbacktotheclient– emiteventstootherclients– broadcastemiteventstoallclients

©JoeMertz– MobiletoCloud:BuildingDistributedApplications

Page 9: Recallthat HTTP is request/response protocol · •Recallthat HTTP is request/response protocol •AJAXallows you to do HTTP request/response within a page, but it is still request/response

• Ateachtable,createagame• Aneventissimplyaword

– Itcanoptionallybeaccompaniedwithashortphrase• OnepersonistheServerwhocan:

– listen foreventsfromclients– emitaneventtooneclient(pointtooneandsay…)– broadcast emiteventstoall clients(pointwithbothhandsandsay…)

• TherestatthetableareClientswhocan– emiteventstotheserver– listenforeventsfromtheserver

• ClientsortheServercangenerateeventsatanytime(dependingonyourgame-play)

• Clientscannotemiteventspeer-to-peer(Servermustrelay)©JoeMertz– MobiletoCloud:BuildingDistributedApplications

Page 10: Recallthat HTTP is request/response protocol · •Recallthat HTTP is request/response protocol •AJAXallows you to do HTTP request/response within a page, but it is still request/response

• on(event,function(parameter-object){…}• emit(event,parameter-object)• events– connection– disconnect– developer-definedevents

©JoeMertz– MobiletoCloud:BuildingDistributedApplications

Page 11: Recallthat HTTP is request/response protocol · •Recallthat HTTP is request/response protocol •AJAXallows you to do HTTP request/response within a page, but it is still request/response

• socket.emit(…)– emitsbacktotheclient

• socket.broadcast.emit(…)– emitstoallconnectedclientsexceptthe socketthatstartsit

©JoeMertz– MobiletoCloud:BuildingDistributedApplications

Page 12: Recallthat HTTP is request/response protocol · •Recallthat HTTP is request/response protocol •AJAXallows you to do HTTP request/response within a page, but it is still request/response

• Demo• Codewalkthrough

©JoeMertz– MobiletoCloud:BuildingDistributedApplications

Page 13: Recallthat HTTP is request/response protocol · •Recallthat HTTP is request/response protocol •AJAXallows you to do HTTP request/response within a page, but it is still request/response

1. AddawelcomemessagetocountPlayers–Whenanewplayerconnects– Serveremitsa"welcome"eventtoit

• Dataoftheeventis{message:"Welcomeplayern"}– Where"n"istheordinalnumberoftheplayerwhohasjoined

2. Createasimpleappinwhichuserscollaborate/compete/communicate.– Actionsbyeachuserisbroadcasttoallusers– Periodically(e.g.everyonesecond)serverbroadcasts

somethingtoallusers

©JoeMertz– MobiletoCloud:BuildingDistributedApplications