Skip to content

Instantly share code, notes, and snippets.

@domino14
Created July 22, 2020 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save domino14/23fbc98229566d7356159c830e3096d6 to your computer and use it in GitHub Desktop.
Save domino14/23fbc98229566d7356159c830e3096d6 to your computer and use it in GitHub Desktop.
Process pubsub messages
// subchans is a map[string]chan *nats.Msg
func (h *Hub) PubsubProcess() {
for {
select {
case msg := <-h.pubsub.subchans["lobby.>"]:
h.sendToRealm(LobbyRealm, msg.Data)
case msg := <-h.pubsub.subchans["user.>"]:
// Send the message to every socket belonging to this user.
subtopics := strings.Split(msg.Subject, ".")
userID := subtopics[1]
for client := range h.clientsByUserID[userID] {
client.send <- msg.Data
}
case msg := <-h.pubsub.subchans["gametv.>"]:
// A gametv message is meant for people who are observing a user's games.
log.Debug().Str("topic", msg.Subject).Msg("got gametv message, forwarding along")
subtopics := strings.Split(msg.Subject, ".")
gameID := subtopics[1]
h.sendToRealm(Realm("gametv-"+gameID), msg.Data)
// Handle more messages here...
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment