> For the complete documentation index, see [llms.txt](https://calliopeide.gitbook.io/calliopeide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://calliopeide.gitbook.io/calliopeide/3.-integration-features/3.2-real-time-communication.md).

# 3.2 Real-time Communication

The system implements real-time communication through Server-Sent Events:

```python
@app.route("/", methods=["GET"])
def send_query():
    data = request.args.get("data", "")
    global output, user_input, stop_stream, input_requested
    input_requested = False
    stop_stream = False
    output.clear()
    user_input = data

    def generate():
        global stop_stream, input_requested
        last_length = 0
        while True:
            if len(output) > last_length:
                for x in output[last_length:]:
                    yield f"data: {json.dumps({'type': 'output', 'data': x})}\n\n".encode()
                last_length = len(output)
```

Features:

1. Event Streaming: Implements SSE
2. State Management: Tracks output state
3. Error Handling: Manages stream errors
4. Response Formatting: Formats JSON responses
5. Connection Management: Handles client connections
