v1 Receive messages of Proto schema type (DEPRECATED)
Stay organized with collections
Save and categorize content based on your preferences.
(DEPRECATED) Receive messages of Proto schema type
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["# v1 Receive messages of Proto schema type (DEPRECATED)\n\n(DEPRECATED) Receive messages of Proto schema type\n\nCode sample\n-----------\n\n### Go\n\n\nBefore trying this sample, follow the Go setup instructions in the\n[Pub/Sub quickstart using\nclient libraries](/pubsub/docs/quickstart-client-libraries).\n\n\nFor more information, see the\n[Pub/Sub Go API\nreference documentation](https://godoc.org/cloud.google.com/go/pubsub).\n\n\nTo authenticate to Pub/Sub, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n \t\"sync\"\n \t\"time\"\n\n \t\"cloud.google.com/go/pubsub\"\n \tstatepb \"github.com/GoogleCloudPlatform/golang-samples/internal/pubsub/schemas\"\n \t\"google.golang.org/protobuf/encoding/protojson\"\n \t\"google.golang.org/protobuf/proto\"\n )\n\n func subscribeWithProtoSchema(w io.Writer, projectID, subID, protoFile string) error {\n \t// projectID := \"my-project-id\"\n \t// subID := \"my-sub\"\n \t// protoFile = \"path/to/a/proto/schema/file(.proto)/formatted/in/protocol/buffers\"\n \tctx := context.Background()\n \tclient, err := pubsub.https://cloud.google.com/go/docs/reference/cloud.google.com/go/pubsub/latest/index.html#cloud_google_com_go_pubsub_Client_NewClient(ctx, projectID)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"pubsub.NewClient: %w\", err)\n \t}\n\n \t// Create an instance of the message to be decoded (a single U.S. state).\n \tstate := &statepb.State{}\n\n \tsub := client.Subscription(subID)\n \tctx2, cancel := context.WithTimeout(ctx, 10*time.Second)\n \tdefer cancel()\n\n \tvar mu sync.Mutex\n \tsub.Receive(ctx2, func(ctx context.Context, msg *pubsub.Message) {\n \t\tmu.Lock()\n \t\tdefer mu.Unlock()\n \t\tencoding := msg.Attributes[\"googclient_schemaencoding\"]\n\n \t\tif encoding == \"BINARY\" {\n \t\t\tif err := proto.Unmarshal(msg.Data, state); err != nil {\n \t\t\t\tfmt.Fprintf(w, \"proto.Unmarshal err: %v\\n\", err)\n \t\t\t\tmsg.Nack()\n \t\t\t\treturn\n \t\t\t}\n \t\t\tfmt.Printf(\"Received a binary-encoded message:\\n%#v\\n\", state)\n \t\t} else if encoding == \"JSON\" {\n \t\t\tif err := protojson.Unmarshal(msg.Data, state); err != nil {\n \t\t\t\tfmt.Fprintf(w, \"proto.Unmarshal err: %v\\n\", err)\n \t\t\t\tmsg.Nack()\n \t\t\t\treturn\n \t\t\t}\n \t\t\tfmt.Fprintf(w, \"Received a JSON-encoded message:\\n%#v\\n\", state)\n \t\t} else {\n \t\t\tfmt.Fprintf(w, \"Unknown message type(%s), nacking\\n\", encoding)\n \t\t\tmsg.Nack()\n \t\t\treturn\n \t\t}\n \t\tfmt.Fprintf(w, \"%s is abbreviated as %s\\n\", state.Name, state.PostAbbr)\n \t\tmsg.Ack()\n \t})\n \treturn nil\n }\n\n### Ruby\n\n\nBefore trying this sample, follow the Ruby setup instructions in the\n[Pub/Sub quickstart using\nclient libraries](/pubsub/docs/quickstart-client-libraries).\n\n\nFor more information, see the\n[Pub/Sub Ruby API\nreference documentation](https://googleapis.dev/ruby/google-cloud-pubsub/latest/Google/Cloud/PubSub.html).\n\n\nTo authenticate to Pub/Sub, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n # subscription_id = \"your-subscription-id\"\n\n pubsub = Google::Cloud::https://cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud.html.new\n\n subscription = pubsub.subscription subscription_id\n\n subscriber = subscription.listen do |received_message|\n encoding = received_message.attributes[\"googclient_schemaencoding\"]\n case encoding\n when \"BINARY\"\n state = Utilities::StateProto.decode received_message.data\n puts \"Received a binary-encoded message:\\n#{state}\"\n when \"JSON\"\n require \"json\"\n state = Utilities::StateProto.decode_json received_message.data\n puts \"Received a JSON-encoded message:\\n#{state}\"\n else\n \"Received a message with no encoding:\\n#{received_message.message_id}\"\n end\n received_message.acknowledge!\n end\n\n subscriber.start\n # Let the main thread sleep for 60 seconds so the thread for listening\n # messages does not quit\n sleep 60\n subscriber.stop.wait!\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=pubsub)."]]