Web sockets in java EE 7 - JavaOne 2013
-
Upload
sivasubramaniam-arunachalam -
Category
Technology
-
view
3.537 -
download
9
description
Transcript of Web sockets in java EE 7 - JavaOne 2013
Web Sockets in Java EE 7
Sivasubramaniam Arunachalam
May 09, 2013
@sivaa_in
https://oraclein.activeevents.com/connect/sessionDetail.ww?SESSION_ID=1081&tclass=popup
It’s me!
• Product/Application Developer
• Web/Enterprise/Middleware/B2B
• Java/Java EE, Python/Django
• 2002
• Technical Consultant/Process Mentor
• Member of BOJUG • Speaker
Agenda
• Introduction to Web Sockets
• History
• Protocol Spec
• Adoption
• Java EE 7 Support
• Demo
Web 1.0 Web 1.0
Web 2.0
Web 3.0
https://www.facebook.com/sitetour/chat.php
600+ M Active Users/Day
http:// 1991
1995
2013 Still 22 Years Old
http?
http/tcp (1974) (1991)
request / response
~ 2k bytes
~ 150 ms
State less
Real Time Real Time ~
4,0,0,0,0,0 | 2,0,1,…
http://www.espncricinfo.com/indian-premier-league-2013/engine/match/598051.html
SRH Score Card vs CSK
Over 1 (Mohit) Over 2 (Holder)
146/8 (20 Overs)
(May 08, 2013)
Pull Based (Polling) Periodic Polling (JS/AJAX)
Server Client
SRH 4/0
Latest Score?
Latest Score?
Latest Score?
Same
Same
Latest Score?
SRH 6/0
Latest Score?
Same
Ov 0.1
Ov 0.3
Ov 0.5
Ov 1.1
Ov 1.2
Push Based (Comet) Server Push/Long Polling (JS/AJAX/iframe)
Server Client
SRH 4/0
Latest Score?
Latest Score?
SRH 6/0
Latest Score?
Ov 0.1
Ov 0.2
Ov 1.1
Ov 1.2
• Request / Response
• Too Many
• Server Connections
• Heavy Headers
• Outdated Content
Streaming Server Side Hack
Server Client
SRH 4/0 (1/n)
Latest Score?
SRH 6/0 (2/n)
SRH 146/8 (n/n)
Ov 0.1
Ov 1.1
Ov 20.0
Server Client
SRH 4/0
Latest Score?
SRH 4/0 SRH 6/0 SRH 146/8
Server Buffer
SRH 6/0
SRH 146/8
PROXY
(1/n) (n-1 to go)
(n/n)
(2/n)
(n-2 to go)
Ov 0.1
Ov 1.1
Ov 20.0
• RTMP • Adobe
• Are you Still using Flash?
• RTD • Windows/Excel
• Non Web
Others
Web Sockets (to rescue)
The Web Socket Way The Original Route
Server Client
Hi Client!
Hey Server!
SRH 4/0
Update me the Score
SRH 6/0
SRH 146/8
Not Interested. Bye
Ov 0.1
Ov 1.1
Ov 20.0
Server Client
Hi Client!
Hey Server!
SRH 4/0
Update me the Score
SRH 6/0
SRH 7/0
Not Interested. Bye
Update Over info too
(1.3 ov)
(1.1 ov)
(0.5 ov)
(1.0 ov)
Ov 0.1
Ov 0.5
Ov 1.0
Ov 1.1
Ov 1.2 (1.2 ov)
Ov 1.3
The Background
https://webmq.tavendo.de:9090/help/faq
Let’s Meet Next Year (2014)
Server Side • Hand Shake
• Data Transfer
RFC 6455
Client Side • Java Script API
• Browser Behavior
https://webmq.tavendo.de:9090/help/faq
• tcp • Full Duplex (Bi-Directional)
• Native Browser Support
• No Hacks
• Not Limited to Web
Fundamentals
+ http
• 80 / 443 (plain/secure)
• No Proxy/Firewall pains
• Stream of Messages (Not Bytes)
• ws:// & wss://
• Only ONE Connection (up/down streams)
The Spec
2 Bytes Header (for each message)
Still Why?
~ 50 ms (Latency)
1000 x
3 x
http://www.codeproject.com/Articles/437342/DotNet-WebSocket-Programming
1 Message/Second to each Client
1,000 clients 10,000 clients 1,00,000 clients
• GET (request)
• 101 (response code)
• Not 200 OK
• http:// -> ws://
The Upgrade
RFC 2616 (14.42)
GET /demo HTTP/1.1
Origin : http://sivaa.in
Host : sivaa.in
Connection : Upgrade
Upgrade : WebSocket
WebSocket-Protocol : json, chat, <protocol>
WebSocket-extensions : compression, <extn>
WebSocket-Version : 10
Client Request
HTTP/1.1 101 Switching Protocols
Connection : Upgrade
Upgrade : WebSocket
WebSocket-Protocol : json, chat, <protocol>
WebSocket-Origin : http://sivaa.in
WebSocket-Location : ws://sivaa.in/demo
Server Response
Challenge by Client
Sec-WebSocket-Key : Key (Random 128 bits -> Base 64)
Response By Server
Sec-WebSocket-Accept : Key + GUID* (SHA1)
[NOT FOR SECURITY]
* 258EAFA5-E914-47DA- 95CA-C5AB0DC85B11 (RFC 6455)
Challenge - Response
http://tools.ietf.org/html/rfc6455#page-28
Client / Server Client / Server
Peer <-> Peer
• Frames (TCP) •
• No request / response behavior • No Correlation too
• Independent
• # of requests != # of responses
Data Transfer
• Headers
• Cookies
• Authentication
And No
• Custom Message Patterns
• Technical / Business
• Request / Response • 1 – 1
• 1 to Many
• Acknowledgements
Sub Protocols
Sub Protocols - Current
http://www.iana.org/assignments/websocket/websocket.xml#subprotocol-name
Extensions
• Compression
• Multiplexing
• Private Extenstions (x-)
Client -> Ping
Server -> Pong
Ping / Pong
• Keep-Alive
• Heart Beat
• Latency Metrics
• N/W Status Probing
• Detect Failed Connections
Server Support
IDL (Java Script)
http://www.w3.org/TR/websockets/
http://www.w3.org/TR/websockets/
http://www.w3.org/TR/websockets/
http://www.w3.org/TR/websockets/
http://www.w3.org/TR/websockets/
1. var ws = new WebSocket("ws://sivaa.in/demo");
2. ws.onopen = function() {
ws.send("Hello Server!");
};
3. ws.onmessage = function(event) {
alert("Reply from Server " + event.data);
};
n-1. ws.send("This is the message sent by the client");
n. ws.close()
Developer Friendly
CONNECTING
OPEN
CLOSING
CLOSED
• Closing Long Lived Connections
• Unresponsive Server
• Buffering Server Response
• Non Encryption
• More Latency
The Guards (Proxy / Firewall)
• Web Sockets isn’t Proxy aware
• Stripping HTTP Headers • Upgrade
• Options to disable
• Rejecting Server Response
• Frames (HTTP Headers Expected)
• wss:// - No issues
Web Sockets and
The Guards
• 501
• 301
• Limited Support
• Inconsistent Behavior
The Hidden Blades
• No Polling (0%)
• Low Bandwidth
• Low Latency
• High Interoperability (expected)
Some Marketing Facts
The Vendors (Server/Client/Proxy)
http://static.ibnlive.in.com/ibnlive/pix/slideshow/12-2011/happy-birthday-rajinikaths/rajini-dec11-8.jpg http://media2.intoday.in/indiatoday/images/stories//2012december/rajinikanth_660_121112060009.jpg
• Web Socket
• Flash
• Long Polling
• Multi-Part Streaming
• Forever Frame
• Long Polling JSONP
The Best Approach
• SSE (Server Side Events)
• SPDY
Web Sockets vs
• JSR 356
• Java EE 7
• Tyrus ( http://tyrus.java.net/ )
• Web Socket SDK
• Reference Implementation
Java EE 7 & Web Socket
• Handshake Response to Clients
• Source/Origin Check
• URI Resolving & Matching
• Sub Protocols Negotiation
• Extension Negotiation
Server Side Capabilities (Expected)
Handlers Handler Purpose
End Point To handle life cycle events
Session To represent currently active session
Remote End Point To represent a peer
Message Handler To handle incoming messages
Error Handler Handler for error scenarios
• Interface Driven
• Annotation Driven
Models
• Text Based
• Binary
• Ping/Pong
Message Types
• Text
• TextStream
• Binary
• BinaryStream
Encoders/Decoders
• javax.websocket.*
• javax.websocket.server.*
• javax.net.websocket.*
Packages
Recent Refactoring Changes
https://blogs.oracle.com/PavelBucek/entry/tyrus_1_0_b12_released (Feb 27, 2013)
Web Sockets & Servlet 3.1
• HTTP Upgrade
• HttpServletRequest.upgrade()
• javax.servlet.http.HttpUpgradeHandler
• javax.servlet.http.WebConnection
• Apache Tomcat 7
• GlassFish 3.1
• Jetty 7
• JBoss 7 • And much more
Web Server Support
https://java.net/projects/websocket-spec/pages/WebSocketAPIs
17+
• Annotations / Interface
• Reference Implementation
• Web Sockets SDK / mvn
• Samples to Begin with
GlassFish
Lets Play!
• Subversion (svn)
• Maven (mvn)
• NetBeans IDE (7.3)
• Glassfish Promoted Build (latest)
Tools (which I use)
1. Get your tools ready
2. Configure GlassFish in NetBeans
3. # svn checkout http://svn.java.net/svn/websocket-sdk~source-code-repository/trunk/samples/
4. # mvn clean install
5. Import in Latest Net Beans
6. Run it
Steps
Demo
Thank You! [email protected]
bit.ly/sivasubramaniam bit.ly/sivaa_in
References
• http://www.inc.com/ss/brief-history-time-management#7 • http://resources3.news.com.au/images/2013/01/30/1226564/845955-tim-berners-lee.jpg • http://netdna.webdesignerdepot.com/uploads/2009/01/macii.jpg#old%20apple%20computer%20559x450 • http://3.bp.blogspot.com/_zVkfb2MIt4A/S8f_qzhuhRI/AAAAAAAAAjg/2idf7_G4wxo/s1600/apple-imac.png • http://www.softwareforeducation.com/wikileki/images/c/c4/Simplex-half-full.gif • http://www.mediafly.com/wp-content/uploads/Speed_Rocket-2.png • http://weaponsman.com/wp-content/uploads/2013/01/thumbs-down.png • http://m.flikie.com/ImageData/WallPapers/d1bef9f8be6d479387953929c766699a.jpg • http://www.tavendo.de/webmq/resources/faq • http://deadliestwebattacks.com/tag/websocket/ • http://en.wikipedia.org/wiki/WebSocket • http://www.infoq.com/articles/Web-Sockets-Proxy-Servers • http://media2.intoday.in/indiatoday/images/stories//2012december/rajinikanth_660_121112060009.jpg