> For the complete documentation index, see [llms.txt](https://coldbox.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://coldbox.ortusbooks.com/the-basics/routing/routing-dsl/sse-routes.md).

# Streaming Routes (SSE)

Terminate a route with a Server-Sent Events stream instead of a normal response. BoxLang only.

{% hint style="warning" %}
🚀 **BoxLang Exclusive** — `toSSE()` requires **BoxLang**. It is not available on CFML engines.
{% endhint %}

`toSSE()` terminates a route by handing it over to a [Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) stream instead of a normal request/response cycle.

```javascript
route( "/events/heartbeat" ).toSSE( ( event, rc, prc, emitter ) => {
    while ( emitter.isOpen() ) {
        emitter.send( { "ts" : now() }, "heartbeat" );
        sleep( 5000 );
    }
} );
```

The callback receives the usual `event`, `rc`, `prc` - plus an **`emitter`**:

| Method                            | Description                                                           |
| --------------------------------- | --------------------------------------------------------------------- |
| `emitter.isOpen()`                | `false` once the client disconnects - use it to end your loop         |
| `emitter.send( data, event, id )` | Send one SSE frame. `data` can be simple or complex (auto-serialized) |
| `emitter.comment( text )`         | Send a raw comment line - useful for manual keep-alives               |
| `emitter.close()`                 | Gracefully end the stream                                             |

Once `toSSE()` takes over, ColdBox rendering is suppressed for that request, any event-cache entry is discarded, and the flash scope is **not** auto-saved - a stream can stay open for minutes, so persisting flash values on it would leak into an unrelated later request.

## When You Have Both a JSON and a Streaming Representation

Use `toSSE()` only for endpoints that *always* stream. If a resource sometimes streams and sometimes responds normally, leave the route pointing at a handler and branch inside the action instead:

```javascript
route( "/reports/:id" ).toHandler( "reports" );
```

```javascript
// handlers/Reports.cfc
function show( event, rc, prc ){
    if ( event.wantsSSE() ) {
        return event.sse( ( emitter ) => {
            // ... stream it
        } );
    }
    // ... normal response
}
```

## Configuring Defaults

Stream-wide defaults (keep-alive interval, reconnect hint, CORS) come from the `sse` settings block and can be overridden per-call on `event.sse()`. See the request context's `sse()` method for the full set of options.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://coldbox.ortusbooks.com/the-basics/routing/routing-dsl/sse-routes.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
