Skip to content

Commit

Permalink
Merge pull request alexandrevicenzi#29 from responsibility-act/master
Browse files Browse the repository at this point in the history
Add Iris (go module) example
  • Loading branch information
alexandrevicenzi authored Jan 27, 2021
2 parents 386fb71 + b5e5d86 commit c17ce60
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions _examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ Examples on how to use `go-sse` with multiple libraries and frameworks.
| [echo](https://github.com/labstack/echo) || [echo.go](https://github.com/alexandrevicenzi/go-sse/blob/master/_examples/echo.go) |
| [fasthttp](https://github.com/valyala/fasthttp) || |
| [gin](https://github.com/gin-gonic/gin) || [gin.go](https://github.com/alexandrevicenzi/go-sse/blob/master/_examples/gin.go) |
| [iris](https://github.com/kataras/iris) || [iris/main.go](https://github.com/alexandrevicenzi/go-sse/blob/master/_examples/iris/main.go) |
| [gorilla/mux](https://github.com/gorilla/mux) || [gorilla.go](https://github.com/alexandrevicenzi/go-sse/blob/master/_examples/gorilla.go) |
| `net/http` || [net_http.go](https://github.com/alexandrevicenzi/go-sse/blob/master/_examples/net_http.go) or [net_http_options.go](https://github.com/alexandrevicenzi/go-sse/blob/master/_examples/net_http_options.go) |
8 changes: 8 additions & 0 deletions _examples/iris/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module iris_example

go 1.15

require (
github.com/alexandrevicenzi/go-sse v1.6.0
github.com/kataras/iris/v12 v12.2.0-alpha2.0.20210127020946-ce25e698f8f8
)
57 changes: 57 additions & 0 deletions _examples/iris/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"log"
"net/http"
"os"
"strconv"
"time"

"github.com/alexandrevicenzi/go-sse"
"github.com/kataras/iris/v12"
)

func main() {
s := sse.NewServer(&sse.Options{
// Increase default retry interval to 10s.
RetryInterval: 10 * 1000,
// CORS headers
Headers: map[string]string{
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, OPTIONS",
"Access-Control-Allow-Headers": "Keep-Alive,X-Requested-With,Cache-Control,Content-Type,Last-Event-ID",
},
// Custom channel name generator
ChannelNameFunc: func(request *http.Request) string {
return request.URL.Path
},
// Print debug info
Logger: log.New(os.Stdout, "go-sse: ", log.Ldate|log.Ltime|log.Lshortfile),
})

defer s.Shutdown()

app := iris.New()
app.Get("/", func(ctx iris.Context) {
ctx.ServeFile("../static/index.html")
})
app.Get("/events/{channel}", iris.FromStd(s))

go func() {
for {
s.SendMessage("/events/channel-1", sse.SimpleMessage(time.Now().Format("2006/02/01/ 15:04:05")))
time.Sleep(5 * time.Second)
}
}()

go func() {
i := 0
for {
i++
s.SendMessage("/events/channel-2", sse.SimpleMessage(strconv.Itoa(i)))
time.Sleep(5 * time.Second)
}
}()

app.Listen(":3000")
}

0 comments on commit c17ce60

Please sign in to comment.