-
Notifications
You must be signed in to change notification settings - Fork 592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option to automatically create a topic and re-use a subscription. #465
Conversation
* @param {object=} options - Configuration object. | ||
* @param {bool=} options.autoCreate - Automatically create topic if | ||
* it doesn't exist. Note that messages published to a topic with | ||
* no subscribers will not be delivered. | ||
* @return {module:pubsub/topic} | ||
* | ||
* @example | ||
* var topic = pubsub.topic('my-existing-topic'); | ||
* topic.publish('New message!'); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
* Adding simple example of using autoCreate. * Putting string comparison last.
Fixed the lint issues and updated after the review. |
@@ -276,7 +313,10 @@ Topic.prototype.subscribe = function(name, options, callback) { | |||
|
|||
var path = Subscription.formatName_(this.projectId, name); | |||
this.makeReq_('PUT', path, null, body, function(err) { | |||
if (err) { | |||
if (options.reuseExisting && err && err.code === 409) { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Overall LGTM. One stylistic nitpick: we add spaces between our keys in an object. Like this: |
LGTM. Thanks for doing this! |
Merged in 3f8f60f |
I wasn't able to get this working from the example you provided. Here's my code: const PubSub = require('@google-cloud/pubsub')
const pubsub = PubSub({
projectId: require('./cred.json').project_id,
credentials: require('./creds.json')
})
const topic = pubsub.topic('hi-there', { autoCreate: true })
topic.publish({
data: 'New message!'
}, function(err) {
console.log(err)
});
|
A similar example came up on StackOverflow (http://stackoverflow.com/questions/38864043/gcloud-check-if-a-topic-exist-and-ability-to-reuse-the-topic), so I'll quote my response there: I believe the problem you'll run into is that if a message is published to a topic that doesn't exist, it is immediately dropped. So, it won't hang around and wait for a subscription to be created; it'll just disappear. However, gcloud-node does have methods that will create a topic if necessary: var topic = pubsub.topic('topic-that-maybe-exists');
topic.get({ autoCreate: true }, function(err, topic) {
// topic.publish(...
}); In fact, almost all gcloud-node objects have the get method that will work the same way as above, i.e. a Pub/Sub subscription or a Storage bucket or a BigQuery dataset, etc. Here's a link to the topic.get() method in the docs: https://googlecloudplatform.github.io/gcloud-node/#/docs/v0.37.0/pubsub/topic?method=get |
I have error
|
these are publish options
|
Co-authored-by: gcf-owl-bot[bot] <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: gcf-owl-bot[bot] <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: gcf-owl-bot[bot] <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
🤖 I have created a release \*beep\* \*boop\* --- ### [3.2.4](https://www.github.com/googleapis/nodejs-os-login/compare/v3.2.3...v3.2.4) (2021-07-12) ### Bug Fixes * **deps:** google-gax v2.17.1 ([#464](https://www.github.com/googleapis/nodejs-os-login/issues/464)) ([67847c3](https://www.github.com/googleapis/nodejs-os-login/commit/67847c3d7f8328c797daf93a8880025b3fdbc763)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
🤖 I have created a release \*beep\* \*boop\* --- ### [3.2.4](https://www.github.com/googleapis/nodejs-os-login/compare/v3.2.3...v3.2.4) (2021-07-12) ### Bug Fixes * **deps:** google-gax v2.17.1 ([#464](https://www.github.com/googleapis/nodejs-os-login/issues/464)) ([67847c3](https://www.github.com/googleapis/nodejs-os-login/commit/67847c3d7f8328c797daf93a8880025b3fdbc763)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
- [ ] Regenerate this pull request now. PiperOrigin-RevId: 468790263 Source-Link: googleapis/googleapis@873ab45 Source-Link: googleapis/googleapis-gen@cb6f37a Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiY2I2ZjM3YWVmZjJhMzQ3MmU0MGE3YmJhY2U4YzY3ZDc1ZTI0YmVlNSJ9
This PR was generated using Autosynth. 🌈 Synth log will be available here: https://source.cloud.google.com/results/invocations/2f68300a-9812-4342-86c6-33ab267ece4f/targets Source-Link: googleapis/synthtool@be74d3e
Co-authored-by: gcf-merge-on-green[bot] <60162190+gcf-merge-on-green[bot]@users.noreply.github.com> Source-Author: F. Hinkelmann <franziska.hinkelmann@gmail.com> Source-Date: Wed Sep 30 14:13:57 2020 -0400 Source-Repo: googleapis/synthtool Source-Sha: 079dcce498117f9570cebe6e6cff254b38ba3860 Source-Link: googleapis/synthtool@079dcce
🤖 I have created a release \*beep\* \*boop\* --- ## [2.4.0](https://www.github.com/googleapis/nodejs-cloud-container/compare/v2.3.2...v2.4.0) (2021-07-09) ### Features * allow updating security group on existing clusters ([#464](https://www.github.com/googleapis/nodejs-cloud-container/issues/464)) ([ed09e02](https://www.github.com/googleapis/nodejs-cloud-container/commit/ed09e028126d308cf131f26453fe6c73a73cec6d)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
🤖 I have created a release \*beep\* \*boop\* --- ### [2.1.1](https://www.github.com/googleapis/nodejs-monitoring/compare/v2.1.0...v2.1.1) (2020-07-24) ### Bug Fixes * move gitattributes files to node templates ([#464](https://www.github.com/googleapis/nodejs-monitoring/issues/464)) ([db2d19d](https://www.github.com/googleapis/nodejs-monitoring/commit/db2d19d9170eb10d8f2c779589e623222991d393)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please).
Source-Link: googleapis/synthtool@d229a12 Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-nodejs:latest@sha256:74ab2b3c71ef27e6d8b69b1d0a0c9d31447777b79ac3cd4be82c265b45f37e5e
Co-authored-by: gcf-owl-bot[bot] <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
🤖 I have created a release \*beep\* \*boop\* --- ## [2.4.0](https://www.github.com/googleapis/nodejs-kms/compare/v2.3.3...v2.4.0) (2021-06-14) ### Features * add ECDSA secp256k1 to the list of supported algorithms ([#464](https://www.github.com/googleapis/nodejs-kms/issues/464)) ([a27f95d](https://www.github.com/googleapis/nodejs-kms/commit/a27f95dffdea8a803d02da443a38b10ed7720c86)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
This PR was generated using Autosynth. 🌈 Synth log will be available here: https://source.cloud.google.com/results/invocations/9c6207e5-a7a6-4e44-ab6b-91751e0230b1/targets - [ ] To automatically regenerate this PR, check this box. Source-Link: googleapis/synthtool@d82decc
This PR was generated using Autosynth. 🌈 Synth log will be available here: https://source.cloud.google.com/results/invocations/bfad0331-d2fb-4a90-9e49-15541b56d989/targets - [ ] To automatically regenerate this PR, check this box.
This adds
autoCreate
topubsub.topic
:And
reuseExisting
totopic.subscribe
:Combined, these two features completely eliminate any bootstrapping code/steps required for the Getting Started on GCP sample.