Chapter 14. Using WebSockets with EJB 3
This chapter covers
• The basics of WebSockets
• Integrating WebSockets with Java EE
• Using annotated and programmatic endpoints
In this chapter we'll delve into an exciting new technology that was added to Java EE 7
to support HTML5: WebSockets. WebSockets are raw sockets that support true full-duplex
communication between the web browser and back-end server. With WebSockets, you can
push data to the web browser from the server without depending on hacks or having the cli-
ent poll the server.
14.1. Limits of request-response
In the traditional HTTP model, the client browser opens a connection to an HTTP server
and requests an operation to be performed, such as GET , POST , PUT , DELETE , and so on.
The HTTP server performs the operation and returns a result, usually HTML content. The
connection to the server may be kept open for additional requests so that if multiple re-
sources from the server are being retrieved, each request doesn't require an additional sock-
et to open and close. If you look at the complexity of some of the current web pages, this
makes sense—pages with lots of content, such as eBay.com, would be extremely slow if
each image had to endure a full socket opening and closing. In each case, however, the client
is the initiator—the client makes a request and the server responds. The server never pushes
data back to the client without the client making a request.
The request-response model works perfectly for websites that produce content, such as
newspapers, online encyclopedias, academic journals, file sharing, and so on. But the re-
quest-response model breaks down for uses on sites such as gaming, instant messaging,
stock updates, and anything else where content/data needs to be pushed from the server to
the client asynchronously. The request-response paradigm is a hindrance for these sites—the
client must always make the initial request. There are different approaches you can take to
get around this problem.