Skip to content
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

Closed
wants to merge 6 commits into from

Conversation

theacodes
Copy link

This adds autoCreate to pubsub.topic:

var topic = pubsub.topic('does-not-yet-exist', {autoCreate: true});
// topic will be created on first RPC.
topic.publish({data: {hello: 'world'});

And reuseExisting to topic.subscribe:

topic.subscribe('existingSub', {reuseExisting: true}, function(err, sub){ ... }));

Combined, these two features completely eliminate any bootstrapping code/steps required for the Getting Started on GCP sample.

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Mar 25, 2015
* @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.

* Adding simple example of using autoCreate.
* Putting string comparison last.
@theacodes
Copy link
Author

Fixed the lint issues and updated after the review.
Also, forgot to mention in the PR - this fixes #445.

@@ -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.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@ryanseys
Copy link
Contributor

Overall LGTM. One stylistic nitpick: we add spaces between our keys in an object.

Like this: { hello: 'world' } not like this: {hello: 'world'}

@stephenplusplus
Copy link
Contributor

LGTM. Thanks for doing this!

@ryanseys
Copy link
Contributor

Merged in 3f8f60f

@matthewmueller
Copy link

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)
});

pubsub.topic doesn't actually take an 2nd options parameter

@stephenplusplus
Copy link
Contributor

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

@gustawdaniel-acaisoft
Copy link

I have error

TS2345: Argument of type '{ autoCreate: boolean; }' is not assignable to parameter of type 'PublishOptions'.   Object literal may only specify known properties, and 'autoCreate' does not exist in type 'PublishOptions'.

@gustawdaniel-acaisoft
Copy link

these are publish options

export interface PublishOptions {
    batching?: BatchPublishOptions;
    flowControlOptions?: FlowControlOptions;
    gaxOpts?: CallOptions;
    messageOrdering?: boolean;
    enableOpenTelemetryTracing?: boolean;
}

chingor13 pushed a commit that referenced this pull request Aug 22, 2022
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>
chingor13 pushed a commit that referenced this pull request Aug 22, 2022
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>
chingor13 pushed a commit that referenced this pull request Aug 22, 2022
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>
sofisl pushed a commit that referenced this pull request Sep 16, 2022
sofisl pushed a commit that referenced this pull request Sep 27, 2022
sofisl pushed a commit that referenced this pull request Oct 5, 2022
sofisl pushed a commit that referenced this pull request Oct 12, 2022
- [ ] Regenerate this pull request now.

PiperOrigin-RevId: 468790263

Source-Link: googleapis/googleapis@873ab45

Source-Link: googleapis/googleapis-gen@cb6f37a
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiY2I2ZjM3YWVmZjJhMzQ3MmU0MGE3YmJhY2U4YzY3ZDc1ZTI0YmVlNSJ9
@release-please release-please bot mentioned this pull request Oct 12, 2022
sofisl pushed a commit that referenced this pull request Oct 13, 2022
sofisl pushed a commit that referenced this pull request Nov 9, 2022
sofisl pushed a commit that referenced this pull request Nov 10, 2022
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
sofisl pushed a commit that referenced this pull request Nov 11, 2022
sofisl pushed a commit that referenced this pull request Nov 11, 2022
sofisl pushed a commit that referenced this pull request Nov 11, 2022
Source-Link: googleapis/synthtool@d229a12
Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-nodejs:latest@sha256:74ab2b3c71ef27e6d8b69b1d0a0c9d31447777b79ac3cd4be82c265b45f37e5e
sofisl pushed a commit that referenced this pull request Nov 11, 2022
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>
sofisl pushed a commit that referenced this pull request Nov 11, 2022
sofisl pushed a commit that referenced this pull request Nov 17, 2022
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
sofisl pushed a commit that referenced this pull request Sep 13, 2023
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants