tree: 7a721e2499a2f053891dad8324ec9fc80316bd0b [path history] [tgz]
  1. CHANGES.md
  2. README.md
  3. apiv1/
  4. debug.go
  5. doc.go
  6. example_subscription_iterator_test.go
  7. example_test.go
  8. example_topic_iterator_test.go
  9. flow_controller.go
  10. flow_controller_test.go
  11. go.mod
  12. go.sum
  13. go_mod_tidy_hack.go
  14. integration_test.go
  15. internal/
  16. iterator.go
  17. iterator_test.go
  18. loadtest/
  19. message.go
  20. mock_test.go
  21. nodebug.go
  22. pstest/
  23. pstest_test.go
  24. pubsub.go
  25. pullstream.go
  26. pullstream_test.go
  27. service.go
  28. snapshot.go
  29. streaming_pull_test.go
  30. subscription.go
  31. subscription_test.go
  32. timeout_test.go
  33. topic.go
  34. topic_test.go
  35. 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)
}