From e1833df93c2e4be5e0dc42841c30d2c61b3e3c46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=2E=20Efe=20=C3=87etin?= Date: Mon, 14 Feb 2022 18:27:57 +0300 Subject: [PATCH] docs: add SSE example (#1776) --- .github/README.md | 42 +++++++++++++++++++++++++++++++++++++ .github/README_ckb.md | 42 +++++++++++++++++++++++++++++++++++++ .github/README_de.md | 42 +++++++++++++++++++++++++++++++++++++ .github/README_es.md | 42 +++++++++++++++++++++++++++++++++++++ .github/README_fa.md | 42 +++++++++++++++++++++++++++++++++++++ .github/README_fr.md | 42 +++++++++++++++++++++++++++++++++++++ .github/README_he.md | 46 +++++++++++++++++++++++++++++++++++++++++ .github/README_id.md | 42 +++++++++++++++++++++++++++++++++++++ .github/README_it.md | 42 +++++++++++++++++++++++++++++++++++++ .github/README_ja.md | 42 +++++++++++++++++++++++++++++++++++++ .github/README_ko.md | 42 +++++++++++++++++++++++++++++++++++++ .github/README_nl.md | 42 +++++++++++++++++++++++++++++++++++++ .github/README_pt.md | 42 +++++++++++++++++++++++++++++++++++++ .github/README_ru.md | 42 +++++++++++++++++++++++++++++++++++++ .github/README_sa.md | 46 +++++++++++++++++++++++++++++++++++++++++ .github/README_tr.md | 42 +++++++++++++++++++++++++++++++++++++ .github/README_zh-CN.md | 42 +++++++++++++++++++++++++++++++++++++ .github/README_zh-TW.md | 42 +++++++++++++++++++++++++++++++++++++ 18 files changed, 764 insertions(+) diff --git a/.github/README.md b/.github/README.md index 15e55489..e308147d 100644 --- a/.github/README.md +++ b/.github/README.md @@ -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/) @@ -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) diff --git a/.github/README_ckb.md b/.github/README_ckb.md index 22cf3eca..8492220b 100644 --- a/.github/README_ckb.md +++ b/.github/README_ckb.md @@ -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/) @@ -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) diff --git a/.github/README_de.md b/.github/README_de.md index aa9c1241..955ba862 100644 --- a/.github/README_de.md +++ b/.github/README_de.md @@ -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/) @@ -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) diff --git a/.github/README_es.md b/.github/README_es.md index 4a986cd3..fc274dd2 100644 --- a/.github/README_es.md +++ b/.github/README_es.md @@ -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/) @@ -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) diff --git a/.github/README_fa.md b/.github/README_fa.md index d7ffeafd..b9581dc0 100644 --- a/.github/README_fa.md +++ b/.github/README_fa.md @@ -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/) @@ -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")) +} +``` + ### Recover middleware diff --git a/.github/README_fr.md b/.github/README_fr.md index cc4973cb..e2803b6b 100644 --- a/.github/README_fr.md +++ b/.github/README_fr.md @@ -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/) @@ -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) diff --git a/.github/README_he.md b/.github/README_he.md index 2473a1bd..edddd1ec 100644 --- a/.github/README_he.md +++ b/.github/README_he.md @@ -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/) @@ -592,6 +593,51 @@ 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 של התאוששות 📖 [התאוששות](https://docs.gofiber.io/api/middleware/recover) diff --git a/.github/README_id.md b/.github/README_id.md index e60f8f25..05bbe780 100644 --- a/.github/README_id.md +++ b/.github/README_id.md @@ -130,6 +130,7 @@ go get -u github.com/gofiber/fiber/v2 - Kembangkan aplikasi dengan [Cepat](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) - [Mendukung 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) - Tersedia dalam [15 bahasa](https://docs.gofiber.io/) - Dan masih banyak lagi, [kunjungi Fiber](https://docs.gofiber.io/) @@ -495,6 +496,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) diff --git a/.github/README_it.md b/.github/README_it.md index 9bcf06b1..66a4ee70 100644 --- a/.github/README_it.md +++ b/.github/README_it.md @@ -130,6 +130,7 @@ go get -u github.com/gofiber/fiber/v2 - Programmazione server-side [veloce](https://dev.to/koddr/welcome-to-fiber-an-express-js-styled-fastest-web-framework-written-with-on-golang-497) - [Template engine](https://github.com/gofiber/template) - [Supporto 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) - Disponible in [15 lingue](https://docs.gofiber.io/) - E molto altro ancora, [esplora Fiber](https://docs.gofiber.io/) @@ -493,6 +494,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) diff --git a/.github/README_ja.md b/.github/README_ja.md index d936c9bb..818c6cfb 100644 --- a/.github/README_ja.md +++ b/.github/README_ja.md @@ -132,6 +132,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) - [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) - [15 ヶ国語](https://docs.gofiber.io/)で利用可能 - [Fiber](https://docs.gofiber.io/)をもっと知る @@ -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) diff --git a/.github/README_ko.md b/.github/README_ko.md index 4c57ff0f..2cf0ad30 100644 --- a/.github/README_ko.md +++ b/.github/README_ko.md @@ -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) 서버 사이드 프로그래밍 - [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/) - 더 알고 싶다면, [Fiber 둘러보기](https://docs.gofiber.io/) @@ -498,6 +499,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) diff --git a/.github/README_nl.md b/.github/README_nl.md index c52a1d25..39802cfa 100644 --- a/.github/README_nl.md +++ b/.github/README_nl.md @@ -130,6 +130,7 @@ go get -u github.com/gofiber/fiber/v2 - [Snelle](https://dev.to/koddr/welcome-to-fiber-an-express-js-styled-fastest-web-framework-written-with-on-golang-497) server-side programmering - [Template engines](https://github.com/gofiber/template) - [WebSocket ondersteuning](https://github.com/gofiber/websocket) +- [Server-Sent events](https://github.com/gofiber/recipes/tree/master/sse) - [Rate Limiter](https://docs.gofiber.io/middleware/limiter) - Vertaald in [15 talen](https://docs.gofiber.io/) - En nog veel meer, [ontdek Fiber](https://docs.gofiber.io/) @@ -498,6 +499,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) diff --git a/.github/README_pt.md b/.github/README_pt.md index 9cb4aa7a..82fb9d2f 100644 --- a/.github/README_pt.md +++ b/.github/README_pt.md @@ -130,6 +130,7 @@ go get -u github.com/gofiber/fiber/v2 - Programação [rápida](https://dev.to/koddr/welcome-to-fiber-an-express-js-styled-fastest-web-framework-written-with-on-golang-497) de aplicações de servidor - [Templates](https://github.com/gofiber/template) - [Suporte à WebSockets](https://github.com/gofiber/websocket) +- [Server-Sent events](https://github.com/gofiber/recipes/tree/master/sse) - [Limitador de requisições](https://docs.gofiber.io/api/middleware/limiter) - Disponível em [15 línguas](https://docs.gofiber.io/) - E muito mais, [explore o Fiber](https://docs.gofiber.io/) @@ -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 Recover 📖 [Recover](https://docs.gofiber.io/api/middleware/recover) diff --git a/.github/README_ru.md b/.github/README_ru.md index 022ca676..414a3d93 100644 --- a/.github/README_ru.md +++ b/.github/README_ru.md @@ -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) программирование на стороне сервера - [Template engines](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/) - И многое другое, [посетите наш Wiki](https://docs.gofiber.io/) @@ -498,6 +499,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) diff --git a/.github/README_sa.md b/.github/README_sa.md index 68772b1d..bc7802d4 100644 --- a/.github/README_sa.md +++ b/.github/README_sa.md @@ -143,6 +143,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) server-side programming - [Template engines](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/) - وأكثر بكثير, [استكشف Fiber](https://docs.gofiber.io/) @@ -556,6 +557,51 @@ 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) diff --git a/.github/README_tr.md b/.github/README_tr.md index fc59090e..4731d025 100644 --- a/.github/README_tr.md +++ b/.github/README_tr.md @@ -130,6 +130,7 @@ go get -u github.com/gofiber/fiber/v2 - [Hızlı](https://dev.to/koddr/welcome-to-fiber-an-express-js-styled-fastest-web-framework-written-with-on-golang-497) sunucu taraflı programlama - [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) - [15 dilde](https://docs.gofiber.io/) mevcut - Ve daha fazlası, [Fiber'ı keşfet](https://docs.gofiber.io/) @@ -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 Kurtarıcısı 📖 [Recover](https://docs.gofiber.io/api/middleware/recover) diff --git a/.github/README_zh-CN.md b/.github/README_zh-CN.md index 3c940763..9bcd7fa7 100644 --- a/.github/README_zh-CN.md +++ b/.github/README_zh-CN.md @@ -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) - [频率限制](https://docs.gofiber.io/api/middleware/limiter) - [15 种语言](https://docs.gofiber.io/) - 更多请[探索文档](https://docs.gofiber.io/) @@ -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")) +} +``` + ### 恢复(panic)中间件 📖 [Recover](https://docs.gofiber.io/api/middleware/recover) diff --git a/.github/README_zh-TW.md b/.github/README_zh-TW.md index 1315f3cf..7967cfba 100644 --- a/.github/README_zh-TW.md +++ b/.github/README_zh-TW.md @@ -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) - 支援[限速](https://docs.gofiber.io/api/middleware/limiter) - 被翻譯成[15種語言](https://docs.gofiber.io/) - 豐富的[文件](https://docs.gofiber.io/) @@ -495,6 +496,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 中介器 📖 [Recover](https://docs.gofiber.io/api/middleware/recover)