Skip to content

Latest commit

 

History

History
76 lines (66 loc) · 2.01 KB

README.md

File metadata and controls

76 lines (66 loc) · 2.01 KB

Cloud Pub/Sub Lite GoDoc

This library is in ALPHA. 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"
	"cloud.google.com/go/pubsublite/ps"
)

To publish messages to a topic:

// Create a PublisherClient for topic1.
// See https://cloud.google.com/pubsub/lite/docs/locations for available zones.
topic := pubsublite.TopicPath{
    Project: "project-id",
    Zone:    "us-central1-b",
    TopicID: "topic1",
}
publisher, err := ps.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.
subscription := pubsublite.SubscriptionPath{
    Project:        "project-id",
    Zone:           "us-central1-b",
    SubscriptionID: "subscription1",
}
subscriber, err := ps.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)
}