Skip to content

SSE Example

Server-Sent Events example demonstrating ctx.sse() for real-time streaming.

Endpoints

EndpointDescription
GET /api/v1/events/clockSends a tick event every second with the current time
GET /api/v1/events/counterSends incrementing count events every 500ms with event IDs
GET /api/v1/events/notificationsSends mock notification events every 3s, accepts ?userId=

Running

bash
cd examples/sse-api
kick dev

Testing with curl

bash
curl -N http://localhost:3000/api/v1/events/clock

Testing with JavaScript

js
const source = new EventSource('http://localhost:3000/api/v1/events/clock')

source.addEventListener('tick', (event) => {
  console.log(JSON.parse(event.data))
})

Source

Released under the MIT License.