improved example
This commit is contained in:
13
README.md
13
README.md
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
Yet another in-memory Go PubSub library.
|
Yet another in-memory Go PubSub library.
|
||||||
I started to develop what is now GubGub in one of my personal projects but I soon found myself using it in other completely unrelated projects and I thought it could be a nice thing to share.
|
I started to develop what is now GubGub in one of my personal projects but I soon found myself using it in other completely unrelated projects and I thought it could be a nice thing to share.
|
||||||
|
It might be very tailored to my personal preferences but my focus was concurrency safety and ease of usage.
|
||||||
|
|
||||||
## Getting started
|
## Getting started
|
||||||
|
|
||||||
@@ -26,15 +27,21 @@ type MyMessage struct {
|
|||||||
Name string
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func consumer(msg MyMessage) {
|
||||||
|
fmt.Printf("Hello %s", msg.Name)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
|
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
topic := gubgub.NewAsyncTopic[MyMessage](ctx)
|
topic := gubgub.NewAsyncTopic[MyMessage](ctx)
|
||||||
|
|
||||||
topic.Subscribe(gubgub.Forever(func(msg MyMessage) {
|
topic.Subscribe(gubgub.Forever(consumer))
|
||||||
fmt.Printf("Hello %s", msg.Name)
|
|
||||||
}))
|
// The AsyncTopic doesn't wait for the subscriber to be registered so, for the purposes of this
|
||||||
|
// example, we sleep on it.
|
||||||
|
time.Sleep(time.Millisecond)
|
||||||
|
|
||||||
topic.Publish(MyMessage{Name: "John Smith"})
|
topic.Publish(MyMessage{Name: "John Smith"})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user