SlideShare a Scribd company logo
Introduction to WebSockets




Tuesday, February 19, 13          1
About

      Gunnar Hillert

     • Company: SpringSource, a division of VMware
     • Projects:
           – Spring Integration (http://www.springintegration.org)
           – Spring AMQP
           – Cloud Foundry (Maven Plugin)
     • Twitter: @ghillert
     • LinkedIn: http://www.linkedin.com/in/hillert
     • Blog
           – http://blog.springsource.org/author/ghillert/
           – http://blog.hillert.com

                                                                     2

Tuesday, February 19, 13                                                 2
Objectives


     •    Survey the lay of the land
     •    Less focus on syntax and mechanics
     •    Broad, pragmatic perspective
     •    Special emphasis on Java




                                               3

Tuesday, February 19, 13                           3
Where to find the slides + samples?



    • Slides: slideshare.net/hillert/devnexus-2013-introduction-to-websockets
    • Samples: https://github.com/cbeams/bitcoin-rt

    • Previous Recordings:
       • Spring One: http://www.infoq.com/presentations/
         Introduction-WebSocket
       • AJUG Dec 2012 - http://vimeo.com/57019021




                                                                                4

Tuesday, February 19, 13                                                            4
WebSocket 101




                                           5

Tuesday, February 19, 13                       5
The Problem




     • Some web apps need two-way communication / rapid
       updates
     • AJAX and Comet techniques can amount to an “abuse of
       HTTP”




                                                              6

Tuesday, February 19, 13                                          6
The Problem




     • Too many connections
     • Too much overhead
     • Too great a burden on the client




                                          7

Tuesday, February 19, 13                      7
The Usual Suspects



     •    Trading
     •    Chat
     •    Gaming
     •    Collaboration
     •    Visualization




                           8

Tuesday, February 19, 13       8
The Goal



                        “The goal of this technology is to provide a
                      mechanism for browser-based applications that
                      need two-way communication with servers that
                         does not rely on opening multiple HTTP
                                       connections”

                           - RFC 6455, The WebSocket Protocol




                                                                       9

Tuesday, February 19, 13                                                   9
The Approach




     •    Two-way messaging over a single connection
     •    Layer on TCP
     •    Not HTTP, but uses HTTP to bootstrap
     •    Extremely low-overhead




                                                       10

Tuesday, February 19, 13                                    10
The WebSocket HTTP Handshake


                           GET /chat HTTP/1.1
                           Host: server.example.com
                           Upgrade: websocket
                           Connection: Upgrade

                           HTTP/1.1 101 Switching
                           Protocols
                           Upgrade: websocket
                           Connection: Upgrade



                                                      11

Tuesday, February 19, 13                                   11
What’s in a Frame?

        0                   1                    2                    3
        0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
      +-+-+-+-+-------+-+-------------+-------------------------------+
      |F|R|R|R| opcode|M| Payload len |      Extended payload length    |
      |I|S|S|S| (4) |A|         (7)     |             (16/64)           |
      |N|V|V|V|        |S|              |   (if payload len==126/127)   |
      | |1|2|3|        |K|              |                               |
      +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
      |      Extended payload length continued, if payload len == 127 |
      + - - - - - - - - - - - - - - - +-------------------------------+
      |                                 |Masking-key, if MASK set to 1 |
      +-------------------------------+-------------------------------+
      | Masking-key (continued)         |          Payload Data         |
      +-------------------------------- - - - - - - - - - - - - - - - +
      :                       Payload Data continued ...                :
      + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
      |                       Payload Data continued ...                |
      +---------------------------------------------------------------+
                           http://www.ietf.org/rfc/rfc6455.txt

                                                                     12

Tuesday, February 19, 13                                                    12
bitcoin-rt


     • visualize Bitcoin transactions in real time
     • inspired by original bitcoinmonitor.com




                                                     13

Tuesday, February 19, 13                                  13
14

Tuesday, February 19, 13        14
bitcoin-rt vs bitcoinmonitor




     • WebSockets instead of Long Polling
     • d3.js (http://d3js.org/) instead of JQuery UI




                                                       15

Tuesday, February 19, 13                                    15
16

Tuesday, February 19, 13        16
bitcoin-rt implementations


     •    Node.js – http://nodejs.org/
     •    Node.js + SockJS – http://sockjs.org
     •    Java + Tomcat native WebSocket API
     •    Java + Atmosphere – https://github.com/Atmosphere
     •    Java + Vert.x – http://vertx.io/
     •    Java + Spring Integration (TCP Module)




                                                              17

Tuesday, February 19, 13                                           17
https://github.com/cbeams/bitcoin-rt


                                                                  18

Tuesday, February 19, 13                                               18
bitcoin-rt

                           Node.js demo



                                          19

Tuesday, February 19, 13                       19
WebSocket benefits



     • more resource-efficient
     • lower-latency data
     • conceptually simpler




                                 20

Tuesday, February 19, 13              20
If WebSocket is so great...


     • Why does bitcoinmonitor use long polling?
     • What about other sites?
           – Asana.com
           – Meteor (http://www.meteor.com)

         self.socket = new SockJS(self.url, undefined, {
               debug: false, protocols_whitelist: [
                 // only allow polling protocols. no websockets or streaming.
                 // streaming makes safari spin, and websockets hurt chrome.
                 'xdr-polling', 'xhr-polling', 'iframe-xhr-polling', 'jsonp-polling'
               ]});




                   github.com/meteor/meteor/blob/master/packages/stream/stream_client.js


                                                                                           21

Tuesday, February 19, 13                                                                        21
Browser Support




                           http://caniuse.com/#feat=websockets (Feb 17, 2013)
                                                                                22

Tuesday, February 19, 13                                                             22
Browser Share World-Wide




                           http://gs.statcounter.com/

                                                        23

Tuesday, February 19, 13                                     23
Browser Share China




                           http://gs.statcounter.com/

                                                        24

Tuesday, February 19, 13                                     24
Browser Versions World-Wide




                           http://gs.statcounter.com/

                                                        25

Tuesday, February 19, 13                                     25
HTTP Proxies



     •    Content caching, internet connectivity, filtering
     •    Can monitor or close connections, buffer unencrypted traffic
     •    Designed for HTTP-based document transfer
     •    Not for long-lived connections




                                                                    26

Tuesday, February 19, 13                                                 26
Proxy Traversal




                      “Today, most transparent proxy servers will not
                       yet be familiar with the Web Socket protocol
                         and these proxy servers will be unable to
                            support the Web Socket protocol”

                                 Peter Lubbers, in a 2010 InfoQ article




                                                                          27

Tuesday, February 19, 13                                                       27
Proxy Issues



     •    Explicit proxies with HTTP Connect
     •    Transparent proxies propagation of Upgrade header
     •    Retaining the Connection header
     •    WebSocket frames vs HTTP traffic




                                                              28

Tuesday, February 19, 13                                           28
A Few Rules of Thumb



     •    “wss:” provides a much better chance of success
     •    Same for browsers using explicit proxies
     •    Transparent proxies can support WebSocket
     •    But must be configured explicitly




                                                            29

Tuesday, February 19, 13                                         29
Keeping Connections Alive



     •    Internet inherently unreliable
     •    Both server and client can go away
     •    Wireless connection may fade out
     •    and so on




                                               30

Tuesday, February 19, 13                            30
A New Set of Challenges



     •    Keep-alive ("ping!")
     •    Heartbeat ("I'm still here!")
     •    Message delivery guarantee
     •    Buffering




                                          31

Tuesday, February 19, 13                       31
How Did We Get Here?




                                                  32

Tuesday, February 19, 13                               32
Some History




     • 1996 - Java Applets/Netscape 2.0
     • 1999/2000 - XMLHttpRequest (XHR)
     • 2003 - Macromedia/Adobe Flash (RTMP Protocol)




                                                       33

Tuesday, February 19, 13                                    33
Comet




     • March 2006 - Comet - Alex Russell
     • event-driven, server-push data streaming
     • e.g. in GMail's GTalk interface




                                                  34

Tuesday, February 19, 13                               34
Comet




     • XHR long-polling / XHR multipart-replace / XHR Streaming
     • htmlfile ActiveX Object
     • Server-sent events (SSE) - Part of HTML5/W3C
       (EventSource)
           – http://www.html5rocks.com/en/tutorials/eventsource/basics/




                                                                          35

Tuesday, February 19, 13                                                       35
Path to WebSockets




     • 2007 - TCPConnection API and protocol (Ian Hickson)
     • WebSocket - First public draft January 2008




                                                             36

Tuesday, February 19, 13                                          36
IETF Standardization




                                                  37

Tuesday, February 19, 13                               37
Network Working Group




    • 2009-Jan - hixie-00
    • 2010-Feb - hixie-75 - Chrome 4
    • 2010-May - hixie-76 - Disabled in FF/Opera




                                                   38

Tuesday, February 19, 13                                38
HyBi Working Group




    • 2010-May - hybi-00 - Same as hixie-76
    • 2011-April - hybi-07 - Firefox 6
    • 2011-Dec - RFC6455




                                              39

Tuesday, February 19, 13                           39
RFC 6455
                           The WebSocket Protocol

                                  Final Version: Dec 2011
                              http://tools.ietf.org/html/rfc6455



                                                                   40

Tuesday, February 19, 13                                                40
WebSocket Protocol Details




    •    TCP-based protocol
    •    HTTP used solely for upgrade request (Status Code 101)
    •    Bi-directional, full-duplex
    •    Data Frames can be Text (UTF-8) or arbitrary Binary data




                                                                41

Tuesday, February 19, 13                                             41
WebSocket Schemes



    • Unencrypted: ws://
    • Encrypted: wss://



    • Use encrypted scheme




                             42

Tuesday, February 19, 13          42
WebSocket Handshake




    •    Request: Sec-WebSocket-Key Header
    •    Response - 258EAFA5-E914-47DA-95CA-C5AB0DC85B11
    •    Appended to key + SHA-1 + base64
    •    Sec-WebSocket-Accept Header




                                                      43

Tuesday, February 19, 13                                   43
WebSocket Protocol Details

        0                   1                    2                    3
        0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
      +-+-+-+-+-------+-+-------------+-------------------------------+
      |F|R|R|R| opcode|M| Payload len |      Extended payload length    |
      |I|S|S|S| (4) |A|         (7)     |             (16/64)           |
      |N|V|V|V|        |S|              |   (if payload len==126/127)   |
      | |1|2|3|        |K|              |                               |
      +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
      |      Extended payload length continued, if payload len == 127 |
      + - - - - - - - - - - - - - - - +-------------------------------+
      |                                 |Masking-key, if MASK set to 1 |
      +-------------------------------+-------------------------------+
      | Masking-key (continued)         |          Payload Data         |
      +-------------------------------- - - - - - - - - - - - - - - - +
      :                       Payload Data continued ...                :
      + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
      |                       Payload Data continued ...                |
      +---------------------------------------------------------------+
                           http://www.ietf.org/rfc/rfc6455.txt

                                                                     44

Tuesday, February 19, 13                                                    44
WebSocket Control Frames



    •    Communicate state about the WebSocket
    •    Close (0x8)
    •    Ping (0x9)
    •    Pong (0xA)
    •    More possible in future
    •    125 bytes or less




                                                 45

Tuesday, February 19, 13                              45
WebSocket Extensions



    • WebSocket Per-frame Compression (Draft)
    • Multiplexing Extension (Draft)
    • Extensions Header: Sec-WebSocket-Extensions



    • Used in the opening handshake (HTTP)




                                                    46

Tuesday, February 19, 13                                 46
Multiplexing Extension (MUX)



    • http://tools.ietf.org/html/draft-ietf-hybi-websocket-
      multiplexing-08
    • Separate logical connections over underlying transport
      connection




                                                               47

Tuesday, February 19, 13                                            47
Sub-Protocols


    • Sub-Protocol Header: Sec-WebSocket-Protocol

    • IANA Registry:

         http://www.iana.org/assignments/websocket/websocket.xml

           – STOMP
           – WAMP
           – soap (WTF?)




                                                              48

Tuesday, February 19, 13                                           48
HTML5 WebSockets =
                           W3C API + IETF Protocol



                                                     49

Tuesday, February 19, 13                                  49
The WebSocket API



    • W3C Candidate Recommendation 20 Sep 2012
    • http://www.w3.org/TR/websockets/
    • Browser client-side API




                                                 50

Tuesday, February 19, 13                              50
The WebSocket API



    • Binary data supported: Blob or ArrayBuffer format
    • Can inspect extensions (read-only)
    • No support for ping/pong frames




                                                          51

Tuesday, February 19, 13                                       51
The readyState attribute



    • CONNECTING (0) - Connection not yet established
    • OPEN (1) - Connection is established + communication
      possible
    • CLOSING (2) - Connection going through closing
      handshake / close() method called
    • CLOSED (3) - Connection is closed / could not be opened




                                                                52

Tuesday, February 19, 13                                             52
Event Handlers



    •    onopen
    •    onmessage
    •    onerror
    •    onclose




                           53

Tuesday, February 19, 13        53
Code Sample


     var socket = new WebSocket(
       'ws://localhost:8080/bitcoin-java-
     servlet/tomcat');
     ...
     socket.onmessage = function(event) {
            console.log(event.data);
            var trade = JSON.parse(event.data);
            ...
         };
     ...

                                            54

Tuesday, February 19, 13                          54
Non-Java Solutions




                                                55

Tuesday, February 19, 13                             55
Non-Java Solutions



    • Node.js websocket package
           – https://npmjs.org/package/websocket

    • Socket.IO (Engine.IO)
           – http://socket.io

    • SockJS
           – http://sockjs.org




                                                   56

Tuesday, February 19, 13                                56
More Than Just WebSockets



    •    XHR streaming
    •    XHR long polling
    •    Hidden iframe
    •    Flash socket
    •    Polling




                                 57

Tuesday, February 19, 13              57
Socket.IO vs SockJS


    • Socket.IO more popular, SockJS gaining ground
    • SockJS focused on transports, horizontal scalability
    • Discussion thread




                                                             58

Tuesday, February 19, 13                                          58
Where We Are In Java Land




                                              59

Tuesday, February 19, 13                           59
Tomcat



    •    WebSocketServlet
    •    Since 7.0.27 (03/2012)
    •    Backport to 6.0.35 Issue 52918
    •    Fairly minimal, server-side only



    • http://tomcat.apache.org/tomcat-7.0-doc/web-socket-
      howto.html



                                                            60

Tuesday, February 19, 13                                         60
Jetty




    • Since Jetty 7.x (early adoption, complex)
    • Revised in Jetty 9
           – http://webtide.intalio.com/2012/10/jetty-9-updated-websocket-api/
    • Builds on Java 7, messages not frames, annotations

         http://download.eclipse.org/jetty/stable-7/apidocs/org/
         eclipse/jetty/websocket/package-summary.html



                                                                            61

Tuesday, February 19, 13                                                         61
Glassfish



    •    Since 3.1 (02/2011)
    •    Exposes frames, server-side only
    •    Like with earlier Jetty versions, a major revision is likely
    •    http://antwerkz.com/glassfish-web-sockets-sample/

    • Glassfish 4 integrates Tyrus (http://tyrus.java.net/)
           – Tyrus = JSR-356 Reference Implementation for Java API for
             WebSocket




                                                                         62

Tuesday, February 19, 13                                                      62
Java API for WebSocket (JSR-356)



    •    Original discussion started in JSR-340 (Servlet 3.1)
    •    Later split out into separate spec
    •    Servlet spec will have an upgrade option
    •    JSR-356 will not require Servlet API




                                                                63

Tuesday, February 19, 13                                             63
What's under discussion



    •    Client and server-side API
    •    Use of annotations (or use API directly)
    •    Support for extensions
    •    Security considerations
    •    Thread model




                                                    64

Tuesday, February 19, 13                                 64
Implementation

    • Declare Endpoint

                  @WebSocketEndpoint(value="/websocket",
                  configuration=DefaultServerConfiguration.class)
                  public class BitCoinEndpoint { ... }


    • LifeCycle
           – @WebSocketOpen, @WebSocketClose
    • Handling Messages
           – @WebSocketMessage
    • Handling Errors
           – @WebSocketError



                                                                    65

Tuesday, February 19, 13                                                 65
Resources



    • All drafts so far
      http://java.net/projects/websocket-spec/downloads/
      directory/Spec%20javadoc%20Drafts
    • JSR-000356 Java API for WebSocket 1.0 Public Review
      http://download.oracle.com/otndocs/jcp/websocket-1_0-pr-
      spec/index.html
    • Mailing list archives
      http://java.net/projects/websocket-spec/lists




                                                             66

Tuesday, February 19, 13                                          66
Client Side



    • AsyncHttpClient
      https://github.com/sonatype/async-http-client
    • Jetty
    • Netty
    • vert.x
    • Grizzly
    • JSR-356 (coming)




                                                      67

Tuesday, February 19, 13                                   67
Other Implementations


    • Atmosphere
      https://github.com/Atmosphere/atmosphere
    • jWebSocket
      http://jwebsocket.org/
    • Netty.Io
      https://netty.io/
    • vert.x
      http://vertx.io/
    • Grizzly
      http://grizzly.java.net/


                                                 68

Tuesday, February 19, 13                              68
Spring Integration WebSocket Support



    •    Atmosphere based Extension (Coming)
    •    Considering adding Client Support (SockJS)
    •    Event Bus support (Integration with Integration.js)
    •    WebSocket implementation using TCP Adapters
           – https://github.com/SpringSource/spring-integration-extensions/
             tree/master/spring-integration-ip-extensions
           – passes Autobahn Testsuite (http://autobahn.ws/testsuite)




                                                                              69

Tuesday, February 19, 13                                                           69
Spring 4.0 WebSocket Support



    • WebSocket Support (JSR-356)
    • Support for SockJS and/or Socket.IO




                                            70

Tuesday, February 19, 13                         70
Building a Non-Trivial
                                Application




                                                    71

Tuesday, February 19, 13                                 71
A Few Conclusions



    •    WebSocket technology is promising
    •    Not a silver bullet
    •    Complement to REST
    •    Potential replacement for Comet techniques
    •    But the need for fallback options will persist




                                                          72

Tuesday, February 19, 13                                       72
A Few Conclusions




    • Integrating WebSockets into a real app is not yet trivial
    • But now is the time to begin thinking about it
    • “Pure WebSocket” applications in the wild unlikely




                                                                  73

Tuesday, February 19, 13                                               73
Predictions




    • A consolidation of 'fallback protocols'
    • Leading to wide adoption in various application frameworks
    • SockJS currently the most promising effort
           – https://github.com/sockjs/sockjs-protocol




                                                              74

Tuesday, February 19, 13                                           74
Many questions remain




    • Usage patterns
    • Higher-level protocols
    • XMPP, AMQP, JMS, …




                               75

Tuesday, February 19, 13            75
Building a real app today

    • Commercial vendors have a lot to offer
    • Particularly
           – blog: http://blog.kaazing.com/
           – http://www.websocket.org/

    • Doing Mobile? Consider Push Technologies
           – Apple Push Notification Service (APNS)
           – Google Cloud Messaging for Android (GCM)
           – Consider



           – Spring Mobile provides early support:
             https://github.com/SpringSource/spring-mobile-urbanairship

                                                                          76

Tuesday, February 19, 13                                                       76
Predictions: Java


    •    JSR-356 will be important
    •    Frameworks have a big role to play
    •    Atmosphere is there today
    •    Dedicated Spring support under development




                                                      77

Tuesday, February 19, 13                                   77
Questions?




                                  Thanks!
                                  http://twitter.com/ghillert



                       http://cbeams.github.com/bitcoin-rt



                                                                78

Tuesday, February 19, 13                                             78
Ad

Recommended

PPTX
WebSocket protocol
Kensaku Komatsu
 
PDF
Introduction to WebSockets
Gunnar Hillert
 
PPTX
HTML5 WebSocket Introduction
Marcelo Jabali
 
PPTX
HTML5 Real Time and WebSocket Code Lab (SFHTML5, GTUGSF)
Peter Lubbers
 
PDF
A Node.JS bag of goodies for analyzing Web Traffic
Philip Tellis
 
KEY
Pushing the web — WebSockets
Roland M
 
PPTX
vlavrynovych - WebSockets Presentation
Volodymyr Lavrynovych
 
PPTX
V2 peter-lubbers-sf-jug-websocket
brent bucci
 
PDF
My Point of View: Michael L. Nelson Web Archiving Cooperative
Michael Nelson
 
PDF
The Case for HTTP/2 - GreeceJS - June 2016
Andy Davies
 
KEY
The HTML5 WebSocket API
David Lindkvist
 
ZIP
Websocket protocol overview
allenmeng
 
PDF
Considerations for building your private cloud folsom update 041513
OpenStack Foundation
 
PDF
HTML5 WebSockets
Harri Hämäläinen
 
PPT
A web perf dashboard up & running in 90 minutes presentation
Justin Dorfman
 
PDF
Nuts and Bolts of WebSocket Devoxx 2014
Arun Gupta
 
PPTX
Front End Optimization [Cloud Connect 2012]
Strangeloop
 
PPTX
Php push notifications
Mohammed Shurrab
 
PDF
HTML5 WebSocket for the Real-Time Web and the Internet of Things
Peter Moskovits
 
PDF
Please Don't Touch the Slow Parts V2
Federico Galassi
 
PDF
Laying out the future
Chris Mills
 
PDF
Kaazing
Alexander Ainslie
 
PPTX
Tech
ManabuYoneyama
 
PDF
Realizzare applicazioni Web con WebSocket, by Simone Bordet
Codemotion
 
PDF
WebSocket Protocol と Plack::Middleware::WebSocket
Yu Nobuoka
 
PPTX
Introduction to WebRTC
Art Matsak
 
PPTX
Implementation Lessons using WebRTC in Asterisk
Moises Silva
 
PPTX
Prismic
Ouadie LAHDIOUI
 
PPTX
Real-Time applications avec la spécification Java (JSR 356) et le protocole W...
Ouadie LAHDIOUI
 
PDF
Hacking (with) WebSockets
Sergey Shekyan
 

More Related Content

What's hot (14)

PDF
My Point of View: Michael L. Nelson Web Archiving Cooperative
Michael Nelson
 
PDF
The Case for HTTP/2 - GreeceJS - June 2016
Andy Davies
 
KEY
The HTML5 WebSocket API
David Lindkvist
 
ZIP
Websocket protocol overview
allenmeng
 
PDF
Considerations for building your private cloud folsom update 041513
OpenStack Foundation
 
PDF
HTML5 WebSockets
Harri Hämäläinen
 
PPT
A web perf dashboard up & running in 90 minutes presentation
Justin Dorfman
 
PDF
Nuts and Bolts of WebSocket Devoxx 2014
Arun Gupta
 
PPTX
Front End Optimization [Cloud Connect 2012]
Strangeloop
 
PPTX
Php push notifications
Mohammed Shurrab
 
PDF
HTML5 WebSocket for the Real-Time Web and the Internet of Things
Peter Moskovits
 
PDF
Please Don't Touch the Slow Parts V2
Federico Galassi
 
PDF
Laying out the future
Chris Mills
 
PDF
Kaazing
Alexander Ainslie
 
My Point of View: Michael L. Nelson Web Archiving Cooperative
Michael Nelson
 
The Case for HTTP/2 - GreeceJS - June 2016
Andy Davies
 
The HTML5 WebSocket API
David Lindkvist
 
Websocket protocol overview
allenmeng
 
Considerations for building your private cloud folsom update 041513
OpenStack Foundation
 
HTML5 WebSockets
Harri Hämäläinen
 
A web perf dashboard up & running in 90 minutes presentation
Justin Dorfman
 
Nuts and Bolts of WebSocket Devoxx 2014
Arun Gupta
 
Front End Optimization [Cloud Connect 2012]
Strangeloop
 
Php push notifications
Mohammed Shurrab
 
HTML5 WebSocket for the Real-Time Web and the Internet of Things
Peter Moskovits
 
Please Don't Touch the Slow Parts V2
Federico Galassi
 
Laying out the future
Chris Mills
 

Viewers also liked (7)

PPTX
Tech
ManabuYoneyama
 
PDF
Realizzare applicazioni Web con WebSocket, by Simone Bordet
Codemotion
 
PDF
WebSocket Protocol と Plack::Middleware::WebSocket
Yu Nobuoka
 
PPTX
Introduction to WebRTC
Art Matsak
 
PPTX
Implementation Lessons using WebRTC in Asterisk
Moises Silva
 
PPTX
Prismic
Ouadie LAHDIOUI
 
PPTX
Real-Time applications avec la spécification Java (JSR 356) et le protocole W...
Ouadie LAHDIOUI
 
Realizzare applicazioni Web con WebSocket, by Simone Bordet
Codemotion
 
WebSocket Protocol と Plack::Middleware::WebSocket
Yu Nobuoka
 
Introduction to WebRTC
Art Matsak
 
Implementation Lessons using WebRTC in Asterisk
Moises Silva
 
Real-Time applications avec la spécification Java (JSR 356) et le protocole W...
Ouadie LAHDIOUI
 
Ad

Similar to DevNexus 2013 - Introduction to WebSockets (20)

PDF
Hacking (with) WebSockets
Sergey Shekyan
 
PDF
Marek pubsubhuddle realtime_web
Skills Matter Talks
 
PDF
WebSockets
Rodrigo Zaccara
 
PDF
Comet with node.js and V8
amix3k
 
PPTX
WebSockets-Revolutionizing-Real-Time-Communication.pptx
YasserLina
 
PDF
Http Push
Luke Melia
 
PDF
Dan node meetup_socket_talk
Ishi von Meier
 
PDF
Server-Side Programming Primer
Ivano Malavolta
 
PPTX
Real time websites and mobile apps with SignalR
Roy Cornelissen
 
PDF
Node.js and Ruby
Michael Bleigh
 
PDF
Real-Time with Flowdock
Flowdock
 
PDF
WebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
Viktor Gamov
 
PDF
SignalR
Troy Miles
 
PPTX
Ws
Sunghan Kim
 
PDF
Real time web apps
Sepehr Rasouli
 
PDF
javanetworking
Arjun Shanka
 
PDF
IRJET- An Overview of Web Sockets: The Future of Real-Time Communication
IRJET Journal
 
PDF
110828recruit agent ws
Kensaku Komatsu
 
PPTX
Codecamp Iasi-26 nov 2011 - Html 5 WebSockets
Florin Cardasim
 
PDF
PyNet
webuploader
 
Hacking (with) WebSockets
Sergey Shekyan
 
Marek pubsubhuddle realtime_web
Skills Matter Talks
 
WebSockets
Rodrigo Zaccara
 
Comet with node.js and V8
amix3k
 
WebSockets-Revolutionizing-Real-Time-Communication.pptx
YasserLina
 
Http Push
Luke Melia
 
Dan node meetup_socket_talk
Ishi von Meier
 
Server-Side Programming Primer
Ivano Malavolta
 
Real time websites and mobile apps with SignalR
Roy Cornelissen
 
Node.js and Ruby
Michael Bleigh
 
Real-Time with Flowdock
Flowdock
 
WebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
Viktor Gamov
 
SignalR
Troy Miles
 
Real time web apps
Sepehr Rasouli
 
javanetworking
Arjun Shanka
 
IRJET- An Overview of Web Sockets: The Future of Real-Time Communication
IRJET Journal
 
110828recruit agent ws
Kensaku Komatsu
 
Codecamp Iasi-26 nov 2011 - Html 5 WebSockets
Florin Cardasim
 
Ad

More from Gunnar Hillert (15)

PDF
High Precision GPS Positioning for Spring Developers
Gunnar Hillert
 
PDF
Migrating to Angular 5 for Spring Developers
Gunnar Hillert
 
PDF
The Spring Update
Gunnar Hillert
 
PDF
Ajug - The Spring Update
Gunnar Hillert
 
PPTX
s2gx2015 who needs batch
Gunnar Hillert
 
PDF
Spring Batch Performance Tuning
Gunnar Hillert
 
PDF
Creating Modular Test-Driven SPAs with Spring and AngularJS
Gunnar Hillert
 
PDF
Modular Test-driven SPAs with Spring and AngularJS
Gunnar Hillert
 
PDF
Atlanta JUG - Integrating Spring Batch and Spring Integration
Gunnar Hillert
 
KEY
S2GX 2012 - Spring Projects Infrastructure
Gunnar Hillert
 
KEY
S2GX 2012 - What's New in Spring Integration
Gunnar Hillert
 
KEY
S2GX 2012 - Introduction to Spring Integration and Spring Batch
Gunnar Hillert
 
PPTX
Spring Projects Infrastructure
Gunnar Hillert
 
PDF
Cloud Foundry for Spring Developers
Gunnar Hillert
 
KEY
jRecruiter - The AJUG Job Posting Service
Gunnar Hillert
 
High Precision GPS Positioning for Spring Developers
Gunnar Hillert
 
Migrating to Angular 5 for Spring Developers
Gunnar Hillert
 
The Spring Update
Gunnar Hillert
 
Ajug - The Spring Update
Gunnar Hillert
 
s2gx2015 who needs batch
Gunnar Hillert
 
Spring Batch Performance Tuning
Gunnar Hillert
 
Creating Modular Test-Driven SPAs with Spring and AngularJS
Gunnar Hillert
 
Modular Test-driven SPAs with Spring and AngularJS
Gunnar Hillert
 
Atlanta JUG - Integrating Spring Batch and Spring Integration
Gunnar Hillert
 
S2GX 2012 - Spring Projects Infrastructure
Gunnar Hillert
 
S2GX 2012 - What's New in Spring Integration
Gunnar Hillert
 
S2GX 2012 - Introduction to Spring Integration and Spring Batch
Gunnar Hillert
 
Spring Projects Infrastructure
Gunnar Hillert
 
Cloud Foundry for Spring Developers
Gunnar Hillert
 
jRecruiter - The AJUG Job Posting Service
Gunnar Hillert
 

Recently uploaded (20)

PPTX
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
PDF
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
PDF
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
PPTX
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
 
PDF
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
PDF
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
 
PDF
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
DOCX
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
PDF
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
 
PDF
"Scaling in space and time with Temporal", Andriy Lupa.pdf
Fwdays
 
PDF
Python Conference Singapore - 19 Jun 2025
ninefyi
 
PPTX
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
PDF
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
 
PDF
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
PDF
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
Priyanka Aash
 
PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
PDF
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
 
PDF
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
PDF
9-1-1 Addressing: End-to-End Automation Using FME
Safe Software
 
PDF
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
 
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
 
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
 
"Scaling in space and time with Temporal", Andriy Lupa.pdf
Fwdays
 
Python Conference Singapore - 19 Jun 2025
ninefyi
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
Priyanka Aash
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
9-1-1 Addressing: End-to-End Automation Using FME
Safe Software
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 

DevNexus 2013 - Introduction to WebSockets

  • 2. About Gunnar Hillert • Company: SpringSource, a division of VMware • Projects: – Spring Integration (http://www.springintegration.org) – Spring AMQP – Cloud Foundry (Maven Plugin) • Twitter: @ghillert • LinkedIn: http://www.linkedin.com/in/hillert • Blog – http://blog.springsource.org/author/ghillert/ – http://blog.hillert.com 2 Tuesday, February 19, 13 2
  • 3. Objectives • Survey the lay of the land • Less focus on syntax and mechanics • Broad, pragmatic perspective • Special emphasis on Java 3 Tuesday, February 19, 13 3
  • 4. Where to find the slides + samples? • Slides: slideshare.net/hillert/devnexus-2013-introduction-to-websockets • Samples: https://github.com/cbeams/bitcoin-rt • Previous Recordings: • Spring One: http://www.infoq.com/presentations/ Introduction-WebSocket • AJUG Dec 2012 - http://vimeo.com/57019021 4 Tuesday, February 19, 13 4
  • 5. WebSocket 101 5 Tuesday, February 19, 13 5
  • 6. The Problem • Some web apps need two-way communication / rapid updates • AJAX and Comet techniques can amount to an “abuse of HTTP” 6 Tuesday, February 19, 13 6
  • 7. The Problem • Too many connections • Too much overhead • Too great a burden on the client 7 Tuesday, February 19, 13 7
  • 8. The Usual Suspects • Trading • Chat • Gaming • Collaboration • Visualization 8 Tuesday, February 19, 13 8
  • 9. The Goal “The goal of this technology is to provide a mechanism for browser-based applications that need two-way communication with servers that does not rely on opening multiple HTTP connections” - RFC 6455, The WebSocket Protocol 9 Tuesday, February 19, 13 9
  • 10. The Approach • Two-way messaging over a single connection • Layer on TCP • Not HTTP, but uses HTTP to bootstrap • Extremely low-overhead 10 Tuesday, February 19, 13 10
  • 11. The WebSocket HTTP Handshake GET /chat HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade 11 Tuesday, February 19, 13 11
  • 12. What’s in a Frame? 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-------+-+-------------+-------------------------------+ |F|R|R|R| opcode|M| Payload len | Extended payload length | |I|S|S|S| (4) |A| (7) | (16/64) | |N|V|V|V| |S| | (if payload len==126/127) | | |1|2|3| |K| | | +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - + | Extended payload length continued, if payload len == 127 | + - - - - - - - - - - - - - - - +-------------------------------+ | |Masking-key, if MASK set to 1 | +-------------------------------+-------------------------------+ | Masking-key (continued) | Payload Data | +-------------------------------- - - - - - - - - - - - - - - - + : Payload Data continued ... : + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Payload Data continued ... | +---------------------------------------------------------------+ http://www.ietf.org/rfc/rfc6455.txt 12 Tuesday, February 19, 13 12
  • 13. bitcoin-rt • visualize Bitcoin transactions in real time • inspired by original bitcoinmonitor.com 13 Tuesday, February 19, 13 13
  • 15. bitcoin-rt vs bitcoinmonitor • WebSockets instead of Long Polling • d3.js (http://d3js.org/) instead of JQuery UI 15 Tuesday, February 19, 13 15
  • 17. bitcoin-rt implementations • Node.js – http://nodejs.org/ • Node.js + SockJS – http://sockjs.org • Java + Tomcat native WebSocket API • Java + Atmosphere – https://github.com/Atmosphere • Java + Vert.x – http://vertx.io/ • Java + Spring Integration (TCP Module) 17 Tuesday, February 19, 13 17
  • 19. bitcoin-rt Node.js demo 19 Tuesday, February 19, 13 19
  • 20. WebSocket benefits • more resource-efficient • lower-latency data • conceptually simpler 20 Tuesday, February 19, 13 20
  • 21. If WebSocket is so great... • Why does bitcoinmonitor use long polling? • What about other sites? – Asana.com – Meteor (http://www.meteor.com) self.socket = new SockJS(self.url, undefined, {       debug: false, protocols_whitelist: [         // only allow polling protocols. no websockets or streaming.         // streaming makes safari spin, and websockets hurt chrome.         'xdr-polling', 'xhr-polling', 'iframe-xhr-polling', 'jsonp-polling'       ]}); github.com/meteor/meteor/blob/master/packages/stream/stream_client.js 21 Tuesday, February 19, 13 21
  • 22. Browser Support http://caniuse.com/#feat=websockets (Feb 17, 2013) 22 Tuesday, February 19, 13 22
  • 23. Browser Share World-Wide http://gs.statcounter.com/ 23 Tuesday, February 19, 13 23
  • 24. Browser Share China http://gs.statcounter.com/ 24 Tuesday, February 19, 13 24
  • 25. Browser Versions World-Wide http://gs.statcounter.com/ 25 Tuesday, February 19, 13 25
  • 26. HTTP Proxies • Content caching, internet connectivity, filtering • Can monitor or close connections, buffer unencrypted traffic • Designed for HTTP-based document transfer • Not for long-lived connections 26 Tuesday, February 19, 13 26
  • 27. Proxy Traversal “Today, most transparent proxy servers will not yet be familiar with the Web Socket protocol and these proxy servers will be unable to support the Web Socket protocol” Peter Lubbers, in a 2010 InfoQ article 27 Tuesday, February 19, 13 27
  • 28. Proxy Issues • Explicit proxies with HTTP Connect • Transparent proxies propagation of Upgrade header • Retaining the Connection header • WebSocket frames vs HTTP traffic 28 Tuesday, February 19, 13 28
  • 29. A Few Rules of Thumb • “wss:” provides a much better chance of success • Same for browsers using explicit proxies • Transparent proxies can support WebSocket • But must be configured explicitly 29 Tuesday, February 19, 13 29
  • 30. Keeping Connections Alive • Internet inherently unreliable • Both server and client can go away • Wireless connection may fade out • and so on 30 Tuesday, February 19, 13 30
  • 31. A New Set of Challenges • Keep-alive ("ping!") • Heartbeat ("I'm still here!") • Message delivery guarantee • Buffering 31 Tuesday, February 19, 13 31
  • 32. How Did We Get Here? 32 Tuesday, February 19, 13 32
  • 33. Some History • 1996 - Java Applets/Netscape 2.0 • 1999/2000 - XMLHttpRequest (XHR) • 2003 - Macromedia/Adobe Flash (RTMP Protocol) 33 Tuesday, February 19, 13 33
  • 34. Comet • March 2006 - Comet - Alex Russell • event-driven, server-push data streaming • e.g. in GMail's GTalk interface 34 Tuesday, February 19, 13 34
  • 35. Comet • XHR long-polling / XHR multipart-replace / XHR Streaming • htmlfile ActiveX Object • Server-sent events (SSE) - Part of HTML5/W3C (EventSource) – http://www.html5rocks.com/en/tutorials/eventsource/basics/ 35 Tuesday, February 19, 13 35
  • 36. Path to WebSockets • 2007 - TCPConnection API and protocol (Ian Hickson) • WebSocket - First public draft January 2008 36 Tuesday, February 19, 13 36
  • 37. IETF Standardization 37 Tuesday, February 19, 13 37
  • 38. Network Working Group • 2009-Jan - hixie-00 • 2010-Feb - hixie-75 - Chrome 4 • 2010-May - hixie-76 - Disabled in FF/Opera 38 Tuesday, February 19, 13 38
  • 39. HyBi Working Group • 2010-May - hybi-00 - Same as hixie-76 • 2011-April - hybi-07 - Firefox 6 • 2011-Dec - RFC6455 39 Tuesday, February 19, 13 39
  • 40. RFC 6455 The WebSocket Protocol Final Version: Dec 2011 http://tools.ietf.org/html/rfc6455 40 Tuesday, February 19, 13 40
  • 41. WebSocket Protocol Details • TCP-based protocol • HTTP used solely for upgrade request (Status Code 101) • Bi-directional, full-duplex • Data Frames can be Text (UTF-8) or arbitrary Binary data 41 Tuesday, February 19, 13 41
  • 42. WebSocket Schemes • Unencrypted: ws:// • Encrypted: wss:// • Use encrypted scheme 42 Tuesday, February 19, 13 42
  • 43. WebSocket Handshake • Request: Sec-WebSocket-Key Header • Response - 258EAFA5-E914-47DA-95CA-C5AB0DC85B11 • Appended to key + SHA-1 + base64 • Sec-WebSocket-Accept Header 43 Tuesday, February 19, 13 43
  • 44. WebSocket Protocol Details 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-------+-+-------------+-------------------------------+ |F|R|R|R| opcode|M| Payload len | Extended payload length | |I|S|S|S| (4) |A| (7) | (16/64) | |N|V|V|V| |S| | (if payload len==126/127) | | |1|2|3| |K| | | +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - + | Extended payload length continued, if payload len == 127 | + - - - - - - - - - - - - - - - +-------------------------------+ | |Masking-key, if MASK set to 1 | +-------------------------------+-------------------------------+ | Masking-key (continued) | Payload Data | +-------------------------------- - - - - - - - - - - - - - - - + : Payload Data continued ... : + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Payload Data continued ... | +---------------------------------------------------------------+ http://www.ietf.org/rfc/rfc6455.txt 44 Tuesday, February 19, 13 44
  • 45. WebSocket Control Frames • Communicate state about the WebSocket • Close (0x8) • Ping (0x9) • Pong (0xA) • More possible in future • 125 bytes or less 45 Tuesday, February 19, 13 45
  • 46. WebSocket Extensions • WebSocket Per-frame Compression (Draft) • Multiplexing Extension (Draft) • Extensions Header: Sec-WebSocket-Extensions • Used in the opening handshake (HTTP) 46 Tuesday, February 19, 13 46
  • 47. Multiplexing Extension (MUX) • http://tools.ietf.org/html/draft-ietf-hybi-websocket- multiplexing-08 • Separate logical connections over underlying transport connection 47 Tuesday, February 19, 13 47
  • 48. Sub-Protocols • Sub-Protocol Header: Sec-WebSocket-Protocol • IANA Registry: http://www.iana.org/assignments/websocket/websocket.xml – STOMP – WAMP – soap (WTF?) 48 Tuesday, February 19, 13 48
  • 49. HTML5 WebSockets = W3C API + IETF Protocol 49 Tuesday, February 19, 13 49
  • 50. The WebSocket API • W3C Candidate Recommendation 20 Sep 2012 • http://www.w3.org/TR/websockets/ • Browser client-side API 50 Tuesday, February 19, 13 50
  • 51. The WebSocket API • Binary data supported: Blob or ArrayBuffer format • Can inspect extensions (read-only) • No support for ping/pong frames 51 Tuesday, February 19, 13 51
  • 52. The readyState attribute • CONNECTING (0) - Connection not yet established • OPEN (1) - Connection is established + communication possible • CLOSING (2) - Connection going through closing handshake / close() method called • CLOSED (3) - Connection is closed / could not be opened 52 Tuesday, February 19, 13 52
  • 53. Event Handlers • onopen • onmessage • onerror • onclose 53 Tuesday, February 19, 13 53
  • 54. Code Sample var socket = new WebSocket( 'ws://localhost:8080/bitcoin-java- servlet/tomcat'); ... socket.onmessage = function(event) { console.log(event.data); var trade = JSON.parse(event.data); ... }; ... 54 Tuesday, February 19, 13 54
  • 55. Non-Java Solutions 55 Tuesday, February 19, 13 55
  • 56. Non-Java Solutions • Node.js websocket package – https://npmjs.org/package/websocket • Socket.IO (Engine.IO) – http://socket.io • SockJS – http://sockjs.org 56 Tuesday, February 19, 13 56
  • 57. More Than Just WebSockets • XHR streaming • XHR long polling • Hidden iframe • Flash socket • Polling 57 Tuesday, February 19, 13 57
  • 58. Socket.IO vs SockJS • Socket.IO more popular, SockJS gaining ground • SockJS focused on transports, horizontal scalability • Discussion thread 58 Tuesday, February 19, 13 58
  • 59. Where We Are In Java Land 59 Tuesday, February 19, 13 59
  • 60. Tomcat • WebSocketServlet • Since 7.0.27 (03/2012) • Backport to 6.0.35 Issue 52918 • Fairly minimal, server-side only • http://tomcat.apache.org/tomcat-7.0-doc/web-socket- howto.html 60 Tuesday, February 19, 13 60
  • 61. Jetty • Since Jetty 7.x (early adoption, complex) • Revised in Jetty 9 – http://webtide.intalio.com/2012/10/jetty-9-updated-websocket-api/ • Builds on Java 7, messages not frames, annotations http://download.eclipse.org/jetty/stable-7/apidocs/org/ eclipse/jetty/websocket/package-summary.html 61 Tuesday, February 19, 13 61
  • 62. Glassfish • Since 3.1 (02/2011) • Exposes frames, server-side only • Like with earlier Jetty versions, a major revision is likely • http://antwerkz.com/glassfish-web-sockets-sample/ • Glassfish 4 integrates Tyrus (http://tyrus.java.net/) – Tyrus = JSR-356 Reference Implementation for Java API for WebSocket 62 Tuesday, February 19, 13 62
  • 63. Java API for WebSocket (JSR-356) • Original discussion started in JSR-340 (Servlet 3.1) • Later split out into separate spec • Servlet spec will have an upgrade option • JSR-356 will not require Servlet API 63 Tuesday, February 19, 13 63
  • 64. What's under discussion • Client and server-side API • Use of annotations (or use API directly) • Support for extensions • Security considerations • Thread model 64 Tuesday, February 19, 13 64
  • 65. Implementation • Declare Endpoint @WebSocketEndpoint(value="/websocket", configuration=DefaultServerConfiguration.class) public class BitCoinEndpoint { ... } • LifeCycle – @WebSocketOpen, @WebSocketClose • Handling Messages – @WebSocketMessage • Handling Errors – @WebSocketError 65 Tuesday, February 19, 13 65
  • 66. Resources • All drafts so far http://java.net/projects/websocket-spec/downloads/ directory/Spec%20javadoc%20Drafts • JSR-000356 Java API for WebSocket 1.0 Public Review http://download.oracle.com/otndocs/jcp/websocket-1_0-pr- spec/index.html • Mailing list archives http://java.net/projects/websocket-spec/lists 66 Tuesday, February 19, 13 66
  • 67. Client Side • AsyncHttpClient https://github.com/sonatype/async-http-client • Jetty • Netty • vert.x • Grizzly • JSR-356 (coming) 67 Tuesday, February 19, 13 67
  • 68. Other Implementations • Atmosphere https://github.com/Atmosphere/atmosphere • jWebSocket http://jwebsocket.org/ • Netty.Io https://netty.io/ • vert.x http://vertx.io/ • Grizzly http://grizzly.java.net/ 68 Tuesday, February 19, 13 68
  • 69. Spring Integration WebSocket Support • Atmosphere based Extension (Coming) • Considering adding Client Support (SockJS) • Event Bus support (Integration with Integration.js) • WebSocket implementation using TCP Adapters – https://github.com/SpringSource/spring-integration-extensions/ tree/master/spring-integration-ip-extensions – passes Autobahn Testsuite (http://autobahn.ws/testsuite) 69 Tuesday, February 19, 13 69
  • 70. Spring 4.0 WebSocket Support • WebSocket Support (JSR-356) • Support for SockJS and/or Socket.IO 70 Tuesday, February 19, 13 70
  • 71. Building a Non-Trivial Application 71 Tuesday, February 19, 13 71
  • 72. A Few Conclusions • WebSocket technology is promising • Not a silver bullet • Complement to REST • Potential replacement for Comet techniques • But the need for fallback options will persist 72 Tuesday, February 19, 13 72
  • 73. A Few Conclusions • Integrating WebSockets into a real app is not yet trivial • But now is the time to begin thinking about it • “Pure WebSocket” applications in the wild unlikely 73 Tuesday, February 19, 13 73
  • 74. Predictions • A consolidation of 'fallback protocols' • Leading to wide adoption in various application frameworks • SockJS currently the most promising effort – https://github.com/sockjs/sockjs-protocol 74 Tuesday, February 19, 13 74
  • 75. Many questions remain • Usage patterns • Higher-level protocols • XMPP, AMQP, JMS, … 75 Tuesday, February 19, 13 75
  • 76. Building a real app today • Commercial vendors have a lot to offer • Particularly – blog: http://blog.kaazing.com/ – http://www.websocket.org/ • Doing Mobile? Consider Push Technologies – Apple Push Notification Service (APNS) – Google Cloud Messaging for Android (GCM) – Consider – Spring Mobile provides early support: https://github.com/SpringSource/spring-mobile-urbanairship 76 Tuesday, February 19, 13 76
  • 77. Predictions: Java • JSR-356 will be important • Frameworks have a big role to play • Atmosphere is there today • Dedicated Spring support under development 77 Tuesday, February 19, 13 77
  • 78. Questions? Thanks! http://twitter.com/ghillert http://cbeams.github.com/bitcoin-rt 78 Tuesday, February 19, 13 78