Introduction to Bayeux Protocol (HTTP Publish-Subscribe)

Attention: This is a short overview presentation showed at JavaDay Rome in 24 January 2009 on the form of slideshow.

Beginning HTTP

  • HTTP was designed as a protocol for retriving documents from remote servers
    • Communication between the client and the server is always initiated by the client and never by the server
    • Connections between the client and the server are transient and the server do not maintain any long-term state information regarding the client
    image

Twist the Formula

  • Why, then, should you want the server to be able to contact the client, and maintain persistent connections?

- There are several suitable

common use case -

use cases: AdviceME

  • Monitoring and Data Feeds
    • Client as a Dashboard or Monitor
    • Demand for monitoring applications
    • Market data feeds
    • Security Alerting
    • SLA Violation Advisor

image

use cases: Please Wait

  • Progress Updates
    • Communicating progress on long-running server-side activities
    • Report progress on long running server task

 

image

use cases: Share with me

  • Chat and Collaboration
    • Chat System
    • Photo sharing
    • On-Line Gaming
    • Collaborative Tools

image

Actual Approachs

  • Push Using Poling
    • Collaborative ability by pooling the server with client regular request asking for update.
      • Network wasteful
      • Server resource exhaustion
      • Client repeatedly contact the server to check for change
      • Pooling schedule not affected

image

Actual Approachs

  • Piggybacking
    • The contents of the response aren’t strictly related to the nature of the request
    • Response will also contain any changes to the domain model
    • Pooling schedule reset

image

<<COMET>>

  • Comet techniques
    • Keep the response stream open for a significantly longer time
    • Server send several pieces of data back in the response
    • Comet request is held open on the server for a while, multiple discrete changes are communicated
    • Long Polling

image

<<COMET>>

  • Comet techniques
    • Most modern web browser, permit only 2 concurrent HTTP connection to the given domain.
    • If more than one request is being held open against the same server, ordinary request will be blocked

      (same for different browser-tab)

      image

<<COMET>>

  • Comet techniques
    • Traditional servers allocate a thread to each incoming request
    • Design work well when serving large volume of short lived request
    • Long polls are held open request for a considerable length of time.
    • Thread-per-Request model is generally unable to scale to large numbers of clients.
    • Java servlet model itself is inherently a thread-per-request

image

<<Java COMET>>

  • Comet techniques
    • Java servlet model was developed to serve a very different traffic profile.
    • A large number of simultaneous request from Comet clients would exhaust the server’s thread pool. (doesn’t scale well)
    • More Comet-Friendly server architectures from the java-community:
      • Continuations (Jetty 6)
      • Grizzly
      • Tomcat 6
      • JSR 315 Servlet 3.0

image 

Bayeux provides a very nice abstraction on top of Comet transport, by casting the transfer data in terms of channels, to which clients can publish and subscribe


Comet and Reverse Ajax

Dave Crane and Phil McCarthy (2008)

 

Bayeux --> CometD

  • Bayeux
    • Emerging de facto standard for Comet
    • Existing reference implementation:
      • COMETD
        • Provided by the people at Sitepen:
          • (the same as) Dojo Javascript Toolkit

image

Bayeux --> CometD

  • Bayeux
    • Adopts exactly the same approach and provides a flexible, scalable API based around a publish-subscribe model
    • Data sent through, is always assigned a notional “channel”

image

Bayeux --> CometD

  • Bayeux
    • Channels allows multiple decoupled conversations on top of a single HTTP connection.
    • The client-side CometD and the server-side Bayeux implementation communicate over several notional channels, all bundled within a single HTTP request-response.

image

Purpose

  • Cometd is a project by the Dojo Fundation to implement Bayeux specification.
  • Bayeux is a purpose to implement responsive user interaction for web clients using Ajax and server-push technique called Comet

 

  • The messages are routed via named channels and can be delivered:
    • server2client
    • client2server
    • client2client (via server)

JSON Messages

Bayeux message are specified in JSON notation

 

     {

        "channel": "/some/name",

        "clientId": "83js73jsh29sjd92",

        "data": { "myapp" : "specific data", value: 100 }

     }

 

 

JavaScript Object Notation is a lightweight data-interchange format

 

Channels Style

  • Channels are by default broadcast publish subscribe.
  • Channels are identified by names and always starts with “/”
  • Channel pattern support trailing wildcards of either “*” or “**” :

 

  • /news/*
    • Valid channels : /news/italy , /news/uk ....

 

  • /news/**
    • Valid channels : /news/italy , /news/italy/lazio ...

Bayeux approach

  • Bayeux event is sent from the server to the client via HTTP response to a HTTP request sent in anticipation:
    • Polling Transport
      • On receipt of the HTTP response, the bayeux client issues a new bayeux message.
    • Streaming Transport
      • Use streaming technique (forever response), allowing multiple message to be sent over the same response

 

Bi-Direction

  • To solve the issue of bi-directional communications, Bayeux client will use two HTTP connections:
    • Messaging may occur asynchronously

image

 

 

Handshake Connect

  • Bayeux connection are negotiated between client and server with handshake messages that allow to define:
    • Connection Type
    • Authentication
    • Persistence subscription
    • Identification .....

 

image

Interactions

  • Bayeux client may send publish and subscribe messages to register interest in a channel:

image

Server APIs

  • Web application developers can optionally use server-side pub-sub APIs in their servlets or Java classes to get the Bayeux server context, manage channels, and the incoming and outgoing messages

 

image

WebLogic Stock Demo

  • There is a useful sample in the WebLogic 10.3 installation about this technology:
    • Can find it in the folder :
      • {ORACLE_HOME}\wlserver_10.3\samples\server\examples\src\examples\webapp\pubsub\stock

 

  • The demo describes a real-world scenario based on stock trading, and makes extensive use of the Bayeux APIs in both the server and client components
    • The Demo uses :
      • Dojo as its client-side library
      • WebLogic Bayeux implementation as server APIs

 

image   image

Web XML-Configuration

  • Server dedicated file-based configuration environment

 

image

Security Model

  • JEE security configuration based (ssl support)

 

image

JMS-Bayeux

  • Durable subscriber and persistence support, based on JMS architecture

 

image

Message Persistance

  • Persistent Channels and session-message failover support

 

image

JS Client APIs

  • Dojo JS Cometd library (Init and Publish)

 

    • Initialize the Dojo cometd environment.

dojo.io.cometd.init({}, "/servletContextMapping/cometd");


    • Publish a message to a channel.

dojo.io.cometd.publish("/a/channel", "message content");

    • or JSON message:

dojo.io.cometd.publish("/a/channel", {"data": "content"});

JS Client APIs

  • Dojo JS Cometd library (Subscribe)

 

Subscribe to a channel

  dojo.io.cometd.subscribe("/a/channel", null, "onUpdate");

  function onUpdate(message) {
          if (!message.data) {
                 alert("bad message format "+message);
                 return;
          }

          var data = message.data;
   }

JS Dinamic Demo

  • Dynamic Channel sub & unsubscription

 

  function subscribe(symbol) {

        dojo.io.cometd.subscribe("/stock/"+symbol, null, "onUpdate");

  }

 

  function unsubscribe(symbol) {

       dojo.io.cometd.unsubscribe("/stock/"+symbol, null, "onUpdate");

  }

  function subscribeAll() {

       var count = itemsStore.get().length;

       for (var i=0; i<count; i++) {

                   subscribe(itemsStore.getDataByIndex(i).symbol);

        }

  }

Web Publisher

  • Simple web-based client publisher (stock publisher)

 

image

 

Some useful link

  

Some useful link

  • Bayeux Java server-side implementation

 

 

Some useful link

  • Bayeux others-server side implementation

 

  

 

Nino Guarnacci   

nino.guarnacci (AT) oracle.com

Comments:

Nino, I would also encourage anyone that is interested in trying this with WLS to look at my series about this: http://blogs.oracle.com/jamesbayer/2008/10/realtime_updates_on_webpages_p_1.html The version of Dojo that shipped with the WLS 10.3 sample is old (0.9 maybe?) and the API's have changed in the later releases from the examples that you use in your slides. Thanks, James

Posted by James Bayer on February 09, 2009 at 06:37 AM CET #

excellent post!! if any of your readers are interested in quality ARCH SUPPORTS just click the highlighted links.

Posted by Orthotic Insoles on December 05, 2009 at 04:30 AM CET #

I too conceive so , perfectly composed post! .

Posted by Sydney Edington on April 25, 2011 at 01:59 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

.. useful blog on java and middleware technologies.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today