alexandrevicenzi / go-sse Goto Github PK
View Code? Open in Web Editor NEWServer-Sent Events for Go
License: MIT License
Server-Sent Events for Go
License: MIT License
Greetings
Is it possible to send an event to a newly connected client?
Does this package automatically send messages to a client based on their last event id or is that up to the person using this package?
I am attempting to figure out how to implement this, but am a little unclear about it.
Refer to echo discussion.
Consider changing
//closeNotify := response.(http.CloseNotifier).CloseNotify()
closeNotify := request.Context().Done()
Can i change the retry value of message?
Hi, newbie here. At the given example, it pushes tha data every 5sec. How can can I make the SSE push only when there's a POST(Hey subscribers, new data is coming!) method that kicks. Because I don't think it is wise to keep emitting even there's no new data.
Thank you in advance.
Hello!!!
Try using go-see with go-chi as a router by changing:
http.Handle("/events/", s)
by:
r.Mount("/events/", s)
but I get the following error:
panic: interface conversion: *middleware.compressResponseWriter is not http.CloseNotifier: missing method CloseNotify
...
.../github.com/alexandrevicenzi/go-sse/sse.go:82 +0x4c4
I hope you can help me
Is there any way I can use it with http/2?
With [email protected]
and when testing publishing from multiple browsers with race detector running, encountered race conditions.
WARNING: DATA RACE
Write at 0x00c000456258 by goroutine 68:
github.com/alexandrevicenzi/go-sse.(*Channel).SendMessage()
/home/fat/go/pkg/mod/github.com/alexandrevicenzi/[email protected]/channel.go:26 +0x87
github.com/alexandrevicenzi/go-sse.(*Server).SendMessage()
/home/fat/go/pkg/mod/github.com/alexandrevicenzi/[email protected]/sse.go:117 +0x219
projects/lib/channels.GenChannel.func1()
/home/fat/projects/lib/channels/channels.go:121 +0x377
Previous write at 0x00c000456258 by goroutine 61:
github.com/alexandrevicenzi/go-sse.(*Channel).SendMessage()
/home/fat/go/pkg/mod/github.com/alexandrevicenzi/[email protected]/channel.go:26 +0x87
github.com/alexandrevicenzi/go-sse.(*Server).SendMessage()
/home/fat/go/pkg/mod/github.com/alexandrevicenzi/[email protected]/sse.go:117 +0x219
projects/lib/channels.GenChannel.func1()
/home/fat/projects/lib/channels/channels.go:121 +0x377
Sorry, don't have the dump handy but had another data race on msg.retry write,
Line 94 in fcf9dce
(which is being fed by SSE.SendMessage(chName, sse.NewMessage(strconv.Itoa(time.Now().Second()), string(msg), "msg"))
)
I'm using some complex code as well, so it's possible that I'm not 'doing it right', but these two data races under load seem to point at these two locations (channel.go:26 and sse.go:94)
Greetings
Is it possible to use this library with Labstatck's Echo framework?
TIA
$ go run -race simple.go
2018/04/05 16:27:34 Listening at :3000
load: 2.81 cmd: simple 85940 running 0.01u 0.05s
==================
WARNING: DATA RACE
Write at 0x00c4200a4c90 by goroutine 6:
runtime.mapassign_faststr()
/Users/flowchartsman/opt/go/src/runtime/hashmap_fast.go:694 +0x0
github.com/alexandrevicenzi/go-sse.(*Server).dispatch()
/Users/flowchartsman/gopath/src/github.com/alexandrevicenzi/go-sse/sse.go:178 +0xe58
Previous read at 0x00c4200a4c90 by goroutine 8:
runtime.mapaccess2_faststr()
/Users/flowchartsman/opt/go/src/runtime/hashmap_fast.go:261 +0x0
github.com/alexandrevicenzi/go-sse.(*Server).SendMessage()
/Users/flowchartsman/gopath/src/github.com/alexandrevicenzi/go-sse/sse.go:100 +0x1e9
main.main.func2()
/Users/flowchartsman/gopath/src/github.com/alexandrevicenzi/go-sse/examples/simple.go:30 +0x74
Goroutine 6 (running) created at:
github.com/alexandrevicenzi/go-sse.NewServer()
/Users/flowchartsman/gopath/src/github.com/alexandrevicenzi/go-sse/sse.go:35 +0x237
main.main()
/Users/flowchartsman/gopath/src/github.com/alexandrevicenzi/go-sse/examples/simple.go:13 +0x3b
Goroutine 8 (running) created at:
main.main()
/Users/flowchartsman/gopath/src/github.com/alexandrevicenzi/go-sse/examples/simple.go:26 +0x156
==================
A casual inspection reveals racey map access in sse.go:
} else if ch, ok := s.channels[channel]; ok { //this cannot be done concurrently
if s.Debug {
log.Printf("go-sse: message sent to channel '%s'.", ch.name)
}
ch.SendMessage(message)
In a for loop, I am consuming a lot of stock data streams and publishing it in SSE. However upon checking the browser with the received SSE messages, they are received in batch, not pure real-time.
Is there any settings to tweak to make it work?
Hello, when using the Shutdown
method, I get a panic:
panic: send on closed channel
This is because the removeClient
channel is closed before all HTTP handlers got closed.
I am a beginner, but to me it looks like we should never close removeClient
.
But it will allow a goroutine leak, since goroutines trying to send on this channel will remain blocked.
I'll try to post a PR.
$ gofmt -l *.go
channel.go
client.go
message.go
message_test.go
options.go
options_test.go
sse.go
Hello,
I'm testing with gin and when r.Use(gzip.Gzip(gzip.DefaultCompression))
middleware is enabled, the messages are not displaied by browser console.
If I understand correctly, gzipping data breaks SSE api but I'm wondering if there is a work around to have best of both worlds?
While library outputs are welcomed during development, they are annoying and useless in production.
Maybe conditional logging could be implemented to overcome this problem?
I have a situation where multiple tabs are communicating with the same server individually.
How do I make sure that each tab gets the right responses? The only thing I know is a dynamically generated tab id.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.