tree: 96e1620ea8d84050aa04e312f7014a8b54ec4530 [path history] [tgz]
  1. apiv1/
  2. internal/
  3. pscompat/
  4. admin.go
  5. admin_test.go
  6. CHANGES.md
  7. config.go
  8. config_test.go
  9. doc.go
  10. example_test.go
  11. go.mod
  12. go.sum
  13. go_mod_tidy_hack.go
  14. integration_test.go
  15. main_test.go
  16. README.md
pubsublite/README.md

Pub/Sub Lite Go Reference

This library is in BETA. Backwards-incompatible changes may be made before stable v1.0.0 is released.

Example Usage

import (
	"cloud.google.com/go/pubsub"
	"cloud.google.com/go/pubsublite/pscompat"
)

To publish messages to a topic:

// Create a PublisherClient for topic1 in zone us-central1-b.
// See https://cloud.google.com/pubsub/lite/docs/locations for available zones.
const topic = "projects/project-id/locations/us-central1-b/topics/topic1"
publisher, err := pscompat.NewPublisherClient(ctx, topic)
if err != nil {
	log.Fatal(err)
}

// Publish "hello world".
res := publisher.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)
}

To receive messages for a subscription:

// Create a SubscriberClient for subscription1 in zone us-central1-b.
const subscription = "projects/project-id/locations/us-central1-b/subscriptions/subscription1"
subscriber, err := pscompat.NewSubscriberClient(ctx, subscription)
if err != nil {
	log.Fatal(err)
}

// Use a callback to receive messages.
// Call cancel() to stop receiving messages.
cctx, cancel := context.WithCancel(ctx)
err = subscriber.Receive(cctx, 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)
}