| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/server.R |
| 3 |
\name{handler_stream} |
| 4 |
\alias{handler_stream} |
| 5 |
\title{Create HTTP Streaming Handler} |
| 6 |
\usage{ |
| 7 |
handler_stream(path, on_request, on_close = NULL, method = "*", prefix = FALSE) |
| 8 |
} |
| 9 |
\arguments{ |
| 10 |
\item{path}{URI path to match (e.g., "/stream").} |
| 11 |
|
| 12 |
\item{on_request}{Function called when a request arrives. Signature: |
| 13 |
\verb{function(conn, req)} where \code{conn} is the connection object and \code{req} |
| 14 |
is a list with \code{method}, \code{uri}, \code{headers}, \code{body}.} |
| 15 |
|
| 16 |
\item{on_close}{[default NULL] Function called when the connection closes. |
| 17 |
Signature: \verb{function(conn)}} |
| 18 |
|
| 19 |
\item{method}{[default "*"] HTTP method to match (e.g., "GET", "POST"). |
| 20 |
Use \code{"*"} to match any method.} |
| 21 |
|
| 22 |
\item{prefix}{[default FALSE] Logical, if TRUE matches path as a prefix.} |
| 23 |
} |
| 24 |
\value{ |
| 25 |
A handler object for use with \code{\link[=http_server]{http_server()}}. |
| 26 |
} |
| 27 |
\description{ |
| 28 |
Creates an HTTP streaming handler using chunked transfer encoding. Supports |
| 29 |
any streaming HTTP protocol including Server-Sent Events (SSE), newline- |
| 30 |
delimited JSON (NDJSON), and custom streaming formats. |
| 31 |
} |
| 32 |
\details{ |
| 33 |
HTTP streaming uses chunked transfer encoding (RFC 9112). The first |
| 34 |
\verb{$send()} triggers writing of HTTP headers with \code{Transfer-Encoding: chunked}. |
| 35 |
Headers cannot be modified after the first send. |
| 36 |
|
| 37 |
Set an appropriate \code{Content-Type} header for your streaming format: |
| 38 |
\itemize{ |
| 39 |
\item NDJSON: \code{application/x-ndjson} |
| 40 |
\item JSON stream: \code{application/stream+json} |
| 41 |
\item SSE: \code{text/event-stream} (see \code{\link[=format_sse]{format_sse()}}) |
| 42 |
\item Plain text: \code{text/plain} |
| 43 |
} |
| 44 |
|
| 45 |
\strong{SSE Reconnection:} When an SSE client reconnects after a disconnect, it |
| 46 |
sends a \code{Last-Event-ID} header containing the last event ID it received. |
| 47 |
Access this via \code{req$headers["Last-Event-ID"]} in \code{on_request} to resume |
| 48 |
the event stream from the correct position. |
| 49 |
|
| 50 |
To broadcast to multiple clients, store connection objects in a list and |
| 51 |
iterate over them (e.g., \code{lapply(conns, function(c) c$send(data))}). |
| 52 |
} |
| 53 |
\section{Connection Object}{ |
| 54 |
|
| 55 |
The \code{conn} object passed to callbacks has these methods: |
| 56 |
\itemize{ |
| 57 |
\item \code{conn$send(data)}: Send data chunk to client. |
| 58 |
\item \code{conn$close()}: Close the connection (sends terminal chunk). |
| 59 |
\item \code{conn$set_status(code)}: Set HTTP status code (before first send). |
| 60 |
\item \code{conn$set_header(name, value)}: Set response header (before first send). |
| 61 |
\item \code{conn$id}: Unique connection identifier. |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
\examples{ |
| 66 |
# NDJSON streaming endpoint |
| 67 |
h <- handler_stream("/stream", function(conn, req) { |
| 68 |
conn$set_header("Content-Type", "application/x-ndjson") |
| 69 |
conn$send('{"status":"connected"}\n') |
| 70 |
}) |
| 71 |
|
| 72 |
# SSE endpoint with reconnection support |
| 73 |
h <- handler_stream("/events", function(conn, req) { |
| 74 |
conn$set_header("Content-Type", "text/event-stream") |
| 75 |
conn$set_header("Cache-Control", "no-cache") |
| 76 |
last_id <- req$headers["Last-Event-ID"] |
| 77 |
# Resume from last_id if client is reconnecting |
| 78 |
conn$send(format_sse(data = "connected", id = "1")) |
| 79 |
}) |
| 80 |
|
| 81 |
# Long-lived streaming with broadcast triggered by POST |
| 82 |
conns <- list() |
| 83 |
handlers <- list( |
| 84 |
handler_stream("/stream", |
| 85 |
on_request = function(conn, req) { |
| 86 |
conn$set_header("Content-Type", "application/x-ndjson") |
| 87 |
conns[[as.character(conn$id)]] <<- conn |
| 88 |
conn$send('{"status":"connected"}\n') |
| 89 |
}, |
| 90 |
on_close = function(conn) { |
| 91 |
conns[[as.character(conn$id)]] <<- NULL |
| 92 |
} |
| 93 |
), |
| 94 |
# POST endpoint triggers broadcast to all streaming clients |
| 95 |
handler("/broadcast", function(req) { |
| 96 |
msg <- paste0('{"msg":"', rawToChar(req$body), '"}\n') |
| 97 |
lapply(conns, function(c) c$send(msg)) |
| 98 |
list(status = 200L, body = "sent") |
| 99 |
}, method = "POST") |
| 100 |
) |
| 101 |
|
| 102 |
} |
| 103 |
\seealso{ |
| 104 |
\code{\link[=format_sse]{format_sse()}} for formatting Server-Sent Events. |
| 105 |
} |