Skip to content

hpidcock/go-pub-sub-channel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Pub Sub Channels

Build Status

Easily send messages between multiple go routines with error response handling.

Setup

rt := router.NewRouter()
defer rt.Close()

Subscribing

subChan := rt.Subscribe("channel id")
for {
    select {
    case msg, ok := <-subChan:
        if ok == false {
            // Either was closed from Unsubscribe call or
            // rt.Close()
            break
        }
    }
}

Publishing

err := rt.Publish(context.Background(), "channel_id", "Hello World!")
if err == router.ErrNotDelivered {
    // The message didn't get delivered to any subscribers.
} else if err == router.ErrTimedOut {
    // The message may have been delivered to some subscribers,
    // but the context timed out.
} else err != nil {
    // Errors passed back by one or more subscribers.
}

Unsubscribing

doneChan := rt.Unsubscribe("channel_id", subChan)
// Either wait for the doneChan to close
<-doneChan
// Or wait for the subChan to close

About

Send messages between goroutines

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages