SSE Streaming
Server-Sent Events for real-time streaming.
1 min readEdit this page
Usage
import { createSSE, sseHeartbeat } from '@restjs/core'
app.get('/events', (req, res) => {
const sse = createSSE(res)
const stop = sseHeartbeat(sse, 15000)
sse.send({ message: 'Connected' }, 'connect')
const interval = setInterval(() => {
if (!sse.isOpen) { clearInterval(interval); stop(); return }
sse.send({ time: Date.now() }, 'update')
}, 1000)
})API
sse.send(data, 'eventType')
sse.comment('heartbeat')
sse.isOpen
sse.close()Client
const source = new EventSource('/events')
source.addEventListener('update', (e) => {
console.log(JSON.parse(e.data))
})Note
SSE is used by the AI platform for streaming chat and agent responses.