Building Websocket Applications with GlassFish and Grizzly


Published on

Learn what websockets are and how you can build websocket based applications using the GlassFish application server or embed them in your own applications using Grizzly.

Published in: Technology
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Building Websocket Applications with GlassFish and Grizzly

  1. 1. <ul>Building WebSocket Applications with GlassFish and Grizzly </ul><ul><li>Justin Lee – Principal Member of Technical Staff at Oracle </li></ul>
  2. 2. The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. The statements and opinions expressed here are my own and do not necessarily represent those of Oracle Corporation.
  3. 3. What are WebSockets? From the abstract: The WebSocket protocol enables two-way communication between a user agent running untrusted code running in a controlled environment to a remote host that has opted-in to communications from that code. The security model used for this is the Origin-based security model commonly used by Web browsers. The protocol consists of an initial handshake followed by basic message framing, layered over TCP. 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 (e.g. using XMLHttpRequest or <iframe>s and long polling).
  4. 4. What are they? <ul><li>Look a lot like HTTP but really aren't </li><ul><li>HTTP-like handshake negotiation </li></ul><li>Available via clear or SSL transports
  5. 5. Bidirectional, asynchronous communication thereafter
  6. 6. Introduce a formal, standard object through which client code can talk to the server </li><ul><li>WebSocket vs XHR </li></ul><li>Basically a TCP connection tunneled through an HTTP server </li><ul><li>potentially
  7. 7. event based </li></ul><li>Subprotocols
  8. 8. Extensions
  9. 9. The specification is still under development </li></ul>
  10. 10. What are they not? <ul><li>An AJAX replacement </li><ul><li>Not exactly
  11. 11. AJAX callbacks need to be emulated
  12. 12. 'poll' : function() { new Ajax.Request('long_polling', { method : 'GET', onSuccess : counter.update }); } </li></ul></ul>
  13. 13. What are they not? <ul><li>Comet replacement </li><ul><li>Not directly
  14. 14. Websockets are a transport technology
  15. 15. Comet is an application strategy </li></ul><li>Again with the callbacks
  16. 16. Better performance </li><ul><li>Ericsson Labs
  17. 17. </li></ul></ul>
  18. 18. Client Handshake GET /chat HTTP/1.1 Host: Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== Sec-WebSocket-Origin: Sec-WebSocket-Protocol: chat, superchat Sec-WebSocket-Version: 6
  19. 19. Server Handshake HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= Sec-WebSocket-Protocol: chat
  20. 20. WebSocket Object [Constructor(in DOMString url, optional in DOMString protocol)] interface WebSocket { readonly attribute DOMString URL; // ready state const unsigned short CONNECTING = 0; const unsigned short OPEN = 1; const unsigned short CLOSED = 2; readonly attribute unsigned short readyState; readonly attribute unsigned long bufferedAmount; // networking attribute Function onopen; attribute Function onmessage; attribute Function onclose; boolean send(in DOMString data); void close(); };
  21. 21. WebSocket Client var ws = new WebSocket(&quot;ws://;); ws.onopen = function(evt) { alert(&quot;Connection open ...&quot;); }; ws.onmessage = function(evt) { alert( &quot;Received Message: &quot; +; }; ws.onclose = function(evt) { alert(&quot;Connection closed.&quot;); }; ws.send(&quot;Hello Web Socket!&quot;); ws.close();
  22. 22. WebSocketEngine.getEngine().register(new <application class> ()); SelectorThread st = new SelectorThread(); st.setPort(port); st.setAdapter(new StaticResourcesAdapter(&quot;target/ws-war&quot;)); st.setAsyncHandler(new DefaultAsyncHandler()); st.setEnableAsyncExecution(true); st.getAsyncHandler().addAsyncFilter(new WebSocketAsyncFilter()); st.listen(); Configuring Grizzly 1.9
  23. 23. HttpServer httpServer = HttpServer.createSimpleServer(&quot;./&quot;, PORT); final Collection<NetworkListener> listeners = httpServer.getListeners(); for (NetworkListener listener : listeners) { listener.registerAddOn(new WebSocketAddOn()); } httpServer.start(); Configuring Grizzly 2.0
  24. 24. Configuring GlassFish <ul><li>New entry on <http>
  25. 25. http.websockets- support- enabled=true
  26. 26. asadmin set </li></ul>
  27. 27. Comments <ul><li>WebSockets filter detects a websocket request and upgrades accordingly
  28. 28. Ignores non websocket related requests
  29. 29. Normal traffic unaffected
  30. 30. One last step: </li></ul><ul><ul><li>WebSocketEngine.getEngine().register(<url path>, <application>); </li></ul></ul>
  31. 31. How can you use it? <ul><li>Maven artifact </li></ul><ul><ul><li>
  32. 32. </li></ul></ul><ul><ul><li>OSGi Bundles </li></ul><li>GlassFish Server 3.1+
  33. 33. Grizzly Standalone </li><ul><li>Client code
  34. 34. 1.9
  35. 35. 2.0: </li><ul><li>Anonymous: git://
  36. 36. Developers: ssh://<userid> </li></ul></ul><li> </li></ul>
  37. 37. <ul><Insert Picture Here> </ul><ul><li>The Code </li></ul>
  38. 38. <ul><Insert Picture Here> </ul><ul><li>The Demo </li></ul>
  39. 39. What's Next? <ul><li>EE Alignment </li></ul><ul><ul><li>Servlet bridge </li><ul><li>not quite integration
  40. 40. Santiago's work </li></ul></ul></ul><ul><ul><li>Annotation support </li><ul><li>Deployment
  41. 41. Injection
  42. 42. LifeCycle ? </li></ul></ul><li>No word on inclusion in EE </li><ul><li>New, evolving tech
  43. 43. Adoption remains to be proven </li></ul></ul>
  44. 44. <ul>Building WebSocket Applications with GlassFish and Grizzly </ul><ul><li>Justin Lee – Principal Member of Technical Staff at Oracle </li></ul>
  45. 45. <ul>Building WebSocket Applications with GlassFish and Grizzly </ul><ul>Justin Lee <li> </li></ul>