tree: 57ab989eff78db807b5d8de5d109944f62b4d1ce [path history] [tgz]
  1. README.md
  2. apiv1/
  3. debug.go
  4. doc.go
  5. example_subscription_iterator_test.go
  6. example_test.go
  7. example_topic_iterator_test.go
  8. flow_controller.go
  9. flow_controller_test.go
  10. integration_test.go
  11. internal/
  12. iterator.go
  13. iterator_test.go
  14. loadtest/
  15. message.go
  16. mock_test.go
  17. nodebug.go
  18. pstest/
  19. pstest_test.go
  20. pubsub.go
  21. pullstream.go
  22. pullstream_test.go
  23. service.go
  24. snapshot.go
  25. streaming_pull_test.go
  26. subscription.go
  27. subscription_test.go
  28. timeout_test.go
  29. topic.go
  30. topic_test.go
  31. trace.go
pubsub/README.md

Cloud Pub/Sub GoDoc

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)
}