# Server-Sent Events

**Content-Type:&#x20;*****\****

A server-sent event is when a web page automatically gets updates from a server.

This was also possible before, but the web page would have to ask if any updates were available. With server-sent events, the updates come automatically.

Examples: Facebook/Twitter updates, stock price updates, news feeds, sport results, etc. <https://www.w3schools.com/htmL/html5\\_serversentevents.asp>

## Receive Server-Sent Event Notifications

The EventSource object is used to receive server-sent event notifications:

```js
var source = new EventSource("http://localhost:8080/handler");
source.onmessage = function(event) {
  document.getElementById("result").innerHTML += event.data + "<br>";
};
```

### Check Server-Sent Events Support

```js
if(typeof(EventSource) !== "undefined") {
    // Yes! Server-sent events support!
    // Some code.....
} else {
    // Sorry! No server-sent events support..
}
```

## Server-Side Code Example

The server-side event stream syntax is simple. Set the `"Content-Type"` header to `"text/event-stream"`. Now you can start sending event streams. The response messages starts with `"data: "` and ends with `"\n\n"`.

```go
func handler(ctx iris.Context) {
    // It's always recommended to first check for `Flusher`
    // compatibility in Go, you do that with:
	flusher, ok := ctx.ResponseWriter().Flusher()
	if !ok {
		ctx.StopWithText(iris.StatusHTTPVersionNotSupported, "Streaming unsupported!")
		return
    }

    ctx.ContentType("text/event-stream")
    ctx.Header("Cache-Control", "no-cache")

    now := time.Now()
    ctx.Writef("data: The server time is: %s\n\n", now)

    // Flush the data immediately instead of buffering it for later.
    flusher.Flush()
}
```

* Set the `"Content-Type"` header to `"text/event-stream"`
* Specify that the page should not cache
* Output the data to send (**Always** start with `"data: "`)
* `Flush` the output data back to the web page


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://iris-go.gitbook.io/iris/responses/sse.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
