As mentioned in Chapter 1, What's in Java EE 8?, while SSE is an HTTP-based standard for one-sided communication, WebSockets is a standard allowing for bidirectional communication between both client and server. WebSockets can be used in scenarios which require two-way communication, such as chat-based applications. WebSockets are published as endpoints using either a programmatic approach or an annotated one.
An endpoint would either extend the javax.websocket.Endpoint class for programmatic style or use the easier approach of using @ServerEndpoint annotation. An endpoint instance is created per connection:
@ServerEndpoint("/chat")
public class ChatEndpoint {
@OnMessage
public void onMessage(final Session session, String msg) {
try {
session.getBasicRemote().sendText(msg);
} catch (IOException e) { ... }
}
}
While this is a simple...