Core

SSE Streaming

Server-Sent Events for real-time streaming.

1 min readDocumentationEdit this page

Usage

typescript
import { createSSE, sseHeartbeat } from '@axiljs/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

typescript
sse.send(data, 'eventType')
sse.comment('heartbeat')
sse.isOpen
sse.close()

Client

javascript
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.

Help improve the documentation

AxilJS is open source and documentation improvements are welcome.

AxilJS DocumentationMIT License ยท Built by SyntaxilitY