Sse

From G1Joshi/Agent-Skills · View interactive version

Sse is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert Server-Sent Events (SSE) assistance covering HTTP streaming, EventSource API, unidirectional live updates, reconnection handling, and text/event-stream semantics. Use when streaming LLM completions, delivering live notifications, or building real-time dashboards without WebSockets.

How to use this skill

Skill file

Server-Sent Events (SSE)

SSE allow a web page to get updates from a server. Unlike WebSockets, SSEs are unidirectional (Server -> Client) and use standard HTTP.

When to Use

Quick Start

// Client
const evtSource = new EventSource("/api/events");

evtSource.onmessage = (event) => {
  const data = JSON.parse(event.data);
  console.log("Update:", data);
};
// Server (Express)
app.get("/api/events", (req, res) => {
  // Headers to keep connection open
  res.setHeader("Content-Type", "text/event-stream");
  res.setHeader("Cache-Control", "no-cache");
  res.setHeader("Connection", "keep-alive");

  setInterval(() => {
    // Format: "data: <payload>\n\n"
    res.write(`data: ${JSON.stringify({ time: new Date() })}\n\n`);
  }, 1000);
});

Core Concepts

`text/event-stream` Protocol Format

Data is pushed over an open HTTP connection formatted in plain text blocks ending with double newlines:

HTTP/1.1 200 OK
Content-Type: text/event-stream
Cache-Control: no-cache
Connection: keep-alive

id: 101
event: price_update
data: {"symbol": "NVDA", "price": 128.50}

id: 102
event: price_update
data: {"symbol": "AAPL", "price": 224.10}

Browser `EventSource` Client API

Standard browser API with built-in automatic reconnection handling and event dispatching:

// Client-side subscription
const eventSource = new EventSource("/api/live-stream");

eventSource.addEventListener("price_update", (e) => {
  const data = JSON.parse(e.data);
  console.log("Updated price:", data.symbol, data.price);
});

eventSource.onerror = (err) => {
  console.error("SSE stream disconnected. Browser will auto-reconnect.", err);
};

Resumable Streams with `Last-Event-ID`

When reconnections occur, the browser automatically transmits the last received ID so servers can replay missed events:

// Server resumes from last known event ID
app.get("/api/live-stream", (req, res) => {
  const lastEventId = req.headers["last-event-id"];
  if (lastEventId) {
    const missedEvents = eventLog.getSince(Number(lastEventId));
    missedEvents.forEach((e) =>
      res.write(`id: ${e.id}\ndata: ${JSON.stringify(e)}\n\n`),
    );
  }
});

Common Patterns

LLM Token Streaming via SSE

Problem: Waiting for complete LLM responses takes seconds; users need instant incremental token streaming.

Solution: Stream chunks formatted as Server-Sent Events.

app.get("/api/stream", async (req, res) => {
  res.setHeader("Content-Type", "text/event-stream");
  res.setHeader("Cache-Control", "no-cache");
  res.setHeader("Connection", "keep-alive");

  const stream = await openai.chat.completions.create({
    model: "gpt-4o",
    messages: [{ role: "user", content: "Write a short poem" }],
    stream: true,
  });

  for await (const chunk of stream) {
    const text = chunk.choices[0]?.delta?.content || "";
    res.write(`data: ${JSON.stringify({ text })}\n\n`);
  }
  res.write("event: done\ndata: {}\n\n");
  res.end();
});

Best Practices

Do:

`) every 15-30 seconds to prevent aggressive firewall timeouts.

Don't:

Troubleshooting

| Error | Cause | Solution |

| :------------------ | :-------------------------------- | :------------------------------------------------- |

| Buffered Response | Nginx/Proxy buffering the stream. | Disable proxy buffering (X-Accel-Buffering: no). |

| Cors Error | Standard CORS rules apply. | Configure Access-Control headers. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.