https://github.com/eighty4/sse
https://github.com/eighty4/sse
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/eighty4/sse
- Owner: eighty4
- License: bsd-2-clause
- Created: 2019-07-12T21:20:45.000Z (almost 7 years ago)
- Default Branch: main
- Last Pushed: 2021-12-05T00:24:47.000Z (over 4 years ago)
- Last Synced: 2025-05-18T17:52:59.133Z (about 1 year ago)
- Language: Go
- Size: 5.86 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://pkg.go.dev/github.com/eighty4/sse)
## SSE Upgrade
This library exports a func that upgrades http requests to an event streaming connection by sending a `Content-Type: text/event-stream` header to the client.
```go
http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) {
var connection *sse.Connection
connection, _ = sse.Upgrade(w, r)
connection.SendString("thanks for connecting. here's some data.")
connection.Close()
})
```
Connection's `SendBytes([]byte)`, `SendString(string)` and `SendJson(interface{})` funcs will format and send data to the client.
Use `Close()` when you're done streaming event data.
A connection's BuildMessage() func can be used to send a payload with the id and event attributes.
```go
connection.BuildMessage().WithId("id").WithEvent("event").SendString("data")
```
This message will be sent to the client:
```text
id: id
event: event
data: data
```
### Example
```go
package main
import (
"github.com/eighty4/sse"
"log"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
connection, err := sse.Upgrade(w, r)
if err != nil {
w.WriteHeader(500)
log.Println("sse upgrade error", err.Error())
return
}
connection.BuildMessage().WithEvent("auth").SendString("token")
connection.Close()
}
func main() {
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
```
#### Original Repository
I found originating source for sse.go on GitHub a couple of years ago, but I couldn't find the repository to reference when publishing updates.