8 Using Server-Sent Events in WebLogic Server

Oracle WebLogic Server supports server-sent events through the integration of the Jersey 2.x library.The Jersey 2.x library provides the Reference Implementation (RI) of JSR 370 (JAX-RS 2.1: Java API for RESTful Web Services).

Overview of Server-Sent Events (SSE)

Server-sent events enable servers to push data to web pages over standard HTTP or HTTPS through a unidirectional client-server connection. In the server-sent events communication model, the browser client establishes the initial connection, and the server provides the data and sends it to the client. For general information about server-sent events, see the Server-Sent Events W3C Candidate Recommendation.

Server-sent events are part of the HTML 5 specification, which also includes WebSocket technology. Both communication models enable servers to send data to clients unsolicited. However, server-sent events establish one-way communication from server to clients, while a WebSocket connection provides a bidirectional, full-duplex communication channel between servers and clients, promoting user interaction through two-way communication. The following key differences exist between WebSocket and server-sent events technologies:

  • Server-sent events can only push data to the client, while WebSocket technology can both send and receive data from a client.

  • The simpler server-sent events communication model is better suited for server-only updates, while WebSocket technology requires additional programming for server-only updates.

  • Server-sent events are sent over standard HTTP and therefore do not require any special protocol or server implementation to work. WebSocket technology requires the server to understand the WebSocket protocol to successfully upgrade an HTTP connection to a WebSocket connection.

For more information about WebSocket technology, see Using the WebSocket Protocol in WebLogic Server in Developing Applications for Oracle WebLogic Server.

Using Server-Sent Events

Server-sent events are used to push notifications asynchronously to the client over standard HTTP protocol.

From JAX-RS 2.1, the server-sent event APIs are defined in the javax.ws.rs.sse package. This package includes the interfaces Sse, SseEventSink, SseEvent, SseBroadcaster, and SseEventSource for server-sent events.

The server-sent events server API is used to accept connections and send events to one or more clients. From the server side, an instance that implements the interface SseEventSink corresponds to a single client HTTP connection.

You can also configure the applications to send events to multiple clients simultaneously using the SseBroadcaster interface. The interface enables to send events to all registered event outputs.

From the client side, the interface SseEventSource is used to open a connection to the Web Target that is configured with a resource location. The clients must request the opening of a server-sent event connection using the media type text/event-stream in the Accept header. The established connection is persistent and can be re-used to send multiple events from the server.

Understanding the WebLogic Server-Sent Events API

WebLogic Server supports server-sent events through the integration of the Jersey 2.x. The use of server-sent events through Jersey 2.x is supported only in JAX-RS resources.

From JAX-RS 2.1, the server-sent event APIs are defined in the javax.ws.rs.sse package. This package includes the interfaces Sse, SseEventSink, SseEvent, SseBroadcaster, and SseEventSource for server-sent events.

For more information about server-sent events in Jersey 2.x, see Server-Sent Events (SSE) Support in the Jersey 2.29 User Guide.

The WebLogic Server Server-Sent Events API is in the package org.glassfish.jersey.media.sse. For information about the interfaces and classes included in this package, see the API documentation for javax.ws.rs.sse in the Java EE 8 API Documentation.