The preceding sections have introduced you to synchronous and asynchronous patterns of web service interaction. The JAX-RS 2.1 API introduces support for the publish-subscribe pattern with the inclusion of server-sent events. Server-sent events (SSEs) are a specification originally introduced as part of HTML5 by the W3C. It provides a way to establish a one-way channel from a server to a client. The connection is long running; it is reused for multiple events sent from the server, yet it is still based on the HTTP protocol. Clients request the opening of an SSE connection by using the special media type text/event-stream in the Accept header.
The following are the key JAX-RS APIs defined for the realization of SSE under the javax.ws.rs.sse package:
Interface | Description |
SseEvent |
Base Server-Sent Event definition. The basic attributes of an event are ID, name, and comment. There are two types of SseEvent:
|
Sse |
Used on the server side to create an event. |
SseEventSource |
This is the client for reading and processing incoming Server-Sent Events. |
SseEventSink |
This is the events stream used to publish the event to the client. |
SseBroadCaster |
Applications may need to send events to multiple clients simultaneously. This action is called broadcasting in JAX-RS. Multiple SseEventSink can be registered (or subscribed) on a single SseBroadcaster. |
The following sequence diagram depicts the realization of SSE using the previously mentioned JAX-RS APIs. In the next chapter, we will see an example of SSE implementation using the Jersey framework: