Server-sent events

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 publish-subscribe pattern involves two key actors, publisher and subscribers, connected via a communication channel. One or more subscribers register for the event with the publisher, and the publisher publishes the corresponding event to all the registered subscribers.

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:

  • InboundSseEvent is used on the client side for accepting Server-Sent Events.
  • OutboundSseEvent is used on the server side when creating and sending an event to a client. OutboundSseEvent.Builder is used for creating OutboundSseEvent.

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:

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

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