Previous Topic

Next Topic

Book Contents

Book Index

Server Sent Events

What are Server-Sent Events

When a standard flow of an HTTP request-response is executed, the client opens a connection. It is followed by an HTTP request towards the server, such as an HTTP GET request, for example. After that, an HTTP response is sent from the server and the connection is closed after the response has been fully sent and received. The initiative always comes first from the client, and the client requests all the data.

In comparison, when Server-Sent Events (SSE) are used, it is possible for the server to asynchronously push the data in a client-server connection established by the client. After that, it is the server who is the initiator of the sending of data and deciding whether to send it towards the client whenever new data is available.

In some ways, Server-Sent Events are similar to long-polling as a mechanism, however not only one message is sent per connection:

SSE technology typically reuses the connection for many messages also called events.

When to use Server-Sent Events

With Server-Sent Events a client subscribes to event notifications that are sent from a server. The server generates new events and sends them back to clients who have subscribed to receive those notifications.

Server-Sent Events is technically a solution for a one-way publish-subscribe model.

Server-Sent Events can be used for message exchanging in a RESTful service. Constant POST request-response requests can be a way to receive all new message notifications, however this can be impractical. Instead, an initial typical GET HTTP request-response mechanism is used to subscribe for the resource. As a result:

More detailed information on how to use SSE Events with Jersey can be found here:

https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/sse.html