|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +/** |
| 16 | + * This application demonstrates how to perform basic operations on |
| 17 | + * schemas with the Google Cloud Pub/Sub API. |
| 18 | + * |
| 19 | + * For more information, see the README.md under /pubsub and the documentation |
| 20 | + * at https://cloud.google.com/pubsub/docs. |
| 21 | + */ |
| 22 | + |
| 23 | +// This is a generated sample. Please see typescript/README.md for more info. |
| 24 | + |
| 25 | +'use strict'; |
| 26 | + |
| 27 | +// sample-metadata: |
| 28 | +// title: Listen with exactly-once delivery |
| 29 | +// description: Listen for messages on an exactly-once delivery subscription. |
| 30 | +// usage: node listenForMessagesWithExactlyOnceDelivery.js <subscription-name-or-id> |
| 31 | + |
| 32 | +// [START pubsub_subscriber_exactly_once] |
| 33 | +/** |
| 34 | + * TODO(developer): Uncomment this variable before running the sample. |
| 35 | + */ |
| 36 | +// const subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID'; |
| 37 | + |
| 38 | +// Imports the Google Cloud client library |
| 39 | +const {PubSub} = require('@google-cloud/pubsub'); |
| 40 | + |
| 41 | +// Creates a client; cache this for further use |
| 42 | +const pubSubClient = new PubSub(); |
| 43 | + |
| 44 | +async function listenForMessagesWithExactlyOnceDelivery( |
| 45 | + subscriptionNameOrId, |
| 46 | + timeout |
| 47 | +) { |
| 48 | + // References an existing subscription |
| 49 | + const subscription = pubSubClient.subscription(subscriptionNameOrId); |
| 50 | + |
| 51 | + // Create an event handler to handle messages |
| 52 | + let messageCount = 0; |
| 53 | + const messageHandler = async message => { |
| 54 | + console.log(`Received message ${message.id}:`); |
| 55 | + console.log(`\tData: ${message.data}`); |
| 56 | + console.log(`\tAttributes: ${message.attributes}`); |
| 57 | + messageCount++; |
| 58 | + |
| 59 | + // Use `ackWithResponse()` instead of `ack()` to get a Promise that tracks |
| 60 | + // the result of the acknowledge call. When exactly-once delivery is enabled |
| 61 | + // on the subscription, the message is guaranteed not to be delivered again |
| 62 | + // if the ack Promise resolves. |
| 63 | + try { |
| 64 | + // When the Promise resolves, the value is always AckResponses.Success, |
| 65 | + // signaling that the ack was accepted. Note that you may call this |
| 66 | + // method on a subscription without exactly-once delivery, but it will |
| 67 | + // always return AckResponses.Success. |
| 68 | + await message.ackWithResponse(); |
| 69 | + console.log(`Ack for message ${message.id} successful.`); |
| 70 | + } catch (e) { |
| 71 | + // In all other cases, the error passed on reject will explain why. This |
| 72 | + // is only for permanent failures; transient errors are retried automatically. |
| 73 | + const ackError = e; |
| 74 | + console.log( |
| 75 | + `Ack for message ${message.id} failed with error: ${ackError.errorCode}` |
| 76 | + ); |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + // Listen for new messages until timeout is hit |
| 81 | + subscription.on('message', messageHandler); |
| 82 | + |
| 83 | + setTimeout(() => { |
| 84 | + subscription.removeListener('message', messageHandler); |
| 85 | + console.log(`${messageCount} message(s) received.`); |
| 86 | + }, timeout * 1000); |
| 87 | +} |
| 88 | +// [END pubsub_subscriber_exactly_once] |
| 89 | + |
| 90 | +function main( |
| 91 | + subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID', |
| 92 | + timeout = 60 |
| 93 | +) { |
| 94 | + listenForMessagesWithExactlyOnceDelivery( |
| 95 | + subscriptionNameOrId, |
| 96 | + Number(timeout) |
| 97 | + ).catch(err => { |
| 98 | + console.error(err.message); |
| 99 | + process.exitCode = 1; |
| 100 | + }); |
| 101 | +} |
| 102 | + |
| 103 | +main(...process.argv.slice(2)); |
0 commit comments