Day 17 — Real-Time Communication via Server-Sent Events (SSE)¶
Daily Time Investment: 2.5 hours | Week: 3 | Phase: 3
Daily Schedule¶
| Segment | Duration | Activity |
|---|---|---|
| Core Theory | 45 min | JAX-RS SSE API: SseEventSink, Sse, OutboundSseEvent, SseBroadcaster, lifecycle hooks, disconnect detection |
| Book Reading | 30 min | EE8 AppDev Ch10 — JAX-RS Server-Sent Events section |
| Hands-On Lab | 75 min | Unicast progress streaming (5 ETL stages) + Multicast broadcaster (2 subscribers); verify onClose eviction |
Files in This Day¶
-
EE8 AppDev Ch10 — JAX-RS SSE API
SseEventSink,Ssefactory,OutboundSseEventbuilder (.name()/.id()/.data()/.reconnectDelay()/.comment()),SseBroadcasterpub/sub fan-out,onClose+onErrorlifecycle hooks, SSE wire format (text/event-stream), JavaScriptEventSourceclient. -
Lab Guide — SSE Unicast & Multicast
Unicast: stream 5 progressive ETL stages over single persistent connection; Multicast: 2 subscribers receive broadcaster alerts simultaneously;
onCloselistener detects disconnected sinks;sink.isClosed()prevents orphaned threads.
Prerequisites to Continue¶
New concepts not seen in Phase 1 or Phase 2
- Server-Sent Events (SSE) — W3C standard HTTP/1.1 protocol for unidirectional server-to-client streaming; the connection stays open and the server pushes events; client auto-reconnects on disconnect (built-in retry mechanism)
- SSE Wire Format — plain text over HTTP with
Content-Type: text/event-stream; each event has optional fields:event: name\n,id: eventId\n,data: payload\n,retry: milliseconds\n,: comment\n; events are separated by a blank line\n\n SseEventSink— represents ONE open client connection; injected via@Context SseEventSink sink; the JAX-RS method must returnvoidand be annotated with@Produces(MediaType.SERVER_SENT_EVENTS)Sse— the container-provided factory; injected via@Context Sse sse; used to createOutboundSseEvent.Builderinstances andSseBroadcasterinstancesSseBroadcaster— enterprise pub/sub channel; multipleSseEventSinkinstances subscribe; a singlebroadcast(event)call fans out to ALL registered subscribers simultaneouslysink.isClosed()— check before writing to detect client disconnect; prevents throwing exceptions and resource leaks from writing to a dead connection- SSE vs WebSockets — SSE is unidirectional (server → client only) over plain HTTP; WebSockets are bidirectional (full-duplex) over WS/WSS protocol; SSE works transparently through firewalls/proxies; WebSockets require proxy WS upgrade support