|
| 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 | + * subscriptions 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 | +// sample-metadata: |
| 24 | +// title: Create Subscription With Filtering |
| 25 | +// description: Creates a new subscription with filtering. |
| 26 | +// usage: node createSubscriptionWithFiltering.js <topic-name-or-id> <subscription-name-or-id> <filter-string> |
| 27 | + |
| 28 | +// [START pubsub_create_subscription_with_filter] |
| 29 | +/** |
| 30 | + * TODO(developer): Uncomment these variables before running the sample. |
| 31 | + */ |
| 32 | +// const topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID'; |
| 33 | +// const subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID'; |
| 34 | +// const filterString = 'YOUR_FILTER_STRING'; // e.g. 'attributes.author="unknown"' |
| 35 | + |
| 36 | +// Imports the Google Cloud client library |
| 37 | +import {PubSub} from '@google-cloud/pubsub'; |
| 38 | + |
| 39 | +// Creates a client; cache this for further use |
| 40 | +const pubSubClient = new PubSub(); |
| 41 | + |
| 42 | +async function createSubscriptionWithFilter( |
| 43 | + topicNameOrId: string, |
| 44 | + subscriptionNameOrId: string, |
| 45 | + filterString: string |
| 46 | +) { |
| 47 | + // Creates a new subscription |
| 48 | + await pubSubClient |
| 49 | + .topic(topicNameOrId) |
| 50 | + .createSubscription(subscriptionNameOrId, { |
| 51 | + filter: filterString, |
| 52 | + }); |
| 53 | + console.log( |
| 54 | + `Created subscription ${subscriptionNameOrId} with filter ${filterString}.` |
| 55 | + ); |
| 56 | +} |
| 57 | +// [END pubsub_create_subscription_with_filter] |
| 58 | + |
| 59 | +function main( |
| 60 | + topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID', |
| 61 | + subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID', |
| 62 | + filterString = 'YOUR_FILTER_STRING' |
| 63 | +) { |
| 64 | + createSubscriptionWithFilter( |
| 65 | + topicNameOrId, |
| 66 | + subscriptionNameOrId, |
| 67 | + filterString |
| 68 | + ).catch(err => { |
| 69 | + console.error(err.message); |
| 70 | + process.exitCode = 1; |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +main(...process.argv.slice(2)); |
0 commit comments