Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📝 docs: add SSE example #1776

Merged
merged 1 commit into from Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions .github/README.md
Expand Up @@ -133,6 +133,7 @@ go get -u github.com/gofiber/fiber/v2
- [Rapid](https://dev.to/koddr/welcome-to-fiber-an-express-js-styled-fastest-web-framework-written-with-on-golang-497) server-side programming
- [Template engines](https://github.com/gofiber/template)
- [WebSocket support](https://github.com/gofiber/websocket)
- [Server-Sent events](https://github.com/gofiber/recipes/tree/master/sse)
- [Rate Limiter](https://docs.gofiber.io/api/middleware/limiter)
- Translated in [15 languages](https://docs.gofiber.io/)
- And much more, [explore Fiber](https://docs.gofiber.io/)
Expand Down Expand Up @@ -497,6 +498,47 @@ func main() {
}
```

### Server-Sent Events

📖 [More Info](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)

```go
import (
"github.com/gofiber/fiber/v2"
"github.com/valyala/fasthttp"
)

func main() {
app := fiber.New()

app.Get("/sse", func(c *fiber.Ctx) error {
c.Set("Content-Type", "text/event-stream")
c.Set("Cache-Control", "no-cache")
c.Set("Connection", "keep-alive")
c.Set("Transfer-Encoding", "chunked")

c.Context().SetBodyStreamWriter(fasthttp.StreamWriter(func(w *bufio.Writer) {
fmt.Println("WRITER")
var i int

for {
i++
msg := fmt.Sprintf("%d - the time is %v", i, time.Now())
fmt.Fprintf(w, "data: Message: %s\n\n", msg)
fmt.Println(msg)

w.Flush()
time.Sleep(5 * time.Second)
}
}))

return nil
})

log.Fatal(app.Listen(":3000"))
}
```

### Recover middleware

📖 [Recover](https://docs.gofiber.io/api/middleware/recover)
Expand Down
42 changes: 42 additions & 0 deletions .github/README_ckb.md
Expand Up @@ -130,6 +130,7 @@ go get -u github.com/gofiber/fiber/v2
- پڕۆگرامکردنی [خێرا](https://dev.to/koddr/welcome-to-fiber-an-express-js-styled-fastest-web-framework-written-with-on-golang-497)ی ڕاژە
- [داڕێژە](https://github.com/gofiber/template)
- پشتگیریی [WebSocket](https://github.com/gofiber/websocket)
- [Server-Sent events](https://github.com/gofiber/recipes/tree/master/sse)
- [Rate Limiter](https://docs.gofiber.io/api/middleware/limiter)
- وەرگێڕراوە بۆ [15 زمان](https://docs.gofiber.io/)
- زیاتریش، [فایبەر بپشکنە](https://docs.gofiber.io/)
Expand Down Expand Up @@ -497,6 +498,47 @@ func main() {
}
```

### Server-Sent Events

📖 [More Info](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)

```go
import (
"github.com/gofiber/fiber/v2"
"github.com/valyala/fasthttp"
)

func main() {
app := fiber.New()

app.Get("/sse", func(c *fiber.Ctx) error {
c.Set("Content-Type", "text/event-stream")
c.Set("Cache-Control", "no-cache")
c.Set("Connection", "keep-alive")
c.Set("Transfer-Encoding", "chunked")

c.Context().SetBodyStreamWriter(fasthttp.StreamWriter(func(w *bufio.Writer) {
fmt.Println("WRITER")
var i int

for {
i++
msg := fmt.Sprintf("%d - the time is %v", i, time.Now())
fmt.Fprintf(w, "data: Message: %s\n\n", msg)
fmt.Println(msg)

w.Flush()
time.Sleep(5 * time.Second)
}
}))

return nil
})

log.Fatal(app.Listen(":3000"))
}
```

### Recover middleware

📖 [Recover](https://docs.gofiber.io/api/middleware/recover)
Expand Down
42 changes: 42 additions & 0 deletions .github/README_de.md
Expand Up @@ -130,6 +130,7 @@ go get -u github.com/gofiber/fiber/v2
- [Schnelle](https://dev.to/koddr/welcome-to-fiber-an-express-js-styled-fastest-web-framework-written-with-on-golang-497) serverseitige Programmierung
- [Template engines](https://github.com/gofiber/template)
- [WebSocket support](https://github.com/gofiber/websocket)
- [Server-Sent events](https://github.com/gofiber/recipes/tree/master/sse)
- [Rate Limiter](https://docs.gofiber.io/api/middleware/limiter)
- Verfügbar in [15 Sprachen](https://docs.gofiber.io/)
- Und vieles mehr - [erkunde Fiber](https://docs.gofiber.io/)
Expand Down Expand Up @@ -492,6 +493,47 @@ func main() {
}
```

### Server-Sent Events

📖 [More Info](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)

```go
import (
"github.com/gofiber/fiber/v2"
"github.com/valyala/fasthttp"
)

func main() {
app := fiber.New()

app.Get("/sse", func(c *fiber.Ctx) error {
c.Set("Content-Type", "text/event-stream")
c.Set("Cache-Control", "no-cache")
c.Set("Connection", "keep-alive")
c.Set("Transfer-Encoding", "chunked")

c.Context().SetBodyStreamWriter(fasthttp.StreamWriter(func(w *bufio.Writer) {
fmt.Println("WRITER")
var i int

for {
i++
msg := fmt.Sprintf("%d - the time is %v", i, time.Now())
fmt.Fprintf(w, "data: Message: %s\n\n", msg)
fmt.Println(msg)

w.Flush()
time.Sleep(5 * time.Second)
}
}))

return nil
})

log.Fatal(app.Listen(":3000"))
}
```

### Recover middleware

📖 [Recover](https://docs.gofiber.io/api/middleware/recover)
Expand Down
42 changes: 42 additions & 0 deletions .github/README_es.md
Expand Up @@ -130,6 +130,7 @@ go get -u github.com/gofiber/fiber/v2
- Programación [rápida](https://dev.to/koddr/welcome-to-fiber-an-express-js-styled-fastest-web-framework-written-with-on-golang-497) del lado del servidor
- [Template engines](https://github.com/gofiber/template)
- [WebSocket support](https://github.com/gofiber/websocket)
- [Server-Sent events](https://github.com/gofiber/recipes/tree/master/sse)
- [Rate Limiter](https://docs.gofiber.io/api/middleware/limiter)
- Disponible en [15 idiomas](https://docs.gofiber.io/)
- Y mucho más, [explora Fiber](https://docs.gofiber.io/)
Expand Down Expand Up @@ -492,6 +493,47 @@ func main() {
}
```

### Server-Sent Events

📖 [More Info](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)

```go
import (
"github.com/gofiber/fiber/v2"
"github.com/valyala/fasthttp"
)

func main() {
app := fiber.New()

app.Get("/sse", func(c *fiber.Ctx) error {
c.Set("Content-Type", "text/event-stream")
c.Set("Cache-Control", "no-cache")
c.Set("Connection", "keep-alive")
c.Set("Transfer-Encoding", "chunked")

c.Context().SetBodyStreamWriter(fasthttp.StreamWriter(func(w *bufio.Writer) {
fmt.Println("WRITER")
var i int

for {
i++
msg := fmt.Sprintf("%d - the time is %v", i, time.Now())
fmt.Fprintf(w, "data: Message: %s\n\n", msg)
fmt.Println(msg)

w.Flush()
time.Sleep(5 * time.Second)
}
}))

return nil
})

log.Fatal(app.Listen(":3000"))
}
```

### Middleware de Recuperación

📖 [Recover](https://docs.gofiber.io/api/middleware/recover)
Expand Down
42 changes: 42 additions & 0 deletions .github/README_fa.md
Expand Up @@ -159,6 +159,7 @@ go get -u github.com/gofiber/fiber/v2
- برنامه نویسی سمت سرور [سریع](https://dev.to/koddr/welcome-to-fiber-an-express-js-styled-fastest-web-framework-written-with-on-golang-497)
- دارای [Template engines](https://github.com/gofiber/template) اختصاصی
- [پشتیبانی از وب سوکت](https://github.com/gofiber/websocket)
- [Server-Sent events](https://github.com/gofiber/recipes/tree/master/sse)
- قابلیت [Rate Limiter](https://docs.gofiber.io/api/middleware/limiter)
- ترجمه در [15 زبان](https://docs.gofiber.io/)
- و امکانات بیشتر, [دیدن در داکیومنت](https://docs.gofiber.io/)
Expand Down Expand Up @@ -594,6 +595,47 @@ func main() {
}
```

### Server-Sent Events

📖 [More Info](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)

```go
import (
"github.com/gofiber/fiber/v2"
"github.com/valyala/fasthttp"
)

func main() {
app := fiber.New()

app.Get("/sse", func(c *fiber.Ctx) error {
c.Set("Content-Type", "text/event-stream")
c.Set("Cache-Control", "no-cache")
c.Set("Connection", "keep-alive")
c.Set("Transfer-Encoding", "chunked")

c.Context().SetBodyStreamWriter(fasthttp.StreamWriter(func(w *bufio.Writer) {
fmt.Println("WRITER")
var i int

for {
i++
msg := fmt.Sprintf("%d - the time is %v", i, time.Now())
fmt.Fprintf(w, "data: Message: %s\n\n", msg)
fmt.Println(msg)

w.Flush()
time.Sleep(5 * time.Second)
}
}))

return nil
})

log.Fatal(app.Listen(":3000"))
}
```

</div>

### Recover middleware
Expand Down
42 changes: 42 additions & 0 deletions .github/README_fr.md
Expand Up @@ -130,6 +130,7 @@ go get -u github.com/gofiber/fiber/v2
- Programmation côté serveur [rapide](https://dev.to/koddr/welcome-to-fiber-an-express-js-styled-fastest-web-framework-written-with-on-golang-497)
- [Template engines](https://github.com/gofiber/template)
- [WebSocket support](https://github.com/gofiber/websocket)
- [Server-Sent events](https://github.com/gofiber/recipes/tree/master/sse)
- [Rate Limiter](https://docs.gofiber.io/api/middleware/limiter)
- Available in [15 languages](https://docs.gofiber.io/)
- Et plus encore, [explorez Fiber](https://docs.gofiber.io/)
Expand Down Expand Up @@ -494,6 +495,47 @@ func main() {
}
```

### Server-Sent Events

📖 [More Info](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)

```go
import (
"github.com/gofiber/fiber/v2"
"github.com/valyala/fasthttp"
)

func main() {
app := fiber.New()

app.Get("/sse", func(c *fiber.Ctx) error {
c.Set("Content-Type", "text/event-stream")
c.Set("Cache-Control", "no-cache")
c.Set("Connection", "keep-alive")
c.Set("Transfer-Encoding", "chunked")

c.Context().SetBodyStreamWriter(fasthttp.StreamWriter(func(w *bufio.Writer) {
fmt.Println("WRITER")
var i int

for {
i++
msg := fmt.Sprintf("%d - the time is %v", i, time.Now())
fmt.Fprintf(w, "data: Message: %s\n\n", msg)
fmt.Println(msg)

w.Flush()
time.Sleep(5 * time.Second)
}
}))

return nil
})

log.Fatal(app.Listen(":3000"))
}
```

### Recover middleware

📖 [Recover](https://docs.gofiber.io/api/middleware/recover)
Expand Down
46 changes: 46 additions & 0 deletions .github/README_he.md
Expand Up @@ -153,6 +153,7 @@ go get -u github.com/gofiber/fiber/v2
- תכנות [מהיר](https://dev.to/koddr/welcome-to-fiber-an-express-js-styled-fastest-web-framework-written-with-on-golang-497) של צד שרת
- [מנועי תבניות](https://docs.gofiber.io/middleware#template)
- [תמיכה ב-WebSocket](https://github.com/gofiber/websocket)
- [Server-Sent events](https://github.com/gofiber/recipes/tree/master/sse)
- [הגבלת קצבים ובקשות](https://docs.gofiber.io/api/middleware/limiter)
- Available in [12 languages](https://docs.gofiber.io/)
- והרבה יותר, [חקור את Fiber](https://docs.gofiber.io/)
Expand Down Expand Up @@ -592,6 +593,51 @@ func main() {

</div>

### Server-Sent Events

📖 [More Info](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)

<div dir="ltr">

```go
import (
"github.com/gofiber/fiber/v2"
"github.com/valyala/fasthttp"
)

func main() {
app := fiber.New()

app.Get("/sse", func(c *fiber.Ctx) error {
c.Set("Content-Type", "text/event-stream")
c.Set("Cache-Control", "no-cache")
c.Set("Connection", "keep-alive")
c.Set("Transfer-Encoding", "chunked")

c.Context().SetBodyStreamWriter(fasthttp.StreamWriter(func(w *bufio.Writer) {
fmt.Println("WRITER")
var i int

for {
i++
msg := fmt.Sprintf("%d - the time is %v", i, time.Now())
fmt.Fprintf(w, "data: Message: %s\n\n", msg)
fmt.Println(msg)

w.Flush()
time.Sleep(5 * time.Second)
}
}))

return nil
})

log.Fatal(app.Listen(":3000"))
}
```

</div>

### Middleware של התאוששות

📖 [התאוששות](https://docs.gofiber.io/api/middleware/recover)
Expand Down