Real-time: SSE & WebSockets
Server-Sent Events (any engine)
SSE works on every engine, straight from a normal route:
app.get("/events", ctx -> {
try (SseEmitter sse = ctx.sse()) {
sse.send("plain data");
sse.send("update", "{\"n\":1}"); // named event
sse.send("update", "42", "{\"n\":2}"); // with id (client resume)
sse.comment("keep-alive");
}
});
The client consumes it with a standard EventSource. Thanks to virtual
threads, holding thousands of open SSE connections is cheap.
WebSockets (Jetty engine)
The WebSocket API lives in core (WsHandler, WsSession); the
implementation lives in the engine adapter. Today that's
ligero-server-jetty — the JDK engine cannot upgrade protocols and fails at
startup with a clear message if WebSocket routes exist.
runtimeOnly 'com.ligeroframework:ligero-server-jetty:0.8.0' // instead of ligero-server-jdk
app.websocket("/chat", new WsHandler() {
@Override public void onConnect(WsSession session) {
session.attributes().put("joined", Instant.now());
}
@Override public void onMessage(WsSession session, String message) {
session.send("echo: " + message);
}
@Override public void onClose(WsSession session, int statusCode, String reason) { }
@Override public void onError(WsSession session, Throwable error) { }
});
The Pluma engine (ligero-server-pluma) supports WebSockets too, with no
dependencies — see Server Engines.
HTTP routes and WebSockets share the same port; non-upgrade requests flow through the normal pipeline untouched.
Binary messages and compression
Override onBinary to accept binary messages and answer with
session.send(byte[]); a handler that does not override it refuses binary
messages with close code 1003. On the Pluma engine, messages are compressed
with permessage-deflate whenever the client offers it (browsers do) —
nothing to configure — and a compressed message can never inflate beyond the
message size limit.
app.websocket("/upload", new WsHandler() {
@Override public void onBinary(WsSession session, byte[] message) {
session.send(checksum(message));
}
});
Origin checks
A browser will open a WebSocket to your app from any website, cookies
included, and CORS does not apply. So by default a handshake is accepted only
when it has no Origin header (a non-browser client) or the origin is the
app's own host; everything else gets 403. Trust additional origins by
overriding acceptOrigin:
app.websocket("/chat", new WsHandler() {
@Override public boolean acceptOrigin(String origin, String host) {
return "https://app.example.com".equals(origin)
|| WsHandler.super.acceptOrigin(origin, host);
}
// ...
});
The middleware pipeline does not run for WebSocket upgrades, so authenticate
inside onConnect (for example with a short-lived token the page obtained
over HTTP) and close the session if it fails.
Which one do I need?
| SSE | WebSocket | |
|---|---|---|
| Direction | server → client | bidirectional |
| Transport | plain HTTP | protocol upgrade |
| Engine support | all | Jetty adapter |
| Typical use | feeds, notifications, progress | chat, games, collaborative editing |