I am trying to setup a basic stream between my Golang server and VueJS. I followed another post on StackOverflow to get started. However, for some odd reason when I check my console in chrome the output is continuously repeated (0, 1, 2, 3, 4 -short stop- 0, 1, 2, 3, 4 -short stop- etc..).
Here's my code
main.go
package main
import (
"io"
"time"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/stream", func(c *gin.Context) {
chanStream := make(chan int, 2)
go func() {
defer close(chanStream)
for i := 0; i < 5; i++ {
chanStream <- i
time.Sleep(time.Second * 1)
}
}()
c.Stream(func(w io.Writer) bool {
if msg, ok := <-chanStream; ok {
c.SSEvent("message", msg)
return true
}
return false
})
})
r.StaticFile("/", "./public.html")
r.Use(static.Serve("/", static.LocalFile("./public.html", true)))
r.Run()
}
public.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
var stream = new EventSource("/stream");
stream.addEventListener("message", function(e){
console.log(e.data);
});
</script>
</body>
</html>
I'm new to SSE & Vue but I thought the client waits for a response from the server. My expectation is that, once the Gin stream ends, the client just keeps waiting and doesn't do anything until I do EventSource.close(). The output seems like the server sends the response normally but the client keeps making a request once the stream ends? I'm not sure. Can someone point out what I'm doing wrong? Thanks