-
Notifications
You must be signed in to change notification settings - Fork 189
feature(server) : add callback function for register and un-register #133
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
Conversation
|
Hey @panapol-p , Thanks for submitting the PR and apologies for missing issue #132 ! The changes look really good, happy to merge them once the method names have been changed 😄 |
|
Hi, @purehyperbole thank for your commented i will change it. |
|
@panapol-p thanks for making the changes. I think that's a good suggestion. Seems like if that's information that you're after, it may be best to define the func onSubscribe(streamID string, sub *Subscriber) {
...
}and add extend |
|
thank you for your suggestion. I think onSubscriber should look like this func onSubscribe(sub *Subscriber) {
...
}stream-id no need to add in this method, it will be not easy if we use more than 1 stream channel in 1 url path for this problem I add stream-id in to func onSubscribe(sub *Subscriber) {
switch(sub.streamID){
case ... : ...
}
}and good to include |
|
@panapol-p yep, I think that approach looks good! |
|
So sorry i'm so confuse before. |
[revise] subscribe and unsubscribe method on server side
|
Hi @purehyperbole example server (get client name from query parameter that was include into subscriber) c := make(map[string]int32)
onSubscribe := func(streamID string, sub *sse.Subscriber) {
c[streamID]++
clientName := sub.URL.Query().Get("name")
fmt.Printf("\U0001F7E2 [channel : %s] client [%s] is subscribed (%d registered)\n", streamID, clientName, c[streamID])
}
onUnsubscribe := func(streamID string, sub *sse.Subscriber) {
c[streamID]--
clientName := sub.URL.Query().Get("name")
fmt.Printf("🔴 [channel : %s] client [%s] is unsubscribed (%d registered)\n", streamID, clientName, c[streamID])
}
server := sse.NewWithCallback(onSubscribe, onUnsubscribe)
server.AutoStream = true
server.AutoReplay = false
// Create a new Mux and set the handler
mux := http.NewServeMux()
mux.HandleFunc("/events", server.ServeHTTP)
go func() {
for {
time.Sleep(2 * time.Second)
server.Publish("ch1", &sse.Event{
Data: []byte("this is channel 1"),
})
server.Publish("ch2", &sse.Event{
Data: []byte("this is channel 2"),
})
}
}()
http.ListenAndServe(":8081", mux)
example client (we put client name into query parameter) channelPtr := flag.String("channel", "test", "channel id")
namePtr := flag.String("name", "test", "client name")
flag.Parse()
c := sse.NewClient("http://localhost:8081/events?name=" + *namePtr)
err := c.Subscribe(*channelPtr, func(msg *sse.Event) {
fmt.Println(string(msg.Data))
})
if err != nil {
log.Fatalln(err.Error())
}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm! 🎉
|
Changes look great. I will merge and release these changes now. Thanks again for submitting this feature 😄 |

this change makes callback function to support when client register or un-register SSE
issue : #132
example server
example client