Server-Sent Events (SSE)
SSE allows servers to push data to clients over HTTP.
Basic Usage
r.GET("/events", func(c *rue.Context) {
client := rue.NewSSEClient(c)
defer client.Close()
for i := 0; i < 10; i++ {
client.SendEvent("update", fmt.Sprintf("Event %d", i))
time.Sleep(time.Second)
}
})
SSEClient Methods
| Method | Description |
|---|---|
SendEvent(event, data) | Send named event |
SendData(data) | Send data without event name |
SendComment(comment) | Send comment (keep-alive) |
Close() | Close the connection |
Client Example
const eventSource = new EventSource('/events');
eventSource.addEventListener('update', (e) => {
console.log('Update:', e.data);
});
eventSource.onerror = () => {
console.log('Connection lost');
};