README.md (1744B)
1 # Server-Sent Events 2 3 [](https://godoc.org/github.com/gin-contrib/sse) 4 [](https://travis-ci.org/gin-contrib/sse) 5 [](https://codecov.io/gh/gin-contrib/sse) 6 [](https://goreportcard.com/report/github.com/gin-contrib/sse) 7 8 Server-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 16 import "github.com/gin-contrib/sse" 17 18 func 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 ``` 38 event: message 39 data: some data\\nmore data 40 41 id: 124 42 event: message 43 data: {"content":"hi!","date":1431540810,"user":"manu"} 44 45 ``` 46 47 ## Content-Type 48 49 ```go 50 fmt.Println(sse.ContentType) 51 ``` 52 ``` 53 text/event-stream 54 ``` 55 56 ## Decoding support 57 58 There is a client-side implementation of SSE coming soon.