func ExampleSubscription_Pull() { ctx := context.Background() client, err := pubsub.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } sub := client.Subscription("subName") it, err := sub.Pull(ctx) if err != nil { // TODO: Handle error. } // Ensure that the iterator is closed down cleanly. defer it.Stop() // Consume 10 messages. for i := 0; i < 10; i++ { m, err := it.Next() if err == pubsub.Done { // There are no more messages. This will happen if it.Stop is called. break } if err != nil { // TODO: Handle error. break } fmt.Printf("message %d: %s\n", i, m.Data) // Acknowledge the message. m.Done(true) } }
func ExampleClient_Topics() { ctx := context.Background() client, err := pubsub.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } it := client.Topics(ctx) _ = it // TODO: iterate using Next. }
func ExampleNewClient() { ctx := context.Background() _, err := pubsub.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } // See the other examples to learn how to use the Client. }
func ExampleClient_Subscriptions() { ctx := context.Background() client, err := pubsub.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } // List all subscriptions of the project. it := client.Subscriptions(ctx) _ = it // See the SubscriptionIterator example for its usage. }
func ExampleClient_Subscriptions() { ctx := context.Background() client, err := pubsub.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } // List all subscriptions of the project. it := client.Subscriptions(ctx) _ = it // TODO: iterate using Next. }
func setup(t *testing.T) *pubsub.Client { ctx := context.Background() tc := testutil.SystemTest(t) client, err := pubsub.NewClient(ctx, tc.ProjectID) if err != nil { t.Fatalf("failed to create client: %v", err) } return client }
func ExampleClient_Topics() { ctx := context.Background() client, err := pubsub.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } // List all topics. it := client.Topics(ctx) _ = it // See the TopicIterator example for its usage. }
func ExampleSubscription_ModifyPushConfig() { ctx := context.Background() client, err := pubsub.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } sub := client.Subscription("subName") if err := sub.ModifyPushConfig(ctx, &pubsub.PushConfig{Endpoint: "https://example.com/push"}); err != nil { // TODO: Handle error. } }
func ExampleTopic_Delete() { ctx := context.Background() client, err := pubsub.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } topic := client.Topic("topicName") if err := topic.Delete(ctx); err != nil { // TODO: Handle error. } }
func ExampleSubscription_Delete() { ctx := context.Background() client, err := pubsub.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } sub := client.Subscription("subName") if err := sub.Delete(ctx); err != nil { // TODO: Handle error. } }
func ExampleSubscription_Pull() { ctx := context.Background() client, err := pubsub.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } it, err := client.Subscription("subName").Pull(ctx) if err != nil { // TODO: Handle error. } // Ensure that the iterator is closed down cleanly. defer it.Stop() }
func ExampleSubscription_Config() { ctx := context.Background() client, err := pubsub.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } sub := client.Subscription("subName") config, err := sub.Config(ctx) if err != nil { // TODO: Handle error. } fmt.Println(config) }
func ExampleClient_NewTopic() { ctx := context.Background() client, err := pubsub.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } // Create a new topic with the given name. topic, err := client.CreateTopic(ctx, "topicName") if err != nil { // TODO: Handle error. } _ = topic // TODO: use the topic. }
func main() { ctx := context.Background() pubsubClient, err := pubsub.NewClient(ctx, "project") if err != nil { log.Fatalf("pubsub.NewClient: %v", err) } topic, err := pubsubClient.NewTopic(ctx, "topic") if err != nil { log.Fatalf("NewTopic: %v", err) } sub, err := pubsubClient.NewSubscription(ctx, "sub", topic, 0, nil) if err != nil { log.Fatalf("NewSubscription: %v", err) } if _, err := topic.Publish(ctx, &pubsub.Message{ Data: []byte("hello"), }); err != nil { log.Fatalf("Publish: %v", err) } it, err := sub.Pull(ctx) if err != nil { log.Fatalf("Pull: %v", err) } msg, err := it.Next() if err != nil { log.Fatalf("Next: %v", err) } log.Printf("pubsub message: %s", msg.Data) msg.Done(true) it.Stop() datastoreClient, err := datastore.NewClient(ctx, "project") if err != nil { log.Fatalf("datastore.NewClient: %v", err) } k := datastore.NewIncompleteKey(ctx, "Foo", nil) k, err = datastoreClient.Put(ctx, k, &Foo{F: "foo!"}) if err != nil { log.Fatalf("Put: %v", err) } var f Foo if err := datastoreClient.Get(ctx, k, &f); err != nil { log.Fatalf("Get: %v", err) } log.Printf("datastore got %v", f) }
func ExampleSubscription_Exists() { ctx := context.Background() client, err := pubsub.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } sub := client.Subscription("subName") ok, err := sub.Exists(ctx) if err != nil { // TODO: Handle error. } if !ok { // Subscription doesn't exist. } }
func ExampleTopic_Publish() { ctx := context.Background() client, err := pubsub.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } topic := client.Topic("topicName") msgIDs, err := topic.Publish(ctx, &pubsub.Message{ Data: []byte("hello world"), }) if err != nil { // TODO: Handle error. } fmt.Printf("Published a message with a message ID: %s\n", msgIDs[0]) }
func ExampleTopic_Exists() { ctx := context.Background() client, err := pubsub.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } topic := client.Topic("topicName") ok, err := topic.Exists(ctx) if err != nil { // TODO: Handle error. } if !ok { // Topic doesn't exist. } }
func main() { ctx := context.Background() client, err := pubsub.NewClient(ctx, mustGetenv("GCLOUD_PROJECT")) if err != nil { log.Fatal(err) } // Create topic if it doesn't exist. topic, _ = client.CreateTopic(ctx, mustGetenv("PUBSUB_TOPIC")) http.HandleFunc("/", listHandler) http.HandleFunc("/pubsub/publish", publishHandler) http.HandleFunc("/pubsub/push", pushHandler) appengine.Main() }
func ExampleSubscription_Pull_options() { ctx := context.Background() client, err := pubsub.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } sub := client.Subscription("subName") // This program is expected to process and acknowledge messages // in 5 seconds. If not, Pub/Sub API will assume the message is not // acknowledged. it, err := sub.Pull(ctx, pubsub.MaxExtension(5*time.Second)) if err != nil { // TODO: Handle error. } // Ensure that the iterator is closed down cleanly. defer it.Stop() }
func ExampleMessageIterator_Stop_defer() { // If all uses of the iterator occur within the lifetime of a single // function, stop it with defer. ctx := context.Background() client, err := pubsub.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } it, err := client.Subscription("subName").Pull(ctx) if err != nil { // TODO: Handle error. } // Ensure that the iterator is closed down cleanly. defer it.Stop() // TODO: Use the iterator (see the example for MessageIterator.Next). }
func ExampleTopic_Subscriptions() { ctx := context.Background() client, err := pubsub.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } topic := client.Topic("topic-name") // List all subscriptions of the topic (maybe of multiple projects). for subs := topic.Subscriptions(ctx); ; { sub, err := subs.Next() if err == pubsub.Done { break } if err != nil { // TODO: Handle error. } _ = sub // TODO: use the subscription. } }
func ExampleSubscriptionIterator() { ctx := context.Background() client, err := pubsub.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } // List all subscriptions of the project. it := client.Subscriptions(ctx) for { sub, err := it.Next() if err == pubsub.Done { break } if err != nil { // TODO: Handle error. } fmt.Println(sub.Name()) } }
func ExampleTopicIterator_Next() { ctx := context.Background() client, err := pubsub.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } // List all topics. it := client.Topics(ctx) for { t, err := it.Next() if err == iterator.Done { break } if err != nil { // TODO: Handle error. } fmt.Println(t) } }
func main() { ctx := context.Background() // [START auth] proj := os.Getenv("GOOGLE_CLOUD_PROJECT") if proj == "" { fmt.Fprintf(os.Stderr, "GOOGLE_CLOUD_PROJECT environment variable must be set.\n") os.Exit(1) } client, err := pubsub.NewClient(ctx, proj) if err != nil { log.Fatalf("Could not create pubsub Client: %v", err) } // [END auth] // Print all the subscriptions in the project. fmt.Println("Listing all subscriptions from the project:") subs, err := list(client) if err != nil { log.Fatal(err) } for _, sub := range subs { fmt.Println(sub) } t := createTopicIfNotExists(client) const sub = "example-subscription" // Create a new subscription. if err := create(client, sub, t); err != nil { log.Fatal(err) } // Pull messages via the subscription. if err := pullMsgs(client, sub, t); err != nil { log.Fatal(err) } // Delete the subscription. if err := delete(client, sub); err != nil { log.Fatal(err) } }
func ExampleMessageIterator_Stop_goroutine() *pubsub.MessageIterator { // If you use the iterator outside the lifetime of a single function, you // must still stop it. // This (contrived) example returns an iterator that will yield messages // for ten seconds, and then stop. ctx := context.Background() client, err := pubsub.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } it, err := client.Subscription("subName").Pull(ctx) if err != nil { // TODO: Handle error. } // Stop the iterator after receiving messages for ten seconds. go func() { time.Sleep(10 * time.Second) it.Stop() }() return it }
func main() { ctx := context.Background() // [START auth] proj := os.Getenv("GOOGLE_CLOUD_PROJECT") if proj == "" { fmt.Fprintf(os.Stderr, "GOOGLE_CLOUD_PROJECT environment variable must be set.\n") os.Exit(1) } client, err := pubsub.NewClient(ctx, proj) if err != nil { log.Fatalf("Could not create pubsub Client: %v", err) } // [END auth] // List all the topics from the project. fmt.Println("Listing all topics from the project:") topics, err := list(client) if err != nil { log.Fatalf("Failed to list topics: %v", err) } for _, t := range topics { fmt.Println(t) } const topic = "example-topic" // Create a new topic called example-topic. if err := create(client, topic); err != nil { log.Fatalf("Failed to create a topic: %v", err) } // Publish a text message on the created topic. if err := publish(client, topic, "hello world!"); err != nil { log.Fatalf("Failed to publish: %v", err) } // Delete the topic. if err := delete(client, topic); err != nil { log.Fatalf("Failed to delete the topic: %v", err) } }
func ExampleClient_NewSubscription() { ctx := context.Background() client, err := pubsub.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } // Create a new topic with the given name. topic, err := client.CreateTopic(ctx, "topicName") if err != nil { // TODO: Handle error. } // Create a new subscription to the previously created topic // with the given name. sub, err := client.CreateSubscription(ctx, "subName", topic, 10*time.Second, nil) if err != nil { // TODO: Handle error. } _ = sub // TODO: use the subscription. }
func runProgram(flags *hookFlags) error { if err := flags.Verify(); err != nil { return err } f, err := os.Open(flags.configFile) if err != nil { return err } config, err := ParseHookConfig(f) if err != nil { return err } glog.Infof("Connecting to pubsub (project-id: %s)", config.Project) client, err := pubsub.NewClient(context.Background(), config.Project) if err != nil { return err } for path, pathConfig := range config.Paths { topic := client.Topic(pathConfig.Topic) exists, err := topic.Exists(context.Background()) if err != nil { return err } if !exists { return fmt.Errorf("Topic doesn't exist: %s", pathConfig.Topic) } handler := HookHandler{ Secret: pathConfig.Secret, Topic: topic, } glog.Infof("Setting up handler for %s: push to %s", path, pathConfig.Secret, pathConfig.Topic) http.Handle(path, handler) } glog.Infof("Listening on port %d ...", flags.listenPort) return http.ListenAndServe(fmt.Sprintf(":%d", flags.listenPort), nil) }
func setup(t *testing.T) *pubsub.Client { ctx := context.Background() tc := testutil.SystemTest(t) client, err := pubsub.NewClient(ctx, tc.ProjectID) if err != nil { t.Fatalf("failed to create client: %v", err) } // Cleanup resources from the previous failed tests. once.Do(func() { // Create a topic. topic = client.Topic(topicID) ok, err := topic.Exists(ctx) if err != nil { t.Fatalf("failed to check if topic exists: %v", err) } if !ok { if topic, err = client.CreateTopic(ctx, topicID); err != nil { t.Fatalf("failed to create the topic: %v", err) } } // Delete the sub if already exists. sub := client.Subscription(subID) ok, err = sub.Exists(ctx) if err != nil { t.Fatalf("failed to check if sub exists: %v", err) } if ok { if err := client.Subscription(subID).Delete(ctx); err != nil { t.Fatalf("failed to cleanup the topic (%q): %v", subID, err) } } }) return client }