Java Day
     Web Sockets in
       Java EE 7
                                             February 23, 2013



Sivasubramaniam Arunachalam                       @sivaa_in


                  http://www.meetup.com/BangaloreOpenJUG/events/102904102/
It’s me!

• Application Developer
    • Web/Enterprise/Middleware/B2B
    • Java/Java EE, Python/Django
       •      2002

• Technical Consultant
• Process Mentor
•   Speaker
Agenda
• Introduction to Web Sockets
  • History
  • Protocol Spec
  • Adoption
• Java EE 7 Support
• Demo
Tweet Ping
Web 1.0

Web 2.0
Web 3.0
  Web 4.0
600+ M                                       Active Users/Day




https://www.facebook.com/sitetour/chat.php
1991


  http://             1995




Still 22 Years Old
                     2013
     http?
http/tcp
           (1991)    (1974)




request / response

    State less
   ~ 2k bytes

   ~ 150 ms
~ Real   Time
Pull Based (Polling)
             Periodic Polling (JS/AJAX)


                                Latest Score?
         AUS 312/7
                               Latest Score?
         Same
                               Latest Score?
         Same
Server                                          Client
                               Latest Score?
         AUS 316/7

                               Latest Score?
         Same
Push Based (Comet)
         Server Push/Long Polling (JS/AJAX/iframe)


                               Latest Score?
         AUS 312/7
                               Latest Score?




Server                                               Client

         AUS 316/7

                               Latest Score?
• Request / Response
 • Too Many
 • Server Connections
• Heavy Headers
• Outdated Content
Streaming Server Side Hack


                                Latest Score?
         AUS 312/7     (1/n)




Server                                          Client

         AUS 316/7    (2/n)



         IND 0/0      (n/n)
Latest Score?

                     (1/n)
         AUS 312/7   (n-1 to go)




Server                Server
                       Buffer                      Client

         AUS 316/7
                     (2/n)
                                     AUS 312/7
                     (n-2 to go)
                                     AUS 316/7
         IND 0/0                     IND 0/0
                     (n/n)

                     PROXY
Others
• RTMP
 • Adobe
 • Are you Still using Flash?


• RTD
 • Windows/Excel
 • Non Web
Web Sockets (to rescue)
The Web Socket Way    The Original Route


                                   Hey Server!
         Hi Client!

                            Update me the Score

         AUS 312/7

Server                                               Client
         AUS 316/7



         IND 0/0


                               Not Interested. Bye
Hey Server!
          Hi Client!

                            Update me the Score
          AUS 312/7

                            Update Over info too
                       (93.0 ov)
Server                 (94.0 ov)                   Client

          AUS 316/7

                       (95.0 ov)

         IND 0/0
                       (0.0 ov)

                             Not Interested. Bye
The Background
Let’s Meet Next Year (2014)
                   http://www.tavendo.de/webmq/resources/faq
Server Side        Client Side
 • Hand Shake       • Java Script API
 • Data Transfer    • Browser Behavior

   RFC 6455


                     http://www.tavendo.de/webmq/resources/faq
Fundamentals
•   tcp        + http

•   Full Duplex (Bi-Directional)
•   Native Browser Support
•   No Hacks
•   Not Limited to Web
The Spec
• 80 / 443 (plain/secure)
  • No Proxy/Firewall pains
• Stream of Messages (Not Bytes)
• ws:// & wss://
• Only ONE Connection (up/down streams)
Still Why?
2 Bytes Header            1000 x
     (for each message)




~ 50 ms (Latency)          3x
1 Message/Second per Client




  1,000 clients   10,000       clients      1,00,000         clients


                  http://www.codeproject.com/Articles/437342/DotNet-WebSocket-Programming
The Upgrade
• GET    (request)



• 101        (response code)

  • Not 200 OK
• http:// -> ws://
Client Request
GET /demo HTTP/1.1
Origin                 : http://sivaa.in
Host                   : sivaa.in

Connection             : Upgrade
Upgrade                : WebSocket
WebSocket-Protocol     : json, chat, <protocol>
WebSocket-Version      : 10
WebSocket-extensions   : compression, <extn>
Server Response
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
Challenge - 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)
Client / Server


Peer <-> Peer
Data Transfer
• Frames (TCP)
•




• No request / response behavior
    •   No Correlation too
    •   Independent
    •   # of requests   !=   # of responses
And No
    • Headers
    • Cookies
    • Authentication
Sub Protocols
• Custom Message Patterns
• Technical / Business
• Request / Response
 •   1–1
 •   1 to Many
 •   Acknowledgements
Ping / Pong
•   Keep-Alive          Client -> Ping
•   Heart Beat          Server -> Pong
•   Latency Metrics
•   N/W Status Probing
•   Detect Failed Connections
Browser Support




       http://caniuse.com/#feat=websockets
IDL (Java Script)

Server Support



              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/
Developer Friendly
1.     var ws = new WebSocket("ws://sivaa.in/demo");
                                                       CONNECTING
2.     ws.onopen = function() {
           ws.send("Hello Server!");
                                     OPEN
       };

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()   CLOSING

        CLOSED
The Guards
             (Proxy / Firewall)



• Closing Long Lived Connections
  • Unresponsive Server
• Buffering Server Response
  • Non Encryption
  • More Latency
Web Sockets and
                 The Guards
• Web Sockets doesn’t Proxy
• Stripping HTTP Headers
    •   Upgrade
    •   Options to disable
•   Rejecting Server Response
    •   Frames (HTTP Headers Expected)
•   wss:// - No issues
The Hidden Blades
• 301
• Limited Support
• Inconsistent Behavior
Java EE 7 & Web Socket
• JSR 356
• Java EE 7
• Tyrus ( http://tyrus.java.net/ )


  • Web Socket SDK
  • Reference Implementation
Server Side Capabilities
             (Expected)

 •   Handshake Response to Clients
 •   Source/Origin Check
 •   URI Resolving & Matching
 •   Sub Protocols Negotiation
 •   Extension Negotiation
Handlers
•   End point (to handle life cycle events)
•   Session (Current Active Session)
•   Remote End Point (Peer)
•   Message Handler (incoming messages)
•   Error Handler
Packages
• javax.websocket.*
• javax.net.websocket.*

http://java.net/projects/websocket-spec/sources/source-code-
repository/show/tags/javax.net.websocket-api-1.0-b08/src/main/java?rev=213
Web Server Support
•   Apache Tomcat 7
•   GlassFish 3.1
•   Jetty 7
•   JBoss 7
•   And much more
GlassFish
•   Annotations
•   Reference Implementation
•   Web Sockets SDK / mvn
•   Samples to Begin with
Demo
• GlassFish Promoted Build
•   # svn checkout https://svn.java.net/svn/websocket-sdk~source-code-repository
•   # mvn clean install (in websocket-sdk~source-code-repositorytrunksamples)

• Import in Latest Net Beans
• Run it
Thank You!
            siva@sivaa.in
bit.ly/sivaa_in      bit.ly/sivasubramaniam
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

Web Sockets in Java EE 7

  • 1.
    Java Day Web Sockets in Java EE 7 February 23, 2013 Sivasubramaniam Arunachalam @sivaa_in http://www.meetup.com/BangaloreOpenJUG/events/102904102/
  • 2.
    It’s me! • ApplicationDeveloper • Web/Enterprise/Middleware/B2B • Java/Java EE, Python/Django • 2002 • Technical Consultant • Process Mentor • Speaker
  • 3.
    Agenda • Introduction toWeb Sockets • History • Protocol Spec • Adoption • Java EE 7 Support • Demo
  • 4.
  • 5.
  • 6.
    600+ M Active Users/Day https://www.facebook.com/sitetour/chat.php
  • 7.
    1991 http:// 1995 Still 22 Years Old 2013 http?
  • 8.
    http/tcp (1991) (1974) request / response State less ~ 2k bytes ~ 150 ms
  • 9.
    ~ Real Time
  • 10.
    Pull Based (Polling) Periodic Polling (JS/AJAX) Latest Score? AUS 312/7 Latest Score? Same Latest Score? Same Server Client Latest Score? AUS 316/7 Latest Score? Same
  • 11.
    Push Based (Comet) Server Push/Long Polling (JS/AJAX/iframe) Latest Score? AUS 312/7 Latest Score? Server Client AUS 316/7 Latest Score?
  • 12.
    • Request /Response • Too Many • Server Connections • Heavy Headers • Outdated Content
  • 13.
    Streaming Server SideHack Latest Score? AUS 312/7 (1/n) Server Client AUS 316/7 (2/n) IND 0/0 (n/n)
  • 14.
    Latest Score? (1/n) AUS 312/7 (n-1 to go) Server Server Buffer Client AUS 316/7 (2/n) AUS 312/7 (n-2 to go) AUS 316/7 IND 0/0 IND 0/0 (n/n) PROXY
  • 15.
    Others • RTMP •Adobe • Are you Still using Flash? • RTD • Windows/Excel • Non Web
  • 16.
  • 17.
    The Web SocketWay The Original Route Hey Server! Hi Client! Update me the Score AUS 312/7 Server Client AUS 316/7 IND 0/0 Not Interested. Bye
  • 18.
    Hey Server! Hi Client! Update me the Score AUS 312/7 Update Over info too (93.0 ov) Server (94.0 ov) Client AUS 316/7 (95.0 ov) IND 0/0 (0.0 ov) Not Interested. Bye
  • 19.
  • 20.
    Let’s Meet NextYear (2014) http://www.tavendo.de/webmq/resources/faq
  • 21.
    Server Side Client Side • Hand Shake • Java Script API • Data Transfer • Browser Behavior RFC 6455 http://www.tavendo.de/webmq/resources/faq
  • 22.
    Fundamentals • tcp + http • Full Duplex (Bi-Directional) • Native Browser Support • No Hacks • Not Limited to Web
  • 23.
    The Spec • 80/ 443 (plain/secure) • No Proxy/Firewall pains • Stream of Messages (Not Bytes) • ws:// & wss:// • Only ONE Connection (up/down streams)
  • 24.
    Still Why? 2 BytesHeader 1000 x (for each message) ~ 50 ms (Latency) 3x
  • 25.
    1 Message/Second perClient 1,000 clients 10,000 clients 1,00,000 clients http://www.codeproject.com/Articles/437342/DotNet-WebSocket-Programming
  • 26.
    The Upgrade • GET (request) • 101 (response code) • Not 200 OK • http:// -> ws://
  • 27.
    Client Request GET /demoHTTP/1.1 Origin : http://sivaa.in Host : sivaa.in Connection : Upgrade Upgrade : WebSocket WebSocket-Protocol : json, chat, <protocol> WebSocket-Version : 10 WebSocket-extensions : compression, <extn>
  • 28.
    Server Response HTTP/1.1 101Switching Protocols Connection : Upgrade Upgrade : WebSocket WebSocket-Protocol : json, chat, <protocol> WebSocket-Origin : http://sivaa.in WebSocket-Location : ws://sivaa.in/demo
  • 29.
    Challenge - Response Challengeby 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)
  • 30.
  • 31.
    Data Transfer • Frames(TCP) • • No request / response behavior • No Correlation too • Independent • # of requests != # of responses
  • 32.
    And No • Headers • Cookies • Authentication
  • 33.
    Sub Protocols • CustomMessage Patterns • Technical / Business • Request / Response • 1–1 • 1 to Many • Acknowledgements
  • 34.
    Ping / Pong • Keep-Alive Client -> Ping • Heart Beat Server -> Pong • Latency Metrics • N/W Status Probing • Detect Failed Connections
  • 35.
    Browser Support http://caniuse.com/#feat=websockets
  • 36.
    IDL (Java Script) ServerSupport http://www.w3.org/TR/websockets/
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
    Developer Friendly 1. var ws = new WebSocket("ws://sivaa.in/demo"); CONNECTING 2. ws.onopen = function() { ws.send("Hello Server!"); OPEN }; 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() CLOSING CLOSED
  • 42.
    The Guards (Proxy / Firewall) • Closing Long Lived Connections • Unresponsive Server • Buffering Server Response • Non Encryption • More Latency
  • 43.
    Web Sockets and The Guards • Web Sockets doesn’t Proxy • Stripping HTTP Headers • Upgrade • Options to disable • Rejecting Server Response • Frames (HTTP Headers Expected) • wss:// - No issues
  • 44.
    The Hidden Blades •301 • Limited Support • Inconsistent Behavior
  • 45.
    Java EE 7& Web Socket • JSR 356 • Java EE 7 • Tyrus ( http://tyrus.java.net/ ) • Web Socket SDK • Reference Implementation
  • 46.
    Server Side Capabilities (Expected) • Handshake Response to Clients • Source/Origin Check • URI Resolving & Matching • Sub Protocols Negotiation • Extension Negotiation
  • 47.
    Handlers • End point (to handle life cycle events) • Session (Current Active Session) • Remote End Point (Peer) • Message Handler (incoming messages) • Error Handler
  • 48.
  • 49.
    Web Server Support • Apache Tomcat 7 • GlassFish 3.1 • Jetty 7 • JBoss 7 • And much more
  • 50.
    GlassFish • Annotations • Reference Implementation • Web Sockets SDK / mvn • Samples to Begin with
  • 51.
    Demo • GlassFish PromotedBuild • # svn checkout https://svn.java.net/svn/websocket-sdk~source-code-repository • # mvn clean install (in websocket-sdk~source-code-repositorytrunksamples) • Import in Latest Net Beans • Run it
  • 52.
    Thank You! siva@sivaa.in bit.ly/sivaa_in bit.ly/sivasubramaniam
  • 53.
    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