Search code examples
node.jsherokugoogle-cloud-platformgoogle-cloud-pubsubgcloud-node

Google Cloud PubSub - can't seem to get topics


I'm using heroku to run a node.js app that uses gcloud to create a topic, and then subscribe to it. I'm using the following code, as taken from here: https://googlecloudplatform.github.io/gcloud-node/#/docs/v0.16.0/pubsub

var gcloud = require('gcloud')({
  keyFilename: 'pubsub_key.json',
  projectId: 'pipedrivesekoul'
});

var pubsub = gcloud.pubsub();

//create a new topic
pubsub.createTopic('projects/pipedrivesekoul/my-new-topic', function(err, topic, apiResponse) {
  var topic = pubsub.topic('my-new-topic');
  topic.publish({
    data: 'New message!'
  }, function(err) {console.log});
});

  var topic = pubsub.topic('my-new-topic');



// Without specifying any options.
topic.subscribe('newMessages', function(err, subscription, apiResponse) {});

var alltopics = pubsub.getTopics({}, function(err, topics, nextQuery, apiResponse) {});

console.log(pubsub.getTopics({}, function(err, topics, nextQuery, apiResponse) {}));

However, when I deploy on Heroku (https server, registered on Google Console, with the correct APIs deployed and the appropriate key in a json file), instead of seeing a list of topics, it just returns 'undefined':

2015-07-24T18:06:05.321079+00:00 app[web.1]: undefined

2015-07-24T18:06:05.337947+00:00 app[web.1]: Node app is running on port 36252

Not sure why this might be happening and not too sure how to debug this issue. Any suggestions would be greatly appreciated!


Solution

  • I've spotted a couple issues which will hopefully clear this up.

    pubsub.createTopic('projects/pipedrivesekoul/my-new-topic'
    

    You only need to provide the my-new-topic part. The ugly, long title is sent automatically.

    console.log(pubsub.getTopics({}, function(err, topics, nextQuery, apiResponse) {}));
    

    This is actually logging the result of calling

    pubsub.getTopics({}, function(err, topics, nextQuery, apiResponse) {})
    

    Which is undefined. Instead, try:

    pubsub.getTopics({}, function(err, topics, nextQuery, apiResponse) {
      if (err) {
        console.error(err);
        return;
      }
    
      console.log(topics); // hopefully in this array is one called `my-new-topic`
    });