diff --git a/api/notification/live.go b/api/notification/live.go new file mode 100644 index 00000000..4f028e66 --- /dev/null +++ b/api/notification/live.go @@ -0,0 +1,31 @@ +package notification + +import ( + "github.com/0xJacky/Nginx-UI/internal/notification" + "github.com/0xJacky/Nginx-UI/model" + "github.com/gin-gonic/gin" + "io" +) + +func Live(c *gin.Context) { + c.Header("Content-Type", "text/event-stream") + c.Header("Cache-Control", "no-cache") + c.Header("Connection", "keep-alive") + + evtChan := make(chan *model.Notification) + + notification.SetClient(c, evtChan) + + notify := c.Writer.CloseNotify() + go func() { + <-notify + notification.RemoveClient(c) + }() + + for n := range evtChan { + c.Stream(func(w io.Writer) bool { + c.SSEvent("message", n) + return false + }) + } +} diff --git a/api/notification/router.go b/api/notification/router.go index 87c3d166..fc5caa29 100644 --- a/api/notification/router.go +++ b/api/notification/router.go @@ -7,4 +7,6 @@ func InitRouter(r *gin.RouterGroup) { r.GET("notifications/:id", Get) r.DELETE("notifications/:id", Destroy) r.DELETE("notifications", DestroyAll) + + r.GET("notifications/live", Live) } diff --git a/app/package.json b/app/package.json index c4cb7cda..17203839 100644 --- a/app/package.json +++ b/app/package.json @@ -38,6 +38,7 @@ "pinia-plugin-persistedstate": "^4.1.2", "reconnecting-websocket": "^4.4.0", "sortablejs": "^1.15.3", + "sse.js": "^2.5.0", "universal-cookie": "^7.2.2", "unocss": "^0.63.6", "vite-plugin-build-id": "0.5.0", diff --git a/app/pnpm-lock.yaml b/app/pnpm-lock.yaml index 6c3106c0..b703590b 100644 --- a/app/pnpm-lock.yaml +++ b/app/pnpm-lock.yaml @@ -83,6 +83,9 @@ importers: sortablejs: specifier: ^1.15.3 version: 1.15.3 + sse.js: + specifier: ^2.5.0 + version: 2.5.0 universal-cookie: specifier: ^7.2.2 version: 7.2.2 @@ -4271,6 +4274,9 @@ packages: spdx-license-ids@3.0.20: resolution: {integrity: sha512-jg25NiDV/1fLtSgEgyvVyDunvaNHbuwF9lfNV17gSmPFAlYzdfNBlLtLzXTevwkPj7DhGbmN9VnmJIgLnhvaBw==} + sse.js@2.5.0: + resolution: {integrity: sha512-I7zYndqOOkNpz9KIdFZ8c8A7zs1YazNewBr8Nsi/tqThfJkVPuP1q7UE2h4B0RwoWZxbBYpd06uoW3NI3SaZXg==} + stable-hash@0.0.4: resolution: {integrity: sha512-LjdcbuBeLcdETCrPn9i8AYAZ1eCtu4ECAWtP7UleOiZ9LzVxRzzUZEoZ8zB24nhkQnDWyET0I+3sWokSDS3E7g==} @@ -9690,6 +9696,8 @@ snapshots: spdx-license-ids@3.0.20: {} + sse.js@2.5.0: {} + stable-hash@0.0.4: {} std-env@3.7.0: {} diff --git a/app/src/components/Notification/Notification.vue b/app/src/components/Notification/Notification.vue index 49ae62d0..c88f1045 100644 --- a/app/src/components/Notification/Notification.vue +++ b/app/src/components/Notification/Notification.vue @@ -1,22 +1,71 @@