SSE Streaming — Real-Time Event Stream Integration | DeFiMara

What Is SSE Streaming?

DeFiMara uses Server-Sent Events (SSE) to stream agent responses in real time. Instead of waiting for the full response, you receive a continuous stream of events: thinking steps, tool calls, intermediate results, and the final answer — as they happen.

How It Works

1. POST /chat with stream: true. 2. Each line is prefixed with "data: " — parse the JSON payload to get the event type and content. 3. Event types: token (text chunk), tool_call, tool_result, done, error. 4. Close the connection when you receive a done event.

Requirements

HTTP client with streaming support — Python requests with stream=True, or JavaScript fetch with ReadableStream. Valid JWT in Authorization header. Process lines as they arrive, do not buffer the full response.

Back to Blog
API
November 20255 min read

SSE Streaming

What Is SSE Streaming?

Defimara uses Server-Sent Events (SSE) to stream agent responses in real time. Instead of waiting for the full response, you receive a continuous stream of events: thinking steps, tool calls, intermediate results, and the final answer - as they happen.

SSE is a one-way, server-to-client stream over a standard HTTP connection. No WebSocket setup required - works with any HTTP client.

• How It Works

1
POST /chat (streaming)
Send your message with stream: true. The connection stays open and events arrive line by line.
2
Parse event lines
Each line is prefixed with "data: ". Parse the JSON payload to get the event type and content.
3
Handle event types
Events include: token (text chunk), tool_call, tool_result, done, and error.
4
Close on done
When you receive a "done" event the stream ends. Close the connection.

• Requirements

HTTP client with streaming support (requests with stream=True, fetch with ReadableStream, etc.)
Valid JWT in Authorization header
Do not buffer the response - process lines as they arrive
Reconnect logic recommended for long-running agent sessions

• Code Example

Python
import requests, json

with requests.post(
    "http://localhost:8000/chat",
    headers={"Authorization": f"Bearer {token}"},
    json={"message": "swap 10 USDC for ETH", "stream": True},
    stream=True
) as r:
    for line in r.iter_lines():
        if line.startswith(b"data: "):
            event = json.loads(line[6:])
            if event["type"] == "token":
                print(event["content"], end="", flush=True)
            elif event["type"] == "done":
                break

Share this article