Broadcaster provides simplest way of broadcasting in Golang.
See complete example for more detail.
type subscriber struct {
id string
}
func (s *subscriber) GetID() broadcast.ID {
return s.id
}
func (s *subscriber) Subscribe() error {
fmt.Println("subscribe:", s.id)
return nil
}
func (s *subscriber) Unsubscribe() error {
fmt.Println("unsubscribe:", s.id)
return nil
}
func main() {
hub := broadcast.New(
broadcast.Concurrency(10),
broadcast.AllowDuplicableID(true),
)
hub.Subscribe(&subscriber{id: "id1"})
hub.Subscribe(&subscriber{id: "id1"})
hub.Subscribe(&subscriber{id: "id2"})
hub.Subscribe(&subscriber{id: "id3"})
hub.Publish(func (s Subscriber) error {
fmt.Println("publish:", s.(*subscriber).id)
return nil
})
hub.PublishTo(func (s Subscriber) error {
fmt.Println("publish to:", s.(*subscriber).id)
return nil
}, "id1", "id3")
hub.Terminate()
}
// Output:
//
// subscribe: id1
// subscribe: id1
// subscribe: id2
// subscribe: id3
// publish: id1
// publish: id1
// publish: id2
// publish: id3
// publish to: id1
// publish to: id1
// publish to: id3
// unsubscribe: id1
// unsubscribe: id2
// unsubscribe: id3
Concurrency indicates the number of the workers when Publish method is executed. Default is 5.
If AllowDuplicableID is true and tries to set the same ID Subscribers, those are subscribed as slice to the same ID. If not, override a old Subscriber to new Subscriber.