GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Automatically create topic / subscription if not found. · Issue #445 · googleapis/google-cloud-node

Automatically create topic / subscription if not found. #445

New issue
Copy link
New issue
Copy link
Closed
Closed
Automatically create topic / subscription if not found.#445
Copy link
Assignees
stephenplusplus
Labels
🚨This issue needs some love.api: pubsubIssues related to the Pub/Sub API.triage meI really want to be triaged.
Milestone
Pub/Sub Beta

Description

@theacodes
theacodes
opened on Mar 13, 2015
Issue body actions

Presently I have some code to publish to a particular topic, creating it if necessary:

function publish(message, cb){
  topic.publish(message, function(err){
    if(err && err.code == 404){
      logging.info("Creating pub/sub topic.");
      pubsub.createTopic(topicName, function(err, _){
        if(err) return cb(err);
        publish(message, cb);
      });
    } else {
      cb(err);
    }
  });
}

And similar code to listen for messages to a topic. It goes the other way around - trying to create one at first then re-using if it's already there.

function subscribe(cb){
  topic.subscribe(subscriptionName, function(err, subscription){
    if(!err) return wireSubscription(subscription);
    if(err && err.code == 409) return wireSubscription(topic.subscription(subscriptionName));
    throw err;
  });
  ...
}

According to this comment, it seems that the intended usage is to create the topic and subscription outside of the application content and never touch them afterwards. It's not clear in the documentation that this is expected.

I think it would be great to either:

  1. Consolidate subscribe/subscription createTopic/topic into combined create/reuse functions (e.g. .topic() .subscription()). There can be an optional argument for erring instead of re-using. This hides all of this somewhat ugly logic from users that do want this use-case.
  2. Explicitly document that topics and subscriptions should be created beforehand.

I would personally prefer the former as it removes the need to write a "bootstrapping" script and simplifies the user-facing API.

Activity

Next
  1. stephenplusplus commented on Mar 13, 2015

    @stephenplusplus
    stephenplusplus
    on Mar 13, 2015
    Contributor
    More actions

    Do you mean a Topic and Subscription object should create itself if necessary after you attempt to use it? I'm ok with this idea, but what's the trouble with manually creating the topic/sub before using it?

  2. theacodes commented on Mar 13, 2015

    @theacodes
    theacodes
    on Mar 13, 2015
    Author
    More actions

    Yes. The trouble with manually creating it before use leads to having to write unnecessary code to handle creating it if necessary- unless you meant creating it with a bootstrap script beforehand outside of the application context. In that case, it's just inconvenient for no real benefit. I could see myself forgetting to create new subscriptions/topics while iterating new features.

  3. ryanseys commented on Mar 14, 2015

    @ryanseys
    ryanseys
    on Mar 14, 2015
    Contributor
    More actions

    The approach we have taken for every object in this library is that you can create one or reference an existing one. If we changed this, it would need to be changed everywhere and would require two API requests rather than one, causing increased delays for responses.

    Now actually, the latest pubsub v1beta2 (in this library master branch) call to create a topic/subscription is a PUT call meaning its idempotent (basically no matter how many times you call it, it won't cause more effects than if you call it just once). This means that you can effectively "create or get" by always creating the topic regardless of whether it exists or not. If we know we don't need to create it, it would be silly to try to every time we want to do something with it, this would multiply every request time by a factor of two and provide less stability overall.

    Hope this makes sense. Let me know if there's a smart compromise that could be made here.

    Reacted by Renan Batista
  4. theacodes commented on Mar 18, 2015

    @theacodes
    theacodes
    on Mar 18, 2015
    Author
    More actions

    Hmm. I definitely don't want to make a recommendation that would go against the grain of the library as a whole.

    Instead of trying to create every time, couldn't the "use existing" method create if the response is a 404? That should only add 2 additional RPCs (create, then re-submit whatever request originally gave the 404) and only once. Similar to the publish example in my first comment. This wouldn't change the surface of the API but could really help the use case I'm designing for. We could even choose to have it explicit:

    var topic = pubsub.topic('my-topic', {autoCreate: true});
    topic.publish(message);  // will catch 404, create topic, and then publish the message.
    
    var topic2 = pubsub.topic('my-topic-2');
    topic2.publish(message); // will behave as today.
  5. ryanseys commented on Mar 18, 2015

    @ryanseys
    ryanseys
    on Mar 18, 2015
    Contributor
    More actions

    I like this idea.

  6. stephenplusplus commented on Mar 18, 2015

    @stephenplusplus
    stephenplusplus
    on Mar 18, 2015
    Contributor
    More actions

    There might be some useful information to abstract from this pr when I had basically this exact behavior! #107

  7. theacodes commented on Mar 18, 2015

    @theacodes
    theacodes
    on Mar 18, 2015
    Author
    More actions

    Interesting conversation there, and @rakyll seems to be pretty hardline on avoiding "magic". Two thoughts:

    1. I agree with not having methods that will potentially delete and recreate resources.
    2. I think the explicit form {autoCreate: true} avoids "magic".
  8. stephenplusplus
    added
    api: pubsubIssues related to the Pub/Sub API.
    on Mar 23, 2015
  9. ryanseys
    added this to the Pub/Sub Beta milestone on Mar 23, 2015
  10. theacodes commented on Mar 23, 2015

    @theacodes
    theacodes
    on Mar 23, 2015
    Author
    More actions

    Here's another example to further illustrate the need for this. I followed the "encouraged" flow by writing a one-off script to create pub/sub topics and subscriptions ahead of time:

    if (!module.parent) {
      console.log(
        "Creating pub/sub topics and subscriptions on project %s.",
        config.gcloud.projectId);
    
      pubsub.createTopic(topicName, function(err, topic){
        if(err && err.code == 409 && err.errors && err.errors[0].reason == "alreadyExists") {
          console.log("Topic %s already exists.", topicName);
          topic = pubsub.topic(topicName);
        } else if(err || !topic) {
          throw err;
        } else {
          console.log('Created topic %s', topicName);
        }
    
        topic.subscribe(subscriptionName, function(err, subscription){
          if(err && err.code == 409 && err.errors && err.errors[0].reason == "alreadyExists") {
            console.log("Subscription %s already exists.", subscriptionName);
          } else if(err) {
            throw err;
          } else{
            console.log('Created subscription %s', subscriptionName);
          }
          console.log('Done');
          process.exit();
        });
      });
    }

    Definitely seems a bit unwieldy for just one topic & subscription. Of course, if there were a gcloud pubsub topics create ... command this could be workable, but as it stands I think this is poor developer experience.

  11. tmatsuo commented on Mar 25, 2015

    @tmatsuo
    tmatsuo
    on Mar 25, 2015
    Contributor
    More actions

    I have a little concern.

    With the current version of Cloud Pub/Sub, a topic without any subscriptions will throw away your messages until the first subscription will be created and attached to it.

    So if we auto create on publish, then the messages subsequently published to this topic will go /dev/null, the situation will continue until you create the first subscription.

  12. tmatsuo commented on Mar 25, 2015

    @tmatsuo
    tmatsuo
    on Mar 25, 2015
    Contributor
    More actions

    Also, hopefully we will start providing gcloud pubsub subcommand, as well as Developer Console UI, so the first time burden will be mitigated.

  13. theacodes commented on Mar 25, 2015

    @theacodes
    theacodes
    on Mar 25, 2015
    Author
    More actions

    I personally think it's a better to document that a topic with no subscriptions doesn't deliver messages and add the autoCreate option than to keep the developer experience we have now. The experience we have now is quite painful and frustrating.

    Do we have any idea when gcloud pubsub will land? I think we should weigh the effort of adding this vs the time until gcloud pubsub is available.

  14. ryanseys commented on Mar 25, 2015

    @ryanseys
    ryanseys
    on Mar 25, 2015
    Contributor
    More actions

    We welcome contributions too so feel free to mock something up for this. I'm a fan of the autoCreate flag to reduce magic. We have no currently planned release date for the next version unfortunately. We're awaiting library reviews from various teams at Google. I hope to have an update for you soon!

  15. tmatsuo commented on Mar 25, 2015

    @tmatsuo
    tmatsuo
    on Mar 25, 2015
    Contributor
    More actions

    Do we have any idea when gcloud pubsub will land?

    Not so far away. For a time being, you may find it useful to use my command line tool:
    https://github.com/GoogleCloudPlatform/cloud-pubsub-samples-python
    (cmdline-pull)

    Although it still doesn't support push subscription, it can be used for other operations (create, list, publish, pull, etc).

  16. 38 remaining items

  17. GautamSharda
    added 2 commits that reference this issue on Feb 2, 2026

    chore: regenerate synth.metadata (#445)

    3395ab8

    chore: regenerate synth.metadata (#445)

    00ba806
  18. sofisl
    added 2 commits that reference this issue on Feb 4, 2026

    build: update dependencies (#445)

    dccd009

    fix(typescript): query pojection -> projection (#445)

    649612b
  19. GautamSharda
    added a commit that references this issue on Feb 5, 2026

    build: update linkinator config (#445)

    c77a825
  20. sofisl
    added a commit that references this issue on Feb 17, 2026

    fix(typescript): query pojection -> projection (#445)

    73e9261
  21. GautamSharda
    added a commit that references this issue on Feb 23, 2026

    build: update linkinator config (#445)

    d996fa7
  22. sofisl
    added a commit that references this issue on Feb 24, 2026

    Adding script to update Protos (#445)

    0c9cfd2
  23. GautamSharda
    added a commit that references this issue on Feb 26, 2026

    build: update linkinator config (#445)

    18f01c5
  24. GautamSharda
    added 2 commits that reference this issue on Mar 5, 2026

    refactor: use execSync for tests (#445)

    f3961b9

    build: use config file for linkinator (#445)

    5dd4084
  25. GautamSharda
    added 2 commits that reference this issue on Mar 23, 2026

    refactor(ts): Linter & Formatter (#445)

    16aae66

    refactor(ts): Linter & Formatter (#445)

    c224dde
  26. shivanee-p
    added a commit that references this issue on Mar 27, 2026

    refactor(ts): Linter & Formatter (#445)

    a315c86
  27. thiyaguk09
    added a commit that references this issue on May 5, 2026

    refactor(ts): Linter & Formatter (googleapis#445)

    894911e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @stephenplusplus
    stephenplusplus

Labels

🚨This issue needs some love.api: pubsubIssues related to the Pub/Sub API.triage meI really want to be triaged.

Type

No type

Projects

No projects

    Milestone

    • Pub/Sub Beta

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.