Summary

We have seen that WebSocket can dramatically reduce unnecessary network traffic and save on latency, and we know that WebSocket can scale to higher in comparison to previous solutions such as long-polling AJAX requests and COMET.

The Java WebSocket API is a new powerful technical enabler for building scalable web applications, especially on the JVM. This new standard, delivered as part of the Java EE 7 platform, allows simple POJOs to be annotated as a server endpoint with @ServerEndpoint, or as a client endpoint with @ClientEndpoint.

The API has event handling in Java that mirrors the actual design in JavaScript. A WebSocket can fire an open, close, message received, and error event. These can be intercepted with annotations, namely @OnOpen, @OnClose, @OnMessage, and @OnError.

Java WebSocket endpoints can participate in Context and Dependency Injection (CDI) and EJB containers. A WebSocket conversation is sustained by recording the session instance in a dependency bean.

When we retrieve the remote endpoint WebSocket, we can write a synchronous message back to the client or we can elect to write an asynchronous message. The key to this important decision lies squarely in the developer, writing code to the sub interfaces RemoteEndpoint.Basic and RemoteEndpoint.Async.

We learned that the endpoints could have encoder and decoders to handle messages as Java objects. There is also a programmatic equivalent API that complements declarative WebSocket annotations.

In the next chapter, we will rummage around Java RESTful services, including the all-new powerful client-side API.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.232.11