Lesson 28Developing Web Applications with WebSockets

HTTP-based technologies like Java Servlets, JSP, or JSF use the request-response model. A web browser establishes a connection with the web server and sends a request, and then the server responds using the same connection; this is called a half-duplex communication. Think of a narrow bridge where cars can go only in one direction at a time. 

As opposed to HTTP, WebSocket protocol is a two-way street (a full-duplex communication). The data travels in both directions over the same connection. The client doesn’t have to initiate the request; the server can push the data to the client when the new data is available.

Another important advantage that WebSocket protocol has over HTTP protocol is that the former adds almost no overhead to the data payload. The following list includes some of the web applications that can benefit from using the server-side data push with WebSocket protocol:

  • Live trading/auctions/sports notifications
  • Controlling medical equipment over the web
  • Chat applications
  • Multiplayer online games

Although you can create a Java client that uses raw socket connections and the Java can push the data to the client as needed, the server may be located behind the firewall or a proxy server and the company policy wouldn’t allow the opening of arbitrary ports for connections.  Similarly to HTTP/HTTPS, the WebSocket protocol uses standard port 80 for requests and port 443 for secure connections. These ports are usually ...

Get Java Programming 24-Hour Trainer, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.