Skip to main content

SUBSCRIBE

Syntax

SUBSCRIBE channel [channel ...]

Module

pubsub

Categories

pubsub connection slow

Description

Subscribe to one or more channels.

Examples

The Subscribe method returns a readMessage function. This method is lazy so it must be invoked each time the you want to read the next message from the channel. When subscribing to an'N' number of channels, the first N messages will be the subscription confimations. The readMessage functions returns a message object when called. The message object is a string slice with the following inforamtion: event type at index 0 (e.g. subscribe, message), channel name at index 1, message/subscription index at index 2.

db, err := sugardb.NewSugarDB()
if err != nil {
log.Fatal(err)
}
readMessage := db.Subscribe("subscribe_tag_1", "channel1", "channel2") // Return lazy readMessage function
for i := 0; i < 2; i++ {
message := readMessage() // Call the readMessage function for each channel subscription.
}