Skip to content

Difference with new subscription callback vs object #1088

Description

@thesandlord

There is a weird difference between the callback 'subscribe' method and the 'subscription' object that does not make sense.

I would expect these two snippets to act the same way

topic.subscribe('new-subscription', function(err, subscription) {
  subscription.on('message', function(message){
    console.log(message.data);
  }
});
var subscription = topic.subscription('new-subscription');
subscription.on('message', function(message){
    console.log(message.data);
}

However, the first one creates a subscription, but crashes if the subscription already exists.
The second one does not create a subscription, but works on pre-existing subscriptions.

So, I had to write this workaround code

var subscription = topic.subscription('new-subscription');
subscription.exists(function(err, exists) {
  if (!exists){
    subscription.create(function(err, subscription, apiResponse) {
        if (!err) {
          subscription.on('message', function(message){
            console.log(message.data);
          });
       }
    });
  }
});

I really wish both of the first two examples automatically made the subscription AND worked on pre-existing subscriptions. Right now, it feels like the python API (check for the subscription, if it does not exist create it), but the callback hell is not good.

Activity

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

Metadata

Metadata

Labels

api: pubsubIssues related to the Pub/Sub API.type: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions