Servlet Push

HTTP Server Push

HTTP server push  is a mechanism for sending data from a web server to a web browser. HTTP server push can be achieved through several mechanisms. Http Server push also known as HTTP streaming.

Most web servers offer this feature by Non parsed Headers ("Server Push"). Another way of achieving server push is by using the special MIME type called multipart/x-mixed-replace

 

Server Pull and Push


Usually browser asks for a page or image or an object from the server and the server sends the response to the browser. This is called 'Pull' method the most common method in webserver technology. At times the server itself can send data to the browser. Its required sometimes that the server has to push the data to its clients. For example consider the cricket score card or email web clients. These kinds of data change consistently, so server has to push the content to client to synchronize the client with the server data.

Client side Refresh methods


In traditional websites the synch or data update is done using one of the below
  1. Refresh tag in the HTML metadata
  2. JavaScript countdown timer to reload the data

Advantages of server push


  1.  Reduced page refresh
  2.  Limited latency
  3. Quick updates

With server push, the socket connection between the client and the server remains open until the last portion of response is committed. This enables the server to send the updates quickly.

Disadvantages of server Push


  1.  Browser support
  2.  Keep alive connection overhead.

MIME type multipart/x-mixed-replace


Servlet push is achieved by using the MIME type multipart/x-mixed-replace.

From Wikipedia http://en.wikipedia.org/wiki/MIME

The content type multipart/x-mixed-replace was developed as part of a technology to emulate server push and streaming over HTTP.

All parts of a mixed-replace message have the same semantic meaning. However, each part invalidates - "replaces" - the previous parts as soon as it is received completely. Clients should process the individual parts as soon as they arrive and should not wait for the whole message to finish.

Originally developed by Netscape,[5] it is still supported by Mozilla, Firefox, Chrome,[6] Safari, and Opera, but traditionally ignored by Microsoft. It is commonly used in IP cameras as the MIME type for MJPEG streams.[7]

Sample code.


Sample code is given below to explain how servlet push works. This code simply prints the current time on the output (browser) and updates the time every second.
package com.servlet;

import java.io.IOException;
import java.util.Calendar;

import javax.servlet.*;
import javax.servlet.http.*;

public class TimeServlet extends HttpServlet {

 private static final long serialVersionUID = 1L;

 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  ServletOutputStream out = response.getOutputStream(); // Binary data

  // Set the MIME type
  response.setContentType("multipart/x-mixed-replace;boundary=eor");

  for (int i = 0; i < 10; i++) {
   // Start the response i
   out.println("Content-Type:text/plain");
   out.println();
   out.println("Time :" + getTS());

   // End the response i
   out.println();
   out.println("eor");
   out.flush();

   sleep(1000);
  }

  out.println("Content-Type:text/plain");
  out.println();
  out.println("clock is done!");
  out.print("eor");
  out.flush();

  // close the response
  out.close();
 }

 private static String getTS() {
  Calendar cal = Calendar.getInstance();
  return cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND);
 }

 private void sleep(int ms) {
  try {
   Thread.sleep(ms);
  } catch (InterruptedException e) {
  }
 }
}

Comments

Post a Comment

Popular posts from this blog

SVN File Creation Date

Java NIO2 - Watching a directory for changes. FileWatcherService.

Ubuntu / kubuntu - Laptop key board cursor jumping issue