INTRODUCTION TO
       WEBSOCKETS
           jgourgoutis@linkedin.com


   WebSockets are a new way to connect
 clients and servers with efficient two-way
communication that allows us to build true
      desktop-style web applications!
Legacy Web
HTTP Protocol
• High Overhead (request & response
  headers, cookies, etc.)
• “Half-duplex” connection
  (request/response)
Current Workarounds
• AJAX/XHR
  – Content can update w/o full page refresh
  – User-perceived low latency
  – Faux real-time via polling/long-polling
• Comet
  – No standard specification
  – Very complex to implement
HTML5 WebSockets
WebSocket Advantages!
•   Full-duplex, low overhead
•   Direct connection between client & server
•   Same ports as HTTP/HTTPS
•   Based on Web Standards (W3C & IETF)
•   Unlike XHR, it works cross-domain!
More Details
• “ws” and “wss” protocols
• Two-part “handshake”: HTTP upgrade to WS
  in request header
WebSocket API
varws = new WebSocket(url);
ws.onopen = function (evt) { /* connection
  open */ };
ws.onmessage= function (evt) { /* received
  message */ console.dir(evt.data); };
ws.onclose= function(evt){ /* connection
  closed */ };
WebSocket API (ctd)
// Send a message
ws.send(“Do you like the scary clowns?!?”);

// Close the connection
ws.close();
WebSockets Browser Support
Server Events
Not Like WebSockets!
•   Not two-way, just push from server
•   Uses plain-ol’ HTTP
•   Different message transport mechanism
•   Same-origin policy applies.
EventSource API
vares = new EventSource(url);
es.readyState property
0 = connecting,1 = open, 2 = closed
es.close() closes the connection
EventSource Events
es.onopen = function(evt){};
es.onmessage = function(evt){};
es.onerror = function(evt){};
es.addEventListener();

Data object: evt.data
Sending Messages
• MIME type = text/event-stream
• Plain-text format
• id: message; id defaults to “data” if not
  specified
• New lines (n) are significant
EventSource Browser Support
SEXY
DEMO
TIME!
I built a web chat application using WebSocket and EventSource.
Oh, and some Node.js and Socket.io, too.
http://jgourgou-mn.linkedin.biz:8080/

SUPER-AWESOME WEBSOCKET
CHAT DEMO
Take-Aways
• Socket.io is to WebSockets as Jquery is to
  DOM
• Socket.io documentation sucks.
• Web examples for Socket.io suck.
• Node.js is weirdly cool, and overwhelming in
  a Java sort of way.
Resources/Further Reading
• http://www.slideshare.net/pmoskovi/building-living-
  web-applications-with-html5-websockets
• http://howtonode.org/websockets-socketio
• https://github.com/LearnBoost/socket.io
• http://www.websocket.org/
• http://www.nczonline.net/blog/2010/10/19/introducti
  on-to-server-sent-events/
• https://github.com/remy/eventsource-h5d
• http://www.html5rocks.com/en/tutorials/eventsourc
  e/basics/
• http://html5doctor.com/server-sent-events/
Questions?

Intro to Web Sockets

  • 1.
    INTRODUCTION TO WEBSOCKETS jgourgoutis@linkedin.com WebSockets are a new way to connect clients and servers with efficient two-way communication that allows us to build true desktop-style web applications!
  • 2.
  • 3.
    HTTP Protocol • HighOverhead (request & response headers, cookies, etc.) • “Half-duplex” connection (request/response)
  • 4.
    Current Workarounds • AJAX/XHR – Content can update w/o full page refresh – User-perceived low latency – Faux real-time via polling/long-polling • Comet – No standard specification – Very complex to implement
  • 5.
  • 6.
    WebSocket Advantages! • Full-duplex, low overhead • Direct connection between client & server • Same ports as HTTP/HTTPS • Based on Web Standards (W3C & IETF) • Unlike XHR, it works cross-domain!
  • 7.
    More Details • “ws”and “wss” protocols • Two-part “handshake”: HTTP upgrade to WS in request header
  • 8.
    WebSocket API varws =new WebSocket(url); ws.onopen = function (evt) { /* connection open */ }; ws.onmessage= function (evt) { /* received message */ console.dir(evt.data); }; ws.onclose= function(evt){ /* connection closed */ };
  • 9.
    WebSocket API (ctd) //Send a message ws.send(“Do you like the scary clowns?!?”); // Close the connection ws.close();
  • 10.
  • 11.
  • 12.
    Not Like WebSockets! • Not two-way, just push from server • Uses plain-ol’ HTTP • Different message transport mechanism • Same-origin policy applies.
  • 13.
    EventSource API vares =new EventSource(url); es.readyState property 0 = connecting,1 = open, 2 = closed es.close() closes the connection
  • 14.
    EventSource Events es.onopen =function(evt){}; es.onmessage = function(evt){}; es.onerror = function(evt){}; es.addEventListener(); Data object: evt.data
  • 15.
    Sending Messages • MIMEtype = text/event-stream • Plain-text format • id: message; id defaults to “data” if not specified • New lines (n) are significant
  • 16.
  • 17.
  • 18.
    I built aweb chat application using WebSocket and EventSource. Oh, and some Node.js and Socket.io, too. http://jgourgou-mn.linkedin.biz:8080/ SUPER-AWESOME WEBSOCKET CHAT DEMO
  • 19.
    Take-Aways • Socket.io isto WebSockets as Jquery is to DOM • Socket.io documentation sucks. • Web examples for Socket.io suck. • Node.js is weirdly cool, and overwhelming in a Java sort of way.
  • 20.
    Resources/Further Reading • http://www.slideshare.net/pmoskovi/building-living- web-applications-with-html5-websockets • http://howtonode.org/websockets-socketio • https://github.com/LearnBoost/socket.io • http://www.websocket.org/ • http://www.nczonline.net/blog/2010/10/19/introducti on-to-server-sent-events/ • https://github.com/remy/eventsource-h5d • http://www.html5rocks.com/en/tutorials/eventsourc e/basics/ • http://html5doctor.com/server-sent-events/
  • 21.

Editor's Notes

  • #2 “Building Living Web Applications with HTML5 Websockets” presentation from Peter Moskovits @Kaazing
  • #4 “Half-duplex” is analogous to picking up a phone, dialing a number, waiting for the other end to pick up, asking a question, getting a response, hanging up, and then repeating for the next question – instead of having a fluid, 10-minute conversation.
  • #5 A type-ahead widget sending the letter “a” back to the server via XHR incurs a cost of 788  bytes  in the header for the 1  byte of the character