mirror of https://github.com/tidwall/tile38.git
6a55c8de8f | ||
---|---|---|
.. | ||
apiv1 | ||
internal/distribution | ||
CHANGES.md | ||
LICENSE | ||
README.md | ||
debug.go | ||
doc.go | ||
flow_controller.go | ||
go.mod | ||
go.sum | ||
go_mod_tidy_hack.go | ||
iterator.go | ||
message.go | ||
nodebug.go | ||
pubsub.go | ||
pullstream.go | ||
service.go | ||
snapshot.go | ||
subscription.go | ||
topic.go | ||
trace.go |
README.md
Cloud Pub/Sub
Example Usage
First create a pubsub.Client
to use throughout your application:
client, err := pubsub.NewClient(ctx, "project-id")
if err != nil {
log.Fatal(err)
}
Then use the client to publish and subscribe:
// Publish "hello world" on topic1.
topic := client.Topic("topic1")
res := topic.Publish(ctx, &pubsub.Message{
Data: []byte("hello world"),
})
// The publish happens asynchronously.
// Later, you can get the result from res:
...
msgID, err := res.Get(ctx)
if err != nil {
log.Fatal(err)
}
// Use a callback to receive messages via subscription1.
sub := client.Subscription("subscription1")
err = sub.Receive(ctx, func(ctx context.Context, m *pubsub.Message) {
fmt.Println(m.Data)
m.Ack() // Acknowledge that we've consumed the message.
})
if err != nil {
log.Println(err)
}