SSE transport for Gerbil
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-10-22 15:30:43 -07:00
.gitignore add readme, license, gitignore 2026-07-27 18:42:51 -07:00
build.ss namespace package as gerbil-sse 2026-07-27 21:43:42 -07:00
gerbil.pkg namespace package as gerbil-sse 2026-07-27 21:43:42 -07:00
LICENSE add readme, license, gitignore 2026-07-27 18:42:51 -07:00
README.md Add call-with-sse-frame; build sse-send! on it 2026-10-22 15:30:43 -07:00
sse-test.ss Add call-with-sse-frame; build sse-send! on it 2026-10-22 15:30:43 -07:00
sse.ss Add call-with-sse-frame; build sse-send! on it 2026-10-22 15:30:43 -07:00

SSE Gerbil

A minimal Server-Sent Events transport for Gerbil v19's :std/net/http/server.

(import :gerbil-sse/sse)

The module exports four procedures:

  • call-with-sse begins an SSE response and manages its lifetime.
  • call-with-sse-frame calls a procedure with the response's BufferedWriter, then flushes: whatever it writes is one frame.
  • sse-send! writes and flushes one preframed SSE string.
  • sse? recognizes SSE sessions.
(def (events-handler request response)
  (call-with-sse
   response
   (lambda (session)
     (sse-send! session "event: ping\ndata: hello\n\n"))))

call-with-sse-frame writes a frame in pieces, for example bytes kept from earlier:

(call-with-sse-frame
 session
 (lambda (writer)
   (BufferedWriter-write writer event-header)   ; u8vectors
   (BufferedWriter-write writer cached-data)
   (BufferedWriter-write-string-utf8 writer "\n\n")))

The writer is valid only inside that procedure, which must not send on the same session. If it raises, the session fails as it does after a failed write: part of a frame may have been written, so nothing more is sent.

call-with-sse closes the response when its callback returns normally. Frames are serialized so concurrent sends cannot interleave. Event construction and application-level framing remain the caller's responsibility.

Development

gerbil build
gerbil test -v 0 ./sse-test.ss

License

MIT