Files

2.2 KiB

name, kind, lang, domain, version, purity, signature, description, tags, uses_functions, uses_types, returns, returns_optional, error_type, imports, params, output, tested, tests, test_file_path, file_path
name kind lang domain version purity signature description tags uses_functions uses_types returns returns_optional error_type imports params output tested tests test_file_path file_path
sse_send function go infra 1.0.0 impure func SSESend(w http.ResponseWriter, event SSEEvent) error Escribe un evento Server-Sent Events formateado al ResponseWriter segun la spec W3C y hace flush si el writer implementa http.Flusher. Campos opcionales (event, id, retry) solo se incluyen si tienen valor. Data con saltos de linea se traduce a multiples lineas data: segun la spec.
sse
server-sent-events
http
server
infra
realtime
SSEEvent_go_infra
false error_go_core
fmt
net/http
strings
name desc
w http.ResponseWriter destino. Debe tener headers SSE ya seteados (Content-Type: text/event-stream). Si implementa http.Flusher se hace flush tras la escritura.
name desc
event SSEEvent a serializar. Campos opcionales se omiten si estan vacios. Data multilinea se trocea en varias lineas data: segun la spec.
error si la escritura al ResponseWriter falla, nil si el evento se envio y se hizo flush correctamente true
serializa data simple sin event ni id
incluye event cuando esta presente
incluye id cuando esta presente
incluye retry cuando es positivo
data multilinea genera multiples lineas data:
hace flush si el writer es Flusher
functions/infra/sse_test.go functions/infra/sse_send.go

Ejemplo

http.HandleFunc("/events", func(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "text/event-stream")
    w.Header().Set("Cache-Control", "no-cache")
    w.Header().Set("Connection", "keep-alive")

    SSESend(w, SSEEvent{
        Event: "metric",
        ID:    "1",
        Data:  `{"cpu": 45.2}`,
    })
})

Notas

Funcion atomica: solo formatea y flushea un evento. La gestion del ciclo de vida (headers, loop de eventos, deteccion de desconexion) se hace en sse_handler.

Sigue estrictamente la spec W3C de Server-Sent Events. El doble salto de linea final separa eventos. Para enviar un comentario keepalive (no un evento) escribir : keepalive\n\n directamente — no usar esta funcion.