onSubscribed() event handler doesn't work properly
Hudayberdyyev opened this issue · 2 comments
Hudayberdyyev commented
My config file:
token_hmac_secret_key: "bbe7d157-a253-4094-9759-06a8236543f9"
admin: true
admin_password: "d0683813-0916-4c49-979f-0e08a686b727"
admin_secret: "4e9eafcf-0120-4ddd-b668-8dc40072c78e"
api_key: "d7627bb6-2292-4911-82e1-615c0ed3eebb"
allowed_origins: ["*"]
allow_subscribe_for_client: true
allow_publish_for_subscriber: true
allow_history_for_client: true
allow_history_for_anonymous: true
force_recovery: true
history_size: 500
history_ttl: "1800s"
presence: true
My code below:
package main
import (
"github.com/centrifugal/centrifuge-go"
"github.com/dgrijalva/jwt-go"
"log"
"time"
)
func generateToken(username string) string {
// TODO: Expose values to constants
claims := jwt.MapClaims{
"sub": username,
//"testkey": "testvalue",
}
t, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString([]byte("bbe7d157-a253-4094-9759-06a8236543f9"))
if err != nil {
log.Println(err)
}
return t
}
func getSocketClient(username string) *centrifuge.Client {
client := centrifuge.NewJsonClient(
"ws://localhost:8000/connection/websocket",
centrifuge.Config{
Token: generateToken(username),
},
)
if err := client.Connect(); err != nil {
panic(err)
}
return client
}
func main() {
mercuryClient := getSocketClient("mercury")
saturnClient := getSocketClient("saturn")
// Mercury subscription
mercurySub, err := mercuryClient.NewSubscription("test", centrifuge.SubscriptionConfig{JoinLeave: true})
if err != nil {
panic(err)
}
mercurySub.OnLeave(func(event centrifuge.LeaveEvent) {
log.Printf("mercury on leave fired\n")
})
mercurySub.OnJoin(func(event centrifuge.JoinEvent) {
log.Printf("mercury on join fired\n")
})
mercurySub.OnSubscribed(func(event centrifuge.SubscribedEvent) {
log.Printf("mercury on subscribed fired")
})
mercurySub.OnSubscribing(func(event centrifuge.SubscribingEvent) {
log.Printf("mercury on subscribing fired")
})
err = mercurySub.Subscribe()
if err != nil {
panic(err)
}
time.Sleep(2 * time.Second)
//Saturn subscription
saturnSub, err := saturnClient.NewSubscription("test", centrifuge.SubscriptionConfig{JoinLeave: true})
if err != nil {
panic(err)
}
saturnSub.OnLeave(func(event centrifuge.LeaveEvent) {
log.Printf("saturn on leave fired\n")
})
saturnSub.OnJoin(func(event centrifuge.JoinEvent) {
log.Printf("saturn on join fired\n")
})
saturnSub.OnSubscribed(func(event centrifuge.SubscribedEvent) {
log.Printf("saturn on subscribed fired")
})
saturnSub.OnSubscribing(func(event centrifuge.SubscribingEvent) {
log.Printf("saturn on subscribing fired")
})
err = saturnSub.Subscribe()
if err != nil {
panic(err)
}
select {}
}
May be there is my fault. But I need your help please.
That's my backend application. I want to know when new client subscribed to particular channel. In the example above i create two separate centrifugo clients with different usernames. then i try to subscribe for test channel in both of them. But I didn't got information about subscribing of second client.
How I can achive desired result ? Is there any mistake ?
FZambia commented
Hello, to make it work you may add two options to server config:
join_leave: true
allow_presence_for_client: true
This will enable join/leave messages emitting on the server and will allow clients to ask for join/leave messages to be pushed into subscription.
Hudayberdyyev commented
It works, thanks a lot for your help !