Documentation ¶
Overview ¶
Package pubsub contains a Google Cloud Pub/Sub client.
More information about Google Cloud Pub/Sub is available on https://cloud.google.com/pubsub/docs
Example (Auth) ¶
package main import ( "io/ioutil" "log" "camlistore.org/third_party/golang.org/x/oauth2" "camlistore.org/third_party/golang.org/x/oauth2/google" "camlistore.org/third_party/google.golang.org/cloud" "camlistore.org/third_party/google.golang.org/cloud/pubsub" ) func main() context.Context { // Initialize an authorized context with Google Developers Console // JSON key. Read the google package examples to learn more about // different authorization flows you can use. // http://godoc.org/golang.org/x/oauth2/google jsonKey, err := ioutil.ReadFile("/path/to/json/keyfile.json") if err != nil { log.Fatal(err) } conf, err := google.JWTConfigFromJSON( jsonKey, pubsub.ScopeCloudPlatform, pubsub.ScopePubSub, ) if err != nil { log.Fatal(err) } ctx := cloud.NewContext("project-id", conf.Client(oauth2.NoContext)) // See the other samples to learn how to use the context. return ctx }
Output:
Index ¶
- Constants
- func Ack(ctx context.Context, sub string, id ...string) error
- func CreateSub(ctx context.Context, name string, topic string, deadline time.Duration, ...) error
- func CreateTopic(ctx context.Context, name string) error
- func DeleteSub(ctx context.Context, name string) error
- func DeleteTopic(ctx context.Context, name string) error
- func ModifyAckDeadline(ctx context.Context, sub string, deadline time.Duration) error
- func ModifyPushEndpoint(ctx context.Context, sub, endpoint string) error
- func Publish(ctx context.Context, topic string, msgs ...*Message) ([]string, error)
- func SubExists(ctx context.Context, name string) (bool, error)
- func TopicExists(ctx context.Context, name string) (bool, error)
- type Message
Examples ¶
Constants ¶
const ( // ScopePubSub grants permissions to view and manage Pub/Sub // topics and subscriptions. ScopePubSub = "https://www.googleapis.com/auth/pubsub" // ScopeCloudPlatform grants permissions to view and manage your data // across Google Cloud Platform services. ScopeCloudPlatform = "https://www.googleapis.com/auth/cloud-platform" )
Variables ¶
This section is empty.
Functions ¶
func CreateSub ¶
func CreateSub(ctx context.Context, name string, topic string, deadline time.Duration, endpoint string) error
CreateSub creates a Pub/Sub subscription on the backend. A subscription should subscribe to an existing topic.
The messages that haven't acknowledged will be pushed back to the subscription again when the default acknowledgement deadline is reached. You can override the default deadline by providing a non-zero deadline. Deadline must not be specified to precision greater than one second.
As new messages are being queued on the subscription, you may recieve push notifications regarding to the new arrivals. To receive notifications of new messages in the queue, specify an endpoint callback URL. If endpoint is an empty string the backend will not notify the client of new messages.
If the subscription already exists an error will be returned.
func CreateTopic ¶
CreateTopic creates a new topic with the specified name on the backend. It will return an error if topic already exists.
func DeleteTopic ¶
DeleteTopic deletes the specified topic.
func ModifyAckDeadline ¶
ModifyAckDeadline modifies the acknowledgement deadline for the messages retrieved from the specified subscription. Deadline must not be specified to precision greater than one second.
func ModifyPushEndpoint ¶
ModifyPushEndpoint modifies the URL endpoint to modify the resource to handle push notifications coming from the Pub/Sub backend for the specified subscription.
func Publish ¶
Publish publish messages to the topic's subscribers. It returns message IDs upon success.
Example ¶
package main import ( "io/ioutil" "log" "camlistore.org/third_party/golang.org/x/net/context" "camlistore.org/third_party/golang.org/x/oauth2" "camlistore.org/third_party/golang.org/x/oauth2/google" "camlistore.org/third_party/google.golang.org/cloud" "camlistore.org/third_party/google.golang.org/cloud/pubsub" ) func Example_auth() context.Context { jsonKey, err := ioutil.ReadFile("/path/to/json/keyfile.json") if err != nil { log.Fatal(err) } conf, err := google.JWTConfigFromJSON( jsonKey, pubsub.ScopeCloudPlatform, pubsub.ScopePubSub, ) if err != nil { log.Fatal(err) } ctx := cloud.NewContext("project-id", conf.Client(oauth2.NoContext)) return ctx } func main() { ctx := Example_auth() msgIDs, err := pubsub.Publish(ctx, "topic1", &pubsub.Message{ Data: []byte("hello world"), }) if err != nil { log.Fatal(err) } log.Printf("Published a message with a message id: %s\n", msgIDs[0]) }
Output:
Types ¶
type Message ¶
type Message struct { // ID identifies this message. ID string // AckID is the identifier to acknowledge this message. AckID string // Data is the actual data in the message. Data []byte // Labels represents the key-value pairs the current message // is labelled with. Labels map[string]string }
Message represents a Pub/Sub message.
func Pull ¶
Pull pulls messages from the subscription. It returns up to n number of messages, and n could not be larger than 100.
Example ¶
package main import ( "io/ioutil" "log" "camlistore.org/third_party/golang.org/x/net/context" "camlistore.org/third_party/golang.org/x/oauth2" "camlistore.org/third_party/golang.org/x/oauth2/google" "camlistore.org/third_party/google.golang.org/cloud" "camlistore.org/third_party/google.golang.org/cloud/pubsub" ) func Example_auth() context.Context { jsonKey, err := ioutil.ReadFile("/path/to/json/keyfile.json") if err != nil { log.Fatal(err) } conf, err := google.JWTConfigFromJSON( jsonKey, pubsub.ScopeCloudPlatform, pubsub.ScopePubSub, ) if err != nil { log.Fatal(err) } ctx := cloud.NewContext("project-id", conf.Client(oauth2.NoContext)) return ctx } func main() { ctx := Example_auth() // E.g. c.CreateSub("sub1", "topic1", time.Duration(0), "") msgs, err := pubsub.Pull(ctx, "sub1", 1) if err != nil { log.Fatal(err) } log.Printf("New message arrived: %v\n", msgs[0]) if err := pubsub.Ack(ctx, "sub1", msgs[0].AckID); err != nil { log.Fatal(err) } log.Println("Acknowledged message") }
Output: