> 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
