SlideShare una empresa de Scribd logo
1 de 35
HTML 5
WebSockets
Volodymyr Lavrynovych
HTTP Hacks
 Polling
 Long Polling
 Streaming
HTTP request-response (Polling)
Client Server
Connect-Poll
No data in response
Connect-Poll
No data in response
Connect-Poll
Data
Server-Sent Events (Long Polling)
Client Server
Connect-Poll
Data
Poll
Data
Streaming
Client Server
Connect
Push
Close Connection
Push
Push
Push
WebSocketBidirectional communication technology for web apps
WebSocket
 Standard W3C protocol (RFC6455)
 Web Browsers include window.WebSocket
object. No plugins required
 Java EE 7 includes WebSocket API (JSR-356)
W3C: WebSocket Interface
Security
The wss encryption is done the same way as in https
HTTP://... HTTPS://...
WS://… WSS://…
How it works?
1. Establish a socket connection via HTTP for
the initial handshake
2. Switch the protocol from HTTP to a socket-
based protocol
3. Send messages in both directions
simultaneously
The WebSocket handshake
Browser request Server response
GET /mychat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key:
x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: chat
Sec-WebSocket-Version: 13
Origin: http://example.com
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept:
HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol: chat
Polling – overhead calculation
A:
1,000 clients polling every second: Network throughput is (959 x 1,000) =
959,000 bytes = 7,672,000 bits per second (7,67 megabits per second)
B:
10,000 clients polling every second: Network throughput is (959 x 10,000) =
9,590,000 bytes = 76,720,000 bits per second (76,72 Mbps)
C:
10,000 clients polling every second: Network throughput is (959 x 100,000) =
95,900,000 bytes = 767,200,000 bits per second (767,2 Mbps)
WebSocket – overhead calculation
A:
1,000 clients receive 1 message per second: Network throughput is (2 x 1,000) =
2,000 bytes = 16,000 bits per second (0.016 megabits per second)
B:
10,000 clients receive 1 message per second: Network throughput is (2 x
10,000) = 20,000 bytes = 160,000 bits per second (0.16 Mbps)
C:
100,000 clients receive 1 message per second: Network throughput is (2 x
100,000) = 200,000 bytes = 1,600,000 bits per second (1,6 Mbps)
Comparing overheads
megabits per second
Polling, C,
767.2
Polling, B,
76.72
Polling, A, 7.67
WebSocket, C,
1.6
WebSocket, B,
0.16
WebSocket, A,
0.016
WebSocket Polling
window.WebSocket object
JS: WebSocket events
Event Event Handler Description
open ws.onopen This event occurs when socket
connection is established.
message ws.onmessage This event occurs when client
receives data from server.
error ws.onerror This event occurs when there is
any error in communication.
close ws.onclose This event occurs when
connection is closed.
JS: WebSocket methods
Method Description
ws.send() The send(data) method transmits data
using the connection.
ws.close() The close() method would be used to
terminate any existing connection.
JS: WebSocket object creation
& WebSocket
How to?
 Create an endpoint class.
 Implement the lifecycle methods of the
endpoint.
 Add your business logic to the endpoint.
 Deploy the endpoint inside a web application.
Programmatic Endpoints
To deploy this programmatic endpoint, use the following code in your Java EE application:
Annotated Endpoints
Annotation Event Example
OnOpen Connection opened. @OnOpen public void open(Session session, EndpointConfig conf) { }
OnMessage Message received. @OnMessage public void message (Session session, String msg) { }
OnError Connection error. @OnError public void error(Session session, Throwable error) { }
OnClose Connection closed. @OnClose public void close(Session session, CloseReason reason) { }
Simple Chat
Example
index.html
chat.js
ChatEndpoint.java (part 1)
ChatEndpoint.java (part 2)
Configurator.java
UI
WebSocket Frame Inspection
(with Google Chrome Developer Tools)
Demo
My notes
 WebSockets needed 20 sec to identify disconnection
on client-side
 No possibility to identify disconnection on server-side
 To get HttpSession or other data from outside you
should use your implementation of
ServerEndpointConfig.Configurator class.
 Each connection to WebSocket server creates new
instance of javax.websocket.Session class
Compatibility
IE Firefox Chrome Safari Opera
iOS
Safari
Opera
Mini
Android
Browser
Blackberry
Browser
IE
Mobile
Current 11.0 25.0 30.0 7.0 17.0 7.0 5.0-7.0 4.2-4.3 10.0 10.0
Near
future 31.0 18.0
Source: http://caniuse.com/#feat=websockets
Flash-based workaround
Links
Java API for WebSocket
http://docs.oracle.com/javaee/7/tutorial/doc/websocket.htm
HTML5 - WebSockets Tutorial
http://www.tutorialspoint.com/html5/html5_websocket.htm
The WebSocket API
http://www.w3.org/TR/websockets/
Flash-based workaround
https://github.com/gimite/web-socket-js
Thank you!
vlavrynovych@malkosua.com

Más contenido relacionado

La actualidad más candente

Websockets at tossug
Websockets at tossugWebsockets at tossug
Websockets at tossug
clkao
 
Websockets in Node.js - Making them reliable and scalable
Websockets in Node.js - Making them reliable and scalableWebsockets in Node.js - Making them reliable and scalable
Websockets in Node.js - Making them reliable and scalable
Gareth Marland
 
WebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
WebSockets: The Current State of the Most Valuable HTML5 API for Java DevelopersWebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
WebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
Viktor Gamov
 
WEB SOCKET 應用
WEB SOCKET 應用WEB SOCKET 應用
WEB SOCKET 應用
Jerromy Lee
 

La actualidad más candente (20)

HTML5 WebSocket: The New Network Stack for the Web
HTML5 WebSocket: The New Network Stack for the WebHTML5 WebSocket: The New Network Stack for the Web
HTML5 WebSocket: The New Network Stack for the Web
 
J web socket
J web socketJ web socket
J web socket
 
Nuts and Bolts of WebSocket Devoxx 2014
Nuts and Bolts of WebSocket Devoxx 2014Nuts and Bolts of WebSocket Devoxx 2014
Nuts and Bolts of WebSocket Devoxx 2014
 
HTML5 WebSockets
HTML5 WebSocketsHTML5 WebSockets
HTML5 WebSockets
 
Introduction to WebSockets Presentation
Introduction to WebSockets PresentationIntroduction to WebSockets Presentation
Introduction to WebSockets Presentation
 
HTML5 WebSocket for the Real-Time Web and the Internet of Things
HTML5 WebSocket for the Real-Time Weband the Internet of ThingsHTML5 WebSocket for the Real-Time Weband the Internet of Things
HTML5 WebSocket for the Real-Time Web and the Internet of Things
 
Building Next Generation Real-Time Web Applications using Websockets
Building Next Generation Real-Time Web Applications using WebsocketsBuilding Next Generation Real-Time Web Applications using Websockets
Building Next Generation Real-Time Web Applications using Websockets
 
Websockets at tossug
Websockets at tossugWebsockets at tossug
Websockets at tossug
 
Php push notifications
Php push notificationsPhp push notifications
Php push notifications
 
Realtime web application with java
Realtime web application with javaRealtime web application with java
Realtime web application with java
 
Spring + WebSocket integration
Spring + WebSocket integrationSpring + WebSocket integration
Spring + WebSocket integration
 
Ws
WsWs
Ws
 
Websockets in Node.js - Making them reliable and scalable
Websockets in Node.js - Making them reliable and scalableWebsockets in Node.js - Making them reliable and scalable
Websockets in Node.js - Making them reliable and scalable
 
WebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
WebSockets: The Current State of the Most Valuable HTML5 API for Java DevelopersWebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
WebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
 
Asynchronous Web Programming with HTML5 WebSockets and Java
Asynchronous Web Programming with HTML5 WebSockets and JavaAsynchronous Web Programming with HTML5 WebSockets and Java
Asynchronous Web Programming with HTML5 WebSockets and Java
 
WebSockets with Spring 4
WebSockets with Spring 4WebSockets with Spring 4
WebSockets with Spring 4
 
WEB SOCKET 應用
WEB SOCKET 應用WEB SOCKET 應用
WEB SOCKET 應用
 
Getting Started with WebSocket and Server-Sent Events in Java
Getting Started with WebSocket and Server-Sent Events in JavaGetting Started with WebSocket and Server-Sent Events in Java
Getting Started with WebSocket and Server-Sent Events in Java
 
Large scale web socket system with AWS and Web socket
Large scale web socket system with AWS and Web socketLarge scale web socket system with AWS and Web socket
Large scale web socket system with AWS and Web socket
 
Web-Socket
Web-SocketWeb-Socket
Web-Socket
 

Destacado

Il primo anno di vita del bambino
Il primo anno di vita del bambinoIl primo anno di vita del bambino
Il primo anno di vita del bambino
Azza
 

Destacado (10)

Introduction to WebSockets
Introduction to WebSocketsIntroduction to WebSockets
Introduction to WebSockets
 
Presentation websockets
Presentation websocketsPresentation websockets
Presentation websockets
 
reveal.js 3.0.0
reveal.js 3.0.0reveal.js 3.0.0
reveal.js 3.0.0
 
Building WebSocket and Server Side Events Applications using Atmosphere
Building WebSocket and Server Side Events Applications using AtmosphereBuilding WebSocket and Server Side Events Applications using Atmosphere
Building WebSocket and Server Side Events Applications using Atmosphere
 
Programming WebSockets with Glassfish and Grizzly
Programming WebSockets with Glassfish and GrizzlyProgramming WebSockets with Glassfish and Grizzly
Programming WebSockets with Glassfish and Grizzly
 
Kaazing
KaazingKaazing
Kaazing
 
Il primo anno di vita del bambino
Il primo anno di vita del bambinoIl primo anno di vita del bambino
Il primo anno di vita del bambino
 
Top 10 real life WebSocket use cases & experiences - Devoxx UK 2015
Top 10 real life WebSocket use cases & experiences - Devoxx UK 2015Top 10 real life WebSocket use cases & experiences - Devoxx UK 2015
Top 10 real life WebSocket use cases & experiences - Devoxx UK 2015
 
The Heartbleed Attack
The Heartbleed AttackThe Heartbleed Attack
The Heartbleed Attack
 
Culture
CultureCulture
Culture
 

Similar a vlavrynovych - WebSockets Presentation

Dev con kolkata 2012 websockets
Dev con kolkata 2012   websocketsDev con kolkata 2012   websockets
Dev con kolkata 2012 websockets
SANKARSAN BOSE
 
Codecamp iasi-26 nov 2011-web sockets
Codecamp iasi-26 nov 2011-web socketsCodecamp iasi-26 nov 2011-web sockets
Codecamp iasi-26 nov 2011-web sockets
Codecamp Romania
 
Pushing Datatothe Browserwith Comet Ajax W
Pushing Datatothe Browserwith Comet Ajax WPushing Datatothe Browserwith Comet Ajax W
Pushing Datatothe Browserwith Comet Ajax W
rajivmordani
 

Similar a vlavrynovych - WebSockets Presentation (20)

Html5 websockets
Html5 websocketsHtml5 websockets
Html5 websockets
 
Enhancing Mobile User Experience with WebSocket
Enhancing Mobile User Experience with WebSocketEnhancing Mobile User Experience with WebSocket
Enhancing Mobile User Experience with WebSocket
 
WebSockets in JEE 7
WebSockets in JEE 7WebSockets in JEE 7
WebSockets in JEE 7
 
Lecture 6 Web Sockets
Lecture 6   Web SocketsLecture 6   Web Sockets
Lecture 6 Web Sockets
 
Dev con kolkata 2012 websockets
Dev con kolkata 2012   websocketsDev con kolkata 2012   websockets
Dev con kolkata 2012 websockets
 
Websocket
WebsocketWebsocket
Websocket
 
Jwebsocketmobiletechcon2010en 100912071225 Phpapp01
Jwebsocketmobiletechcon2010en 100912071225 Phpapp01Jwebsocketmobiletechcon2010en 100912071225 Phpapp01
Jwebsocketmobiletechcon2010en 100912071225 Phpapp01
 
jWebSocket MobileTechCon 2010 - WebSockets on Android, Symbian and BlackBerry
jWebSocket MobileTechCon 2010 - WebSockets on Android, Symbian and BlackBerryjWebSocket MobileTechCon 2010 - WebSockets on Android, Symbian and BlackBerry
jWebSocket MobileTechCon 2010 - WebSockets on Android, Symbian and BlackBerry
 
Connected Web Systems
Connected Web SystemsConnected Web Systems
Connected Web Systems
 
WebSockets Jump Start
WebSockets Jump StartWebSockets Jump Start
WebSockets Jump Start
 
Websocket
WebsocketWebsocket
Websocket
 
Codecamp iasi-26 nov 2011-web sockets
Codecamp iasi-26 nov 2011-web socketsCodecamp iasi-26 nov 2011-web sockets
Codecamp iasi-26 nov 2011-web sockets
 
Webinar slides "Building Real-Time Collaborative Web Applications"
Webinar slides "Building Real-Time Collaborative Web Applications"Webinar slides "Building Real-Time Collaborative Web Applications"
Webinar slides "Building Real-Time Collaborative Web Applications"
 
Codecamp Iasi-26 nov 2011 - Html 5 WebSockets
Codecamp Iasi-26 nov 2011 - Html 5 WebSocketsCodecamp Iasi-26 nov 2011 - Html 5 WebSockets
Codecamp Iasi-26 nov 2011 - Html 5 WebSockets
 
Decoding real time web communication
Decoding real time web communicationDecoding real time web communication
Decoding real time web communication
 
Building real-time-collaborative-web-applications
Building real-time-collaborative-web-applicationsBuilding real-time-collaborative-web-applications
Building real-time-collaborative-web-applications
 
Fight empire-html5
Fight empire-html5Fight empire-html5
Fight empire-html5
 
Pushing Datatothe Browserwith Comet Ajax W
Pushing Datatothe Browserwith Comet Ajax WPushing Datatothe Browserwith Comet Ajax W
Pushing Datatothe Browserwith Comet Ajax W
 
KSDG-iSlide App 開發心得分享
KSDG-iSlide App 開發心得分享KSDG-iSlide App 開發心得分享
KSDG-iSlide App 開發心得分享
 
Building interactivity with websockets
Building interactivity with websocketsBuilding interactivity with websockets
Building interactivity with websockets
 

vlavrynovych - WebSockets Presentation

  • 2. HTTP Hacks  Polling  Long Polling  Streaming
  • 3. HTTP request-response (Polling) Client Server Connect-Poll No data in response Connect-Poll No data in response Connect-Poll Data
  • 4. Server-Sent Events (Long Polling) Client Server Connect-Poll Data Poll Data
  • 7. WebSocket  Standard W3C protocol (RFC6455)  Web Browsers include window.WebSocket object. No plugins required  Java EE 7 includes WebSocket API (JSR-356)
  • 9. Security The wss encryption is done the same way as in https HTTP://... HTTPS://... WS://… WSS://…
  • 10. How it works? 1. Establish a socket connection via HTTP for the initial handshake 2. Switch the protocol from HTTP to a socket- based protocol 3. Send messages in both directions simultaneously
  • 11. The WebSocket handshake Browser request Server response GET /mychat HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw== Sec-WebSocket-Protocol: chat Sec-WebSocket-Version: 13 Origin: http://example.com HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk= Sec-WebSocket-Protocol: chat
  • 12. Polling – overhead calculation A: 1,000 clients polling every second: Network throughput is (959 x 1,000) = 959,000 bytes = 7,672,000 bits per second (7,67 megabits per second) B: 10,000 clients polling every second: Network throughput is (959 x 10,000) = 9,590,000 bytes = 76,720,000 bits per second (76,72 Mbps) C: 10,000 clients polling every second: Network throughput is (959 x 100,000) = 95,900,000 bytes = 767,200,000 bits per second (767,2 Mbps)
  • 13. WebSocket – overhead calculation A: 1,000 clients receive 1 message per second: Network throughput is (2 x 1,000) = 2,000 bytes = 16,000 bits per second (0.016 megabits per second) B: 10,000 clients receive 1 message per second: Network throughput is (2 x 10,000) = 20,000 bytes = 160,000 bits per second (0.16 Mbps) C: 100,000 clients receive 1 message per second: Network throughput is (2 x 100,000) = 200,000 bytes = 1,600,000 bits per second (1,6 Mbps)
  • 14. Comparing overheads megabits per second Polling, C, 767.2 Polling, B, 76.72 Polling, A, 7.67 WebSocket, C, 1.6 WebSocket, B, 0.16 WebSocket, A, 0.016 WebSocket Polling
  • 16. JS: WebSocket events Event Event Handler Description open ws.onopen This event occurs when socket connection is established. message ws.onmessage This event occurs when client receives data from server. error ws.onerror This event occurs when there is any error in communication. close ws.onclose This event occurs when connection is closed.
  • 17. JS: WebSocket methods Method Description ws.send() The send(data) method transmits data using the connection. ws.close() The close() method would be used to terminate any existing connection.
  • 20. How to?  Create an endpoint class.  Implement the lifecycle methods of the endpoint.  Add your business logic to the endpoint.  Deploy the endpoint inside a web application.
  • 21. Programmatic Endpoints To deploy this programmatic endpoint, use the following code in your Java EE application:
  • 22. Annotated Endpoints Annotation Event Example OnOpen Connection opened. @OnOpen public void open(Session session, EndpointConfig conf) { } OnMessage Message received. @OnMessage public void message (Session session, String msg) { } OnError Connection error. @OnError public void error(Session session, Throwable error) { } OnClose Connection closed. @OnClose public void close(Session session, CloseReason reason) { }
  • 29. UI
  • 30. WebSocket Frame Inspection (with Google Chrome Developer Tools) Demo
  • 31. My notes  WebSockets needed 20 sec to identify disconnection on client-side  No possibility to identify disconnection on server-side  To get HttpSession or other data from outside you should use your implementation of ServerEndpointConfig.Configurator class.  Each connection to WebSocket server creates new instance of javax.websocket.Session class
  • 32. Compatibility IE Firefox Chrome Safari Opera iOS Safari Opera Mini Android Browser Blackberry Browser IE Mobile Current 11.0 25.0 30.0 7.0 17.0 7.0 5.0-7.0 4.2-4.3 10.0 10.0 Near future 31.0 18.0 Source: http://caniuse.com/#feat=websockets
  • 34. Links Java API for WebSocket http://docs.oracle.com/javaee/7/tutorial/doc/websocket.htm HTML5 - WebSockets Tutorial http://www.tutorialspoint.com/html5/html5_websocket.htm The WebSocket API http://www.w3.org/TR/websockets/ Flash-based workaround https://github.com/gimite/web-socket-js