Class SseConnection

java.lang.Object
io.karatelabs.http.SseConnection

public class SseConnection extends Object
Server-Sent Events connection wrapping a Netty channel. Sends the initial HTTP response headers with chunked transfer encoding, then allows sending individual SSE events as data frames.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Close the SSE connection by sending the last chunk and closing the channel.
    boolean
     
    void
    Register a callback for when the client disconnects.
    void
    Commit the SSE response head (200, text/event-stream) now.
    void
    reject(int status, String message)
    Reject this would-be SSE connection with a normal HTTP error response, before any event is sent — for an Accept: text/event-stream request that is not actually an event-source route (the MCP Streamable-HTTP spec wants 405 for a GET to a POST-only endpoint).
    void
    send(String data)
    Send an SSE event with data only (no event name).
    void
    send(String event, String data)
    Send an SSE event with the given event name and data.
    void
    Send an SSE comment (line starting with colon).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • open

      public void open()
      Commit the SSE response head (200, text/event-stream) now. Idempotent; auto-called on the first send/sendComment. Call it explicitly to open the stream before any event.
    • reject

      public void reject(int status, String message)
      Reject this would-be SSE connection with a normal HTTP error response, before any event is sent — for an Accept: text/event-stream request that is not actually an event-source route (the MCP Streamable-HTTP spec wants 405 for a GET to a POST-only endpoint). No-op once the stream has been opened.
    • send

      public void send(String event, String data)
      Send an SSE event with the given event name and data.
      Parameters:
      event - the event name (appears as "event: name" in the stream)
      data - the event data (appears as "data: ..." lines)
    • send

      public void send(String data)
      Send an SSE event with data only (no event name).
    • sendComment

      public void sendComment(String comment)
      Send an SSE comment (line starting with colon). Useful as a keep-alive.
    • close

      public void close()
      Close the SSE connection by sending the last chunk and closing the channel.
    • isOpen

      public boolean isOpen()
    • onDisconnect

      public void onDisconnect(Runnable callback)
      Register a callback for when the client disconnects.