SlideShare una empresa de Scribd logo
1 de 22
Descargar para leer sin conexión
Web Sockets and Comet

       Dylan Schiemann (@dylans)
       SitePen, Inc.
       HTML5 Code Camp, October, 2010


       © SitePen, Inc. All Rights Reserved

Sunday, October 17, 2010
What is Comet?

                   Set of techniques for long-lived HTTP connections
                   Low-latency data transmission from the server to the
                   browser or client
                   Deliver data from server to the client at any time
                   Improve speed and scaling over Ajax
                   Develop event-driven web applications




    © SitePen, Inc. All Rights Reserved
Sunday, October 17, 2010
Normal XHR Cycle
                                                 Browser
                 Each request is a distinct
                 HTTP setup and teardown                          Application JS Code, HTML, Etc.


                 Client must initiate request                              XHR Library

                 to server
                 Low-latency applications       Server
                                                           HTTP                HTTP                 HTTP

                 require frequent polling for
                 server updates                                     Server Application Code




       © SitePen, Inc. All Rights Reserved

Sunday, October 17, 2010
Comet HTTP Request Cycle
                                                      Browser

                                                                Application JS Code, HTML, Etc.
                 Long-running HTTP
                 connection                                             Comet Library



                 Low-latency
                                             Event Delivery
                                              HTTP Tunnel
                 Server can push data to
                                                       Server
                 the client
                                                                  Server Application Code




       © SitePen, Inc. All Rights Reserved

Sunday, October 17, 2010
Comet Use
                 Google Talk and Docs
                 Meebo
                 Facebook Chat
                 Many more...




       © SitePen, Inc. All Rights Reserved

Sunday, October 17, 2010
Comet Methods

       © SitePen, Inc. All Rights Reserved

Sunday, October 17, 2010
Forever-Frame

                   Long-lived http connection is kept alive in a hidden iframe

                   A hack on HTTP 1.1 chunked encoding

                   Incremental content pushed from server to client

                   Browser incremental rendering allows processing and event
                   handling of <script> tags

                   Great performance




    © SitePen, Inc. All Rights Reserved
Sunday, October 17, 2010
Long-Polling

                   Client makes a request

                   Server doesn’t immediately return, unless it has something
                   to return

                   When request is closed by server or the poll times out, a new
                   request is immediately opened

                   XHR-based
                          Cross-browser compatible




    © SitePen, Inc. All Rights Reserved
Sunday, October 17, 2010
Callback-Polling or JSONP-Polling



                                          Long-polling, but works cross-
                                          domain
                                          Relies on JSONP technique for
                                          establishing trust
                                          <script> blocks instead of
                                          XHR




    © SitePen, Inc. All Rights Reserved
Sunday, October 17, 2010
The Future of Comet: HTML5 WebSocket



                                          HTML5 "friend"
                                          Full-duplex communication
                                          Binary data transfer
                                          A Comet transport that wouldn’t
                                          be a hack
                                          Underspecified in places
                                          Local Storage to synchronize
                                          across tabs and frames


    © SitePen, Inc. All Rights Reserved
Sunday, October 17, 2010
Web Sockets

                   WebSocket is a technology providing bi-directional, full-
                   duplex communications channels, over a single
                   Transmission Control Protocol (TCP) socket, designed to be
                   implemented in web browsers and web servers.
                   API standardized by W3C, protocol standardized by IETF
                   Support in Firefox 4, Chrome 4, Opera 10.7, and Safari 5
                   Not available within Internet Explorer (IE9?)
                   Different versions of rec. in browsers, x-domain issues
                   Simple, easy to use API; much simpler than current methods
                   of PUSH technology
                   http://dev.w3.org/html5/websockets/
    © SitePen, Inc. All Rights Reserved
Sunday, October 17, 2010
Simple WebSocket Example

          // Check for presence in browser
          if("WebSocket" in window) {

                  // Create the WebSocket
                  var ws = new WebSocket("ws://yourdomain.com/service");

                  // Open the WebSocket
                  ws.onopen = function() {

                           // Send a message to the server
                           ws.send("Ping!"); ....
                  };

                  // React when a message is received from the server
                  ws.onmessage = function (e) {
                      var receivedMessage = e.data;
                  };

                  // Close and error events
                  ws.onclose = function() {       };
                  ws.onerror = function() {       };
          }
          else {
          // The browser doesn't support WebSocket
          }


    © SitePen, Inc. All Rights Reserved
Sunday, October 17, 2010
Web Sockets and Dojo

                   Like XHR, you're going to want a toolkit...


                   DojoX (pre 1.6) now features dojox.Socket
                   Provides a simple socket connection using WebSocket, or
                   alternate communication mechanisms in legacy browsers
                   for comet-style communication
                   Allows for socket handling with standard Dojo
                   methodologies (i.e. dojo.connect to listen to events)
                   Automatic reconnects using the
                   dojox.socket.Reconnect class


    © SitePen, Inc. All Rights Reserved
Sunday, October 17, 2010
dojox.Socket Usage

          // Require the classes
          dojo.require("dojox.socket");

          // When the class is loaded...
          dojo.ready(function() {

                  // Create a Dojox socket instance
                  var socket = dojox.socket({ url: "//comet-server/comet" });

                  // Connect to events via standard dojo.connect method
                  dojo.connect(socket, "onmessage", function(event){
                      //Retrieve the message
                      var message = event.data;
                  });

                  // Alternate event listening syntax
                  socket.on(“message”, function() { /* handle message */ });

                  // Send a message
                  socket.send("Ping!");

                  // Close a socket
                  socket.close();

          });


    © SitePen, Inc. All Rights Reserved
Sunday, October 17, 2010
dojox.socket.Reconnect Usage

          // Require both Dojo classes
          dojo.require("dojox.socket");
          dojo.require("dojox.socket.Reconnect");

          // When the resources are ready...
          dojo.ready(function() {

                  // Create socket
                  var socket = dojox.socket({url:"/comet"});

                  // Make sockets reconnect automatically
                  socket = dojox.socket.Reconnect(socket);

          });




    © SitePen, Inc. All Rights Reserved
Sunday, October 17, 2010
Comet Servers

                   Historically, web servers have been written to handle burst
                   of short-lived connections
                          Comet connections are long-lived but not always transmitting
                          data
                   Many servers are written geared toward Comet or
                   specifically for Comet
                   Comet servers best when sitting alongside a traditional web
                   server




    © SitePen, Inc. All Rights Reserved
Sunday, October 17, 2010
Comet Servers

                   cometD (Java, Bayeux)
                   Tunguska (on Node.js or Narwhal, Bayeux or RestChannels)
                   Hookbox (Python, CSP)
                   DWR (Java, Bayeux and others)
                   Lightstreamer (Java, Bayeux and others)
                   Faye (JavaScript or Ruby, on Node.js or Rails)
                   APE (Python, CSP)
                   WebSync (.NET, Bayeux)
                   ...


    © SitePen, Inc. All Rights Reserved
Sunday, October 17, 2010
Publish and Subscribe with Comet

                   Communication is done through channels
                          Allows the web server to send directed messages to the Comet
                          server
                   Channels are hierarchical
                          Wildcards can be used




    © SitePen, Inc. All Rights Reserved
Sunday, October 17, 2010
Comet Security

                   Authentication can happen on the web server and define a
                   unique channel on the Comet server
                   Web server can pass authentication credentials to the comet
                   server
                          Adds overhead




    © SitePen, Inc. All Rights Reserved
Sunday, October 17, 2010
Protocols

                   Bayeux
                          PubSub, Transports
                   CSP (Comet Session Protocol)
                          More like working with a socket
                          PubSub is separated
                   REST/HTTP Channels
                   XMPP
                   Many other proprietary and open messaging protocols




    © SitePen, Inc. All Rights Reserved
Sunday, October 17, 2010
Comet Clients

                   Each project has one
                   Many toolkits (Dojo, jQuery) have one
                          Dojo 1.6 has dojox.socket
                   Server-side clients to connect normal servers to Comet
                   servers




    © SitePen, Inc. All Rights Reserved
Sunday, October 17, 2010
Hosted Hookbox

                   Hookbox: Very simple Comet server
                   http://hosted.hookbox.org/ Free Hosted Comet Service
                   http://dylan.io/hookbox.php




    © SitePen, Inc. All Rights Reserved
Sunday, October 17, 2010

Más contenido relacionado

Similar a Intro to WebSockets and Comet

Real-Time with Flowdock
Real-Time with FlowdockReal-Time with Flowdock
Real-Time with FlowdockFlowdock
 
Websocket vs SSE - Paris.js - 24/06/15
Websocket vs SSE - Paris.js - 24/06/15Websocket vs SSE - Paris.js - 24/06/15
Websocket vs SSE - Paris.js - 24/06/15streamdata.io
 
V2 peter-lubbers-sf-jug-websocket
V2 peter-lubbers-sf-jug-websocketV2 peter-lubbers-sf-jug-websocket
V2 peter-lubbers-sf-jug-websocketbrent bucci
 
Camelone-2012 HTML5 WebSocket ActiveMQ/Camel
Camelone-2012 HTML5 WebSocket ActiveMQ/CamelCamelone-2012 HTML5 WebSocket ActiveMQ/Camel
Camelone-2012 HTML5 WebSocket ActiveMQ/CamelCharles Moulliard
 
Dev con kolkata 2012 websockets
Dev con kolkata 2012   websocketsDev con kolkata 2012   websockets
Dev con kolkata 2012 websocketsSANKARSAN BOSE
 
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 WebSocketsFlorin Cardasim
 
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 socketsCodecamp Romania
 
Open End To End Js Stack
Open End To End Js StackOpen End To End Js Stack
Open End To End Js StackSkills Matter
 
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 DevelopersViktor Gamov
 
Browser APIs for data exchange: types and application
Browser APIs for data exchange: types and applicationBrowser APIs for data exchange: types and application
Browser APIs for data exchange: types and applicationPavel Klimiankou
 
Top 10 HTML5 Features for Oracle Cloud Developers
Top 10 HTML5 Features for Oracle Cloud DevelopersTop 10 HTML5 Features for Oracle Cloud Developers
Top 10 HTML5 Features for Oracle Cloud DevelopersBrian Huff
 
HTML5 Real Time and WebSocket Code Lab (SFHTML5, GTUGSF)
HTML5 Real Time and WebSocket Code Lab (SFHTML5, GTUGSF)HTML5 Real Time and WebSocket Code Lab (SFHTML5, GTUGSF)
HTML5 Real Time and WebSocket Code Lab (SFHTML5, GTUGSF)Peter Lubbers
 
Browser Internals-Same Origin Policy
Browser Internals-Same Origin PolicyBrowser Internals-Same Origin Policy
Browser Internals-Same Origin PolicyKrishna T
 
Jwebsocketmobiletechcon2010en 100912071225 Phpapp01
Jwebsocketmobiletechcon2010en 100912071225 Phpapp01Jwebsocketmobiletechcon2010en 100912071225 Phpapp01
Jwebsocketmobiletechcon2010en 100912071225 Phpapp01purans
 
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)Ericom Software
 

Similar a Intro to WebSockets and Comet (20)

Real-Time with Flowdock
Real-Time with FlowdockReal-Time with Flowdock
Real-Time with Flowdock
 
Websocket vs SSE - Paris.js - 24/06/15
Websocket vs SSE - Paris.js - 24/06/15Websocket vs SSE - Paris.js - 24/06/15
Websocket vs SSE - Paris.js - 24/06/15
 
V2 peter-lubbers-sf-jug-websocket
V2 peter-lubbers-sf-jug-websocketV2 peter-lubbers-sf-jug-websocket
V2 peter-lubbers-sf-jug-websocket
 
Camelone-2012 HTML5 WebSocket ActiveMQ/Camel
Camelone-2012 HTML5 WebSocket ActiveMQ/CamelCamelone-2012 HTML5 WebSocket ActiveMQ/Camel
Camelone-2012 HTML5 WebSocket ActiveMQ/Camel
 
Websocket
WebsocketWebsocket
Websocket
 
Dev con kolkata 2012 websockets
Dev con kolkata 2012   websocketsDev con kolkata 2012   websockets
Dev con kolkata 2012 websockets
 
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
 
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
 
Open End To End Js Stack
Open End To End Js StackOpen End To End Js Stack
Open End To End Js Stack
 
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
 
Browser Security
Browser SecurityBrowser Security
Browser Security
 
Browser APIs for data exchange: types and application
Browser APIs for data exchange: types and applicationBrowser APIs for data exchange: types and application
Browser APIs for data exchange: types and application
 
Top 10 HTML5 Features for Oracle Cloud Developers
Top 10 HTML5 Features for Oracle Cloud DevelopersTop 10 HTML5 Features for Oracle Cloud Developers
Top 10 HTML5 Features for Oracle Cloud Developers
 
HTML5 Real Time and WebSocket Code Lab (SFHTML5, GTUGSF)
HTML5 Real Time and WebSocket Code Lab (SFHTML5, GTUGSF)HTML5 Real Time and WebSocket Code Lab (SFHTML5, GTUGSF)
HTML5 Real Time and WebSocket Code Lab (SFHTML5, GTUGSF)
 
Browser Internals-Same Origin Policy
Browser Internals-Same Origin PolicyBrowser Internals-Same Origin Policy
Browser Internals-Same Origin Policy
 
Ws
WsWs
Ws
 
The HTML5 WebSocket API
The HTML5 WebSocket APIThe HTML5 WebSocket API
The HTML5 WebSocket API
 
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
 
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
 

Último

Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbuapidays
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 

Último (20)

Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 

Intro to WebSockets and Comet

  • 1. Web Sockets and Comet Dylan Schiemann (@dylans) SitePen, Inc. HTML5 Code Camp, October, 2010 © SitePen, Inc. All Rights Reserved Sunday, October 17, 2010
  • 2. What is Comet? Set of techniques for long-lived HTTP connections Low-latency data transmission from the server to the browser or client Deliver data from server to the client at any time Improve speed and scaling over Ajax Develop event-driven web applications © SitePen, Inc. All Rights Reserved Sunday, October 17, 2010
  • 3. Normal XHR Cycle Browser Each request is a distinct HTTP setup and teardown Application JS Code, HTML, Etc. Client must initiate request XHR Library to server Low-latency applications Server HTTP HTTP HTTP require frequent polling for server updates Server Application Code © SitePen, Inc. All Rights Reserved Sunday, October 17, 2010
  • 4. Comet HTTP Request Cycle Browser Application JS Code, HTML, Etc. Long-running HTTP connection Comet Library Low-latency Event Delivery HTTP Tunnel Server can push data to Server the client Server Application Code © SitePen, Inc. All Rights Reserved Sunday, October 17, 2010
  • 5. Comet Use Google Talk and Docs Meebo Facebook Chat Many more... © SitePen, Inc. All Rights Reserved Sunday, October 17, 2010
  • 6. Comet Methods © SitePen, Inc. All Rights Reserved Sunday, October 17, 2010
  • 7. Forever-Frame Long-lived http connection is kept alive in a hidden iframe A hack on HTTP 1.1 chunked encoding Incremental content pushed from server to client Browser incremental rendering allows processing and event handling of <script> tags Great performance © SitePen, Inc. All Rights Reserved Sunday, October 17, 2010
  • 8. Long-Polling Client makes a request Server doesn’t immediately return, unless it has something to return When request is closed by server or the poll times out, a new request is immediately opened XHR-based Cross-browser compatible © SitePen, Inc. All Rights Reserved Sunday, October 17, 2010
  • 9. Callback-Polling or JSONP-Polling Long-polling, but works cross- domain Relies on JSONP technique for establishing trust <script> blocks instead of XHR © SitePen, Inc. All Rights Reserved Sunday, October 17, 2010
  • 10. The Future of Comet: HTML5 WebSocket HTML5 "friend" Full-duplex communication Binary data transfer A Comet transport that wouldn’t be a hack Underspecified in places Local Storage to synchronize across tabs and frames © SitePen, Inc. All Rights Reserved Sunday, October 17, 2010
  • 11. Web Sockets WebSocket is a technology providing bi-directional, full- duplex communications channels, over a single Transmission Control Protocol (TCP) socket, designed to be implemented in web browsers and web servers. API standardized by W3C, protocol standardized by IETF Support in Firefox 4, Chrome 4, Opera 10.7, and Safari 5 Not available within Internet Explorer (IE9?) Different versions of rec. in browsers, x-domain issues Simple, easy to use API; much simpler than current methods of PUSH technology http://dev.w3.org/html5/websockets/ © SitePen, Inc. All Rights Reserved Sunday, October 17, 2010
  • 12. Simple WebSocket Example // Check for presence in browser if("WebSocket" in window) { // Create the WebSocket var ws = new WebSocket("ws://yourdomain.com/service"); // Open the WebSocket ws.onopen = function() { // Send a message to the server ws.send("Ping!"); .... }; // React when a message is received from the server ws.onmessage = function (e) { var receivedMessage = e.data; }; // Close and error events ws.onclose = function() { }; ws.onerror = function() { }; } else { // The browser doesn't support WebSocket } © SitePen, Inc. All Rights Reserved Sunday, October 17, 2010
  • 13. Web Sockets and Dojo Like XHR, you're going to want a toolkit... DojoX (pre 1.6) now features dojox.Socket Provides a simple socket connection using WebSocket, or alternate communication mechanisms in legacy browsers for comet-style communication Allows for socket handling with standard Dojo methodologies (i.e. dojo.connect to listen to events) Automatic reconnects using the dojox.socket.Reconnect class © SitePen, Inc. All Rights Reserved Sunday, October 17, 2010
  • 14. dojox.Socket Usage // Require the classes dojo.require("dojox.socket"); // When the class is loaded... dojo.ready(function() { // Create a Dojox socket instance var socket = dojox.socket({ url: "//comet-server/comet" }); // Connect to events via standard dojo.connect method dojo.connect(socket, "onmessage", function(event){ //Retrieve the message var message = event.data; }); // Alternate event listening syntax socket.on(“message”, function() { /* handle message */ }); // Send a message socket.send("Ping!"); // Close a socket socket.close(); }); © SitePen, Inc. All Rights Reserved Sunday, October 17, 2010
  • 15. dojox.socket.Reconnect Usage // Require both Dojo classes dojo.require("dojox.socket"); dojo.require("dojox.socket.Reconnect"); // When the resources are ready... dojo.ready(function() { // Create socket var socket = dojox.socket({url:"/comet"}); // Make sockets reconnect automatically socket = dojox.socket.Reconnect(socket); }); © SitePen, Inc. All Rights Reserved Sunday, October 17, 2010
  • 16. Comet Servers Historically, web servers have been written to handle burst of short-lived connections Comet connections are long-lived but not always transmitting data Many servers are written geared toward Comet or specifically for Comet Comet servers best when sitting alongside a traditional web server © SitePen, Inc. All Rights Reserved Sunday, October 17, 2010
  • 17. Comet Servers cometD (Java, Bayeux) Tunguska (on Node.js or Narwhal, Bayeux or RestChannels) Hookbox (Python, CSP) DWR (Java, Bayeux and others) Lightstreamer (Java, Bayeux and others) Faye (JavaScript or Ruby, on Node.js or Rails) APE (Python, CSP) WebSync (.NET, Bayeux) ... © SitePen, Inc. All Rights Reserved Sunday, October 17, 2010
  • 18. Publish and Subscribe with Comet Communication is done through channels Allows the web server to send directed messages to the Comet server Channels are hierarchical Wildcards can be used © SitePen, Inc. All Rights Reserved Sunday, October 17, 2010
  • 19. Comet Security Authentication can happen on the web server and define a unique channel on the Comet server Web server can pass authentication credentials to the comet server Adds overhead © SitePen, Inc. All Rights Reserved Sunday, October 17, 2010
  • 20. Protocols Bayeux PubSub, Transports CSP (Comet Session Protocol) More like working with a socket PubSub is separated REST/HTTP Channels XMPP Many other proprietary and open messaging protocols © SitePen, Inc. All Rights Reserved Sunday, October 17, 2010
  • 21. Comet Clients Each project has one Many toolkits (Dojo, jQuery) have one Dojo 1.6 has dojox.socket Server-side clients to connect normal servers to Comet servers © SitePen, Inc. All Rights Reserved Sunday, October 17, 2010
  • 22. Hosted Hookbox Hookbox: Very simple Comet server http://hosted.hookbox.org/ Free Hosted Comet Service http://dylan.io/hookbox.php © SitePen, Inc. All Rights Reserved Sunday, October 17, 2010