1# Server-Sent Events
2
3[![GoDoc](https://godoc.org/github.com/gin-contrib/sse?status.svg)](https://godoc.org/github.com/gin-contrib/sse)
4[![Build Status](https://travis-ci.org/gin-contrib/sse.svg)](https://travis-ci.org/gin-contrib/sse)
5[![codecov](https://codecov.io/gh/gin-contrib/sse/branch/master/graph/badge.svg)](https://codecov.io/gh/gin-contrib/sse)
6[![Go Report Card](https://goreportcard.com/badge/github.com/gin-contrib/sse)](https://goreportcard.com/report/github.com/gin-contrib/sse)
7
8Server-sent events (SSE) is a technology where a browser receives automatic updates from a server via HTTP connection. The Server-Sent Events EventSource API is [standardized as part of HTML5[1] by the W3C](http://www.w3.org/TR/2009/WD-eventsource-20091029/).
9
10- [Read this great SSE introduction by the HTML5Rocks guys](http://www.html5rocks.com/en/tutorials/eventsource/basics/)
11- [Browser support](http://caniuse.com/#feat=eventsource)
12
13## Sample code
14
15```go
16import "github.com/gin-contrib/sse"
17
18func httpHandler(w http.ResponseWriter, req *http.Request) {
19	// data can be a primitive like a string, an integer or a float
20	sse.Encode(w, sse.Event{
21		Event: "message",
22		Data:  "some data\nmore data",
23	})
24
25	// also a complex type, like a map, a struct or a slice
26	sse.Encode(w, sse.Event{
27		Id:    "124",
28		Event: "message",
29		Data: map[string]interface{}{
30			"user":    "manu",
31			"date":    time.Now().Unix(),
32			"content": "hi!",
33		},
34	})
35}
36```
37```
38event: message
39data: some data\\nmore data
40
41id: 124
42event: message
43data: {"content":"hi!","date":1431540810,"user":"manu"}
44
45```
46
47## Content-Type
48
49```go
50fmt.Println(sse.ContentType)
51```
52```
53text/event-stream
54```
55
56## Decoding support
57
58There is a client-side implementation of SSE coming soon.
59