Java Reference
In-Depth Information
Chapter8.Adding WebSockets
WebSockets are one of the biggest additions in Java EE 7. In this chapter, we will explore
the new possibilities that they provide to a developer. In our ticket booking applications, we
already used a wide variety of approaches to inform the clients about events occurring on
the server side. These include the following:
• JSF polling
• Java Messaging Service (JMS) messages
• REST requests
• Remote EJB requests
All of them, besides JMS, were based on the assumption that the client will be responsible
for asking the server about the state of the application. In some cases, such as checking
whether someone else has not booked a ticket during our interaction with the application,
this is a wasteful strategy; the server is in the position to inform clients when it is needed.
What's more, it feels like the developer must hack the HTTP protocol to get a notification
from a server to the client. This is a requirement that has to be implemented in most web
applications, and therefore, deserves a standardized solution that can be applied by the de-
velopers in multiple projects without much effort.
WebSockets are changing the game for developers. They replace the request-response
paradigm in which the client always initiates the communication with a two-point bidirec-
tional messaging system. After the initial connection, both sides can send independent mes-
sages to each other as long as the session is alive. This means that we can easily create web
applications that will automatically refresh their state with up-to-date data from the server.
You probably have already seen this kind of behavior in Google Docs or live broadcasts on
news sites. Now we can achieve the same effect in a simpler and more efficient way than in
earlier versions of Java Enterprise Edition. In this chapter, we will try to leverage these
new, exciting features that come with WebSockets in Java EE 7 thanks to JSR 356 ( ht-
tps:// ) and HTML5.
In this chapter, you will learn the following topics:
• How WebSockets work
• How to create a WebSocket endpoint in Java EE 7
• How to create an HTML5/AngularJS client that will accept push notifications from
an application deployed on WildFly
Search WWH ::

Custom Search