The next version of JSF, 2.3 comes with the support of the WebSocket protocol. But can’t we use such with the current version of JSF (2.2)? Yes, we can. Using Java enterprise edition (Java EE) 7 you may create a so called endpoint. NetBeans support creating this out of the box, and it can be combined with JSF 2.2. Not as easy as the built in option of the upcoming version, but with high-performance for some special aspects.

This article is the beginning of a small three-part series.

  1. Technical aspects of the WebSocket protocol
  2. WebSockets in a Java EE 7 application
  3. JSF 2.3 and WebSockets

Todays part is the smallest providing you a little bit of technical background. The other parts will show small, but complete example applications.

HTTP

HTTP is one of the internet protocol current browsers support. It is short for Hypertext Transfer Protocol. As its name suggests, it was developed to transfer text from a server to a client. These texts could (and still can) contain hyperlinks to other texts. HTTP became standardized by W3C [1].

HTTP is a stateless request-response protocol: A client sends a request to the server. The server sends a response to the client and then the connection terminates. For every requested resource, a new connection is needed. For example, if a web page contains some text and three images, then a total of four requests is needed.

Because every request is initiated by the client, it is not possible to push information from server to client at any time. There are some workarounds like deferred answers: The server delays its response until some information is available and sends the answer. Then the connection closes and the server needs a new request before it can “push” further information.

Even though widely spread, HTTP has a couple of more limitations. Thus, a couple of alternatives and extensions have been developed to overcome these restrictions. With HTTP/2 for example, the client may request a couple of resources more effectively because this protocol allows multiple concurrent exchanges on the same connection. It allows a server push data using a client-side initiated connection. HTTP/2 became standardized by the Internet Engineering Task Force (IETF) in 2015 [2].

WebSockets

WebSockets are a different approach to establish a server push. More, the WebSocket protocol enables a real bidirectional communication between two peers. It became standardized by the IEFT in 2011. Its specification is part of the HTML living standard [3].

Technically, the WebSocket protocol is initiated as an upgrade of an HTTP request. This implies, that a connection needs to be initiated by a client. Once the WebSocket connection is established after the upgrade request,  both partners act as peers. The connection becomes a full-duplex communication. Every peer may send data at any time.

The bidirectional communication is not the only advantage of this protocol. Because the connection stays active, it does not need to be initiated again and again for every chunk of information. This saves a couple of bits and increases performance.

The switchover from HTTP to WebSocket will be initiated by the WebSocket Opening-Handshake-Request. It starts like this:

GET /endpoint HTTP/1.1
Host: mueller-bruehl.de
Connection: Upgrade
upgrade: websocket
...

“GET /endpoint” refers to an endpoint of the WebSocket server. “endpoint” is not a fixed term, but the name of the endpoint you want to connect to. Implementing a chat application it might be “GET /chat”. Implementing such an endpoint is essential.

Within this article I don’t want to dive really deep into the protocol. This article is intended to explain the limitations of HTTP and its overcome with WebSockets. To dive into the details, please refer to the mentioned resources or get a more detailed overview at Wikipedia [4].

Endpoint

Although I show the applications within the next parts of this short series (estimated February), I want to demonstrate how to create a WebSocket endpoint with NetBeans. Simply create a fresh web project. Then, add a new file (e.g. by pressing Ctrl + N). Within the New File dialog, choose WebSocket endpoint.

Click onto Next to provide a class name, package name, and endpoint name. After confirming with Finish, NetBeans creates a skeleton class for you.

package de.muellerbruehl.chat;

import javax.websocket.OnMessage;
import javax.websocket.server.ServerEndpoint;

/**
 *
 * @author mmueller
 */
@ServerEndpoint("/chat")
public class Chat {

  @OnMessage
  public String onMessage(String message) {
    return null;
  }
  
}

The @OnMessage annotated method is invoked when this endpoint receives data on its connection. Here, we need to implement the desired behavior. This will be part of the first application described during the next part.

Stay tuned!

 

[1] www.w3.org/Protocols

[2] tools.ietf.org/html/rfc7540

[3] tml.spec.whatwg.org/multipage/comms.html#network

[4] en.wikipedia.org/wiki/WebSocket