lc-sse
2026-11-29
lc-sse: generic SSE library (umbrella: core + Hunchentoot + Clack)
Upstream URL
Author
Maintainer
License
1Overview
lc-sse is a Server-Sent Events library for Common Lisp. It provides:
- Thread-safe SSE streaming with a clean generator lifecycle
- Proxy-safe keep-alive with automatic client-disconnect detection
- Streaming compression: zstd (built-in) and brotli (optional via
cl-brotli) - Hunchentoot and Clack backends (the Clack backend codes against the Clackprotocol, not the
clackpackage itself; load whichever server you prefer) - Optional Woo event-loop dispatch (
lc-sse/woo) - Optional connection registry for broadcast and unicast (
lc-sse/registry)
lc-sse has no dependency on any higher-level framework: it is the SSE foundation used by
datastar-cl and that is its primary use-case, but it is self-contained and general-purpose.
2Installation
2.1Via Quicklisp (once accepted into the dist, WIP)
(ql:quickload :lc-sse) ; core + Hunchentoot + Clack backends
(ql:quickload :lc-sse/registry) ; broadcast/unicast registry
(ql:quickload :lc-sse/brotli) ; optional brotli compression
(ql:quickload :lc-sse/woo) ; optional Woo event-loop dispatch
2.2Manually (until Quicklisp dist inclusion)
Clone the repo alongside your project and add it to your ASDF source registry,
or symlink it under ~/quicklisp/local-projects/:
git clone https://codeberg.org/fsm/lc-sse ~/quicklisp/local-projects/lc-sse
Then ql:quickload as above.
3Sub-system layout
lc-sse/core: base class, lifecycle, macros; no backend depslc-sse/hunchentoot: Hunchentoot adapterlc-sse/clack: Clack-compatible adapter (no compile-time dep on theclackpackage; works withany Clack-compatible server you load separately)lc-sse/brotli: Brotli compression supportlc-sse/registry: Connection registry (broadcast + unicast)lc-sse/woo: Woo event-loop dispatch for Clack generatorslc-sse: loads core + Hunchentoot + Clack
4Quick start
4.1The with-sse macro
with-sse is the single entry point for all SSE patterns:
(with-sse (gen request-or-env-responder &key on-connect on-disconnect keep-alive) body...)
The backend is selected at macroexpansion time from the second argument:
- A single symbol (e.g.
hunchentoot:*request*) -> Hunchentoot - A two-element list
(env responder)-> Clack
The connection lifetime is determined entirely by the body:
| Situation | Pattern |
|---|---|
| Pure push: broadcaster writes, handler only parks | Empty body + :keep-alive N |
| Body writes regularly (dashboard, live feed) | Non-empty body with manual keep-sse-alive loop |
:keep-alive with a non-empty body is inert (a compile-time warning is issued on literal values).
The body must heartbeat itself with (keep-sse-alive gen) in a timed loop.
4.2Hunchentoot: one-shot request/response
Fire events and return immediately; the connection closes when the body exits:
(hunchentoot:define-easy-handler (stream-handler :uri "/stream") ()
(lc-sse:with-sse (gen hunchentoot:*request*)
(lc-sse:send-event gen "message" '("hello from lc-sse"))))
4.3Hunchentoot: pure-push CQRS (empty body + :keep-alive)
The broadcaster writes to generators directly; the GET handler only parks and cleans up on
disconnect. Use an empty body with :keep-alive N: the macro injects a heartbeat loop. When the
socket dies, the next heartbeat write signals client-disconnected, which unwinds the macro,
calls :on-disconnect, and closes the stream:
(ql:quickload :lc-sse/registry)
(defvar *clients* (lc-sse.registry:make-sse-registry "updates"))
(hunchentoot:define-easy-handler (subscribe :uri "/updates") ()
(lc-sse:with-sse (gen hunchentoot:*request*
:keep-alive lc-sse:*default-keep-alive-interval*
:on-connect (lambda (g) (lc-sse.registry:register *clients* g))
:on-disconnect (lambda (g) (lc-sse.registry:unregister *clients* g)))))
;; From any other thread: notify-subscribers snapshots, iterates,
;; and auto-unregisters dead generators
(defun broadcast-update (content)
(lc-sse.registry:notify-subscribers
*clients*
(lambda (gen) (lc-sse:send-event gen "update" (list content)))))
4.4Hunchentoot: long-lived body with manual heartbeat
The body drives its own writes and calls keep-sse-alive to both heartbeat the proxy and probe
for disconnect:
(hunchentoot:define-easy-handler (dashboard :uri "/live") ()
(lc-sse:with-sse (gen hunchentoot:*request*
:on-connect #'log-connection
:on-disconnect #'log-disconnection)
(lc-sse:send-event gen "init" (list (initial-render)))
(loop
(sleep 5)
(lc-sse:send-event gen "update" (list (next-update)))
(lc-sse:keep-sse-alive gen))))
4.5Clack
The backend uses the async responder pattern. :keep-alive behaves identically to the Hunchentoot
examples above:
;; One-shot
(lambda (env)
(lambda (responder)
(lc-sse:with-sse (gen (env responder))
(lc-sse:send-event gen "message" '("hello")))))
;; Pure-push CQRS (empty body + :keep-alive)
(lambda (env)
(lambda (responder)
(lc-sse:with-sse (gen (env responder)
:keep-alive lc-sse:*default-keep-alive-interval*
:on-connect (lambda (g) (lc-sse.registry:register *clients* g))
:on-disconnect (lambda (g) (lc-sse.registry:unregister *clients* g))))))
4.5.1The Woo situation
The above will block Woo. For this pattern to work, the cqrs branch of my woo fork is needed
(hopefully until something like it or better gets included): it registers the lambdas as user
channels that integrate into woo's event loop (libev, really), thus making this pattern possible -
not only possible, it is able to withstand thousands of parallel, long-term SSE streams. You
shouldn't "block the event loop" (meaning you shouldn't use the connections to do heavy work), but
for sending data (with any heavy work done elsewhere), it works as expected.
4.6Running the examples
Runnable single-file examples are in examples/. Each loads its deps via ql:quickload and starts
a server immediately; test with curl -N:
| File | Pattern | Port |
|---|---|---|
examples/ticker.lisp |
Long-lived body, manual keep-sse-alive (Hunchentoot) |
7400 |
examples/broadcast.lisp |
Pure-push CQRS: registry + background broadcaster | 7400 |
examples/clack.lisp |
Long-lived body, manual keep-sse-alive (Clack) |
7401 |
sbcl --load examples/ticker.lisp
# in another terminal:
curl -N http://localhost:7400/ticker
The examples are the executable form of the quick-start patterns above and are kept in sync with them.
5Registry (broadcast to all connected clients)
(ql:quickload :lc-sse/registry)
;; Create a registry once at startup:
(defparameter *clients* (lc-sse.registry:make-sse-registry "my-app"))
;; Register / unregister via :on-connect / :on-disconnect in with-sse (see above).
;; Broadcast from any thread:
(lc-sse.registry:notify-subscribers *clients*
(lambda (gen)
(lc-sse:send-event gen "update" '("tick"))))
For unicast-by-key (e.g. by session id):
(defparameter *sessions* (lc-sse.registry:make-keyed-sse-registry "sessions"))
;; Register with a key:
:on-connect (lambda (g) (lc-sse.registry:register *sessions* g :key session-id))
;; Unicast to one session:
(lc-sse.registry:notify-subscriber *sessions* session-id
(lambda (gen) (lc-sse:send-event gen "update" (list payload))))
6Compression
zstd is supported out of the box (via the zstd system). Brotli is optional:
(ql:quickload :lc-sse/brotli) ; loads cl-brotli and prepends :br to the priority list
Compression is negotiated automatically from the client's Accept-Encoding header. The default
priority order is (:zstd) (or (:br :zstd) with brotli loaded).
6.1Controlling priority and parameters
*default-compression-priority* is the ordered list of algorithms to negotiate. Override globally:
(setf lc-sse:*default-compression-priority* '(:zstd :br)) ; zstd preferred
(setf lc-sse:*default-compression-priority* '()) ; disable compression
*default-compression-parameters* is a per-algorithm alist; each entry's car is an algorithm
keyword, its cdr is a plist of keyword arguments spliced into that algorithm's stream
constructor:
;; Tune brotli window; leave zstd at library defaults
(setf lc-sse:*default-compression-priority* '(:br :zstd)
lc-sse:*default-compression-parameters* '((:br :lgwin 22 :size-hint 4096)
(:zstd)))
Per-request overrides (Hunchentoot example; same keyword args on make-clack-sse-generator):
;; Prefer brotli for this request only
(make-hunchentoot-sse-generator request :compression-priority '(:br :zstd))
;; Disable compression for this request entirely
(make-hunchentoot-sse-generator request :disable-compression t)
;; Per-request brotli tuning
(make-hunchentoot-sse-generator request :algorithm-parameters '((:br :lgwin 22)))
Only the negotiated algorithm's parameter entry is applied; brotli keywords are never passed to
zstd (which would signal program-error on unknown keywords).
7TCP_NODELAY (Nagle suppression)
Without TCP_NODELAY, Nagle's algorithm causes a ~40 ms per-response stall on small back-to-back
chunked SSE writes (the Linux delayed-ACK timer). lc-sse/hunchentoot exports two helpers:
| Class | Use |
|---|---|
tcp-nodelay-mixin |
Mix into any hunchentoot:acceptor subclass |
tcp-nodelay-easy-acceptor |
Drop-in for hunchentoot:easy-acceptor; define-easy-handler still works |
;; Simplest: swap in the drop-in acceptor:
(hunchentoot:start
(make-instance 'lc-sse:tcp-nodelay-easy-acceptor :port 8080))
;; Custom acceptor (e.g. combined with SSL):
(defclass my-acceptor (lc-sse:tcp-nodelay-mixin hunchentoot:ssl-acceptor) ())
process-connection :before sets TCP_NODELAY via usocket once per connection, before any request
processing. Portable on all usocket-backed Lisps; best-effort on LispWorks.
For Clack, TCP_NODELAY is a server-layer concern, configure it in your Clack server startup
directly (there's currently no equivalent for Woo in this library).
8Conditions
The condition system used:
sse-error (error)
- compression-error ; error initialising or flushing the compressor
- client-disconnected ; (also inherits stream-error) raised on broken pipe /; closed socket; used as the disconnect-detection signal; in keep-sse-alive and send-event
client-disconnected is the normal mechanism for detecting disconnect in a keep-alive loop: the
next write or keep-sse-alive call after the socket closes raises it, which unwinds the with-sse
body and triggers :on-disconnect.
serve-sse is the low-level function underlying with-sse; use it when you need to build a
custom lifecycle wrapper.
9Tests
Included are both unit tests and integration tests:
# Unit tests (no server required):
make test-unit
# Integration tests (starts servers on ports 7351 / 7353):
make test-integration
... or from the REPL:
(ql:quickload :lc-sse-tests)
(fiveam:run! :lc-sse)
(ql:quickload :lc-sse-tests/integration)
(lc-sse/tests/integration:run-all-tests)
lc-sse-tests/woo currently *requires* a patched Woo and is kept separate from the standard test
suite.
10License
lc-sse is free software: you can redistribute it and/or modify it under the terms of the GNU
Lesser General Public License as published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
See COPYING.LESSER (LGPLv3) and COPYING (GPLv3) for the full terms.
Copyright (C) 2025, 2026 Frederico Muñoz / ΛↃ lambda combine