Feb 23

Observer pattern implementation in web application using Comet

     Observer pattern can be implemented easily in C/S based applications, but in web application area it is not easy to implement caused by the shortcoming of HTTP protocol. Currently we have some solutions : 

  1. browser plugins: we can use socket communication in flash or applet etc. by this we can implement C/S likely applications, the server side can push messages to the small application in browser side,  when the small application receives messages, it can communication with javascript engine. 
  2. comet : it also can be called "server push". it was based on long time polling, we start a request from browser side, the server side application keep the request live, by this we can keep sending messages to browser side. nowadays, some web application servers start to support comet, like : tomcat6/tomcat7 , jetty etc.. In tomcat, NIO is used to optimized the IO performance and decrease the expense of CPU. 
  3. HTML5 web socket. 

    Thinking of implementing the Observer pattern:

  1. send a request with the concerning message types, when server side received these messages, save these concerning message types in the session scope. Of course we can persistence these concerning message type in database, then we can design a customer independent notification user experience.  
  2. open a long time polling from the browser side, when server side received this request, we save the request related  response in a message send thread, at the same time we must keep this request live. Then we can send message signal to the message send thread. In the message send thread we can determine which message must write to which response.