tl;dr

Server Sent Events (SSE) are a very neat way to have long running HTTP calls that don’t need to “return” anything, but where you want to ensure that the call was successful in a synchronous way.

Problem Area

In my work we do a lot of large data load operations. In the interest of keeping things as simple (and debuggable) as possible it is desirable to do this in a synchronous way (no polling, no callbacks, etc). Having the data load happen via a HTTP POST is typically the easies way to send the data. However, what usually happens when the client and server are far apart is that usual network events, like timeouts will kill the connection.

Server Sent Events

Server Sent Events are simply a HTTP technique with its own Content-type: text/event-stream. The semantics are, the server can send events back to the client without closing the connection. This event stream format typically look like:

event: notification
data: new email

With each event separated by a blank line.

Part of the beauty is that it has some application frameworks have SSE as a built in response type (Spring Boot SseEmitter), making it easy to work with.

You do have to structure your code to be able to utilise this, such as outputting a progress indicator.

event: progress
data: 10 of 50

event: progress
data: 20 of 50

event: progress
data: 30 of 50

event: progress
data: 40 of 50

event: done

Because SSE is just a normal HTTP response, you can easily use cURL to push data, and see the events come back. Or, if you use your own SSE consumer in your code, you can capture a success or failure event.