-
-
Notifications
You must be signed in to change notification settings - Fork 24
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
Not quite understanding OnSession (program crashes) #49
Comments
Okay, I just found out I need to set the I was confused because the API in https://github.com/tmaxmax/go-sse/blob/master/server.go didn't seem to match what I was seeing, but I found out that the current latest version (0.10.0) differs (https://github.com/tmaxmax/go-sse/blob/v0.10.0/server.go). This leads me to the question if this library was actually stable enough to use it in production? The API still seems to change a lot as it seems. |
Answering my own question here regarding stability, since I just encountered #50:
As a result, I can, unfortunately, currently not use this library. Since there is really no good alternatives, I am back to implementing SSE on my own, as I have already done several times. I was hoping this library could make the implementation a bit better, and not homegrown, however to me it currently does not seem stable enough. Fair enough tho the version number is 0.x.x, so this is probably to be expected. |
Hi there and thank you again for the issues you've opened.
The change the next version brings is meant precisely to eliminate the confusion $ go get github.com/tmaxmax/go-sse@master This will give you the new API. Code on
I don't exactly understand what you mean here when you say "typed"/"untyped". Do you mean the If you mean that you don't need topics in this manner but instead each user has an individual, personalized event stream, sending messages to individual connections has been discussed in #36. I've provided a solution there which didn't seem fit for the other person asking but it might work for you. I've yet to thoroughly look into this, though I believe that what I've proposed there should work, albeit with a less intuitive usage.
Sending a welcome message to connections is addressed in #31. You can expand on that using ideas from #36 to send a personalized message to each user. To answer your other concerns:
This is true. There is no v1 yet so every minor version bump brings in breaking changes. The library strictly follows semantic versioning, so if only the patch version is bumped then there aren't any breaking changes. If I do breaking changes, I usually change one single component in a version, so that users of the library don't have to face too many changes at once.
As of now on the
When it comes to user/programmer errors, it's close to impossible to predict all mistaken usages one might come up with. There are various panics which, on wrong usage, occur outside the library's execution scope, so it would be impossible to catch those panics. What the library can do is implement intuitive and well documented APIs which effectively do not permit misusage, and where it can't do that to properly validate the invariants which are in the library's own scope of execution. I agree that in your situation above it would've been nice to receive some message to indicate that no Hopefully my standpoint makes sense. What is important is that in the development of Again, if you think you've found a bug, a reproducible example helps tremendously.
This is awesome! I should be able then to further assist you on your use case. Helping with concrete code in #50 and maybe a short description of what you're actually trying to achieve should give me enough input to propose a solution or find a fix for you. I'm very eager to help you further or fix anything wrong you've discovered and I'm sure we can find a way to neatly integrate |
Yeah well this was a bit misleading. Yes I mean messages without event type, and only
I have to partly disagree. For one, it should not panic. While this is also done in a few other libraries - the more usual case seems to be that libraries don't panic during runtime in go. This might be a bit subject to personal preference - I prefer non-panicking / non throwing libraries in general - if it must panic for whatever reason, it must be clearly stated in the API docs, so that users can handle this case properly. But still, to me, panic + recover is not a common usage scenario in Go libraries. But I was not talking about explicit
Well, as stated, create a server with 2 endpoints, one receives the SSE-stream, the other triggers |
Point taken, that one would have been preventable with a bit of validation code. I'll keep this in mind.
So I've created the following server: package main
import (
"log/slog"
"net/http"
"time"
"github.com/tmaxmax/go-sse"
)
func main() {
s := &sse.Server{
Logger: func(*http.Request) *slog.Logger {
return slog.Default()
},
}
mux := http.NewServeMux()
mux.Handle("GET /events", s)
mux.HandleFunc("POST /update", func(http.ResponseWriter, *http.Request) {
m := &sse.Message{}
m.AppendData(time.Now().String())
_ = s.Publish(m)
})
if err := http.ListenAndServe(":8000", mux); err != nil {
slog.Default().Error("listen and serve", "err", err)
}
} which, excluding the logging code, is the most basic version that satisfies your description, and the following client: package main
import (
"fmt"
"net/http"
"os"
"github.com/tmaxmax/go-sse"
)
func main() {
r, _ := http.NewRequest(http.MethodGet, "http://localhost:8000/events", http.NoBody)
conn := sse.NewConnection(r)
conn.SubscribeMessages(func(event sse.Event) {
fmt.Printf("%s\n\n", event.Data)
})
if err := conn.Connect(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
} (it's basically the From two distinct command lines, I'm executing the following command in a loop: $ curl -X POST http://localhost:8000/update with a I've connected three distinct clients to the server. After six minutes of running, the program still behaves normally and events are published and received without issue. I've also sometimes disconnected one client to see whether the server breaks but it is well behaved. I've also removed the I'm sure that my code and testing method is different from yours. If you could share your code and precisely state the steps to reproduce it (i.e. what exactly is the server setup code, how did you connect to the server, how did you spam the endpoint) to see what happens there it would be tremendously helpful. |
I am trying to use this library, however I am having a bit of a hard time understanding the concepts. First of all, the examples are outdated. https://github.com/tmaxmax/go-sse/blob/master/cmd/complex/main.go#L36 seems to use deprecated APIs, and not the APIs advised here: https://pkg.go.dev/github.com/tmaxmax/go-sse#Server
Then, I want to publish an initial message to every client connecting, and I thought the place for this would be
OnSession
, like this:While I do get the initial message just fine, the first attempt to call
sseServer.Publish(ev)
crashes the program:If I remove the
OnSession
parameter, thesseServer.Publish(ev)
will work just fine. So I am not sure how I would set upOnSession
correctly to avoid the crash. In any case, the library should have proper error handling here.I don't want to use subscriptions for typed messages, in fact, I only need to send untyped messages. So I actually don't need all the topic & subscription logic. How do I have to use your library?
The text was updated successfully, but these errors were encountered: