Skip to content

Instantly share code, notes, and snippets.

@carlozamagni
Last active November 8, 2017 18:04
Show Gist options
  • Save carlozamagni/62b2470740e0d888bdc41d54e86d6c26 to your computer and use it in GitHub Desktop.
Save carlozamagni/62b2470740e0d888bdc41d54e86d6c26 to your computer and use it in GitHub Desktop.
Log a pubsub topic to console
'use strict'
const PubSub = require('@google-cloud/pubsub')
const pubSubClient = PubSub({
projectId: '<project-id>',
keyFilename: 'service_account_test.json'
})
var topic = pubSubClient.topic('<topic-name>');
//topic.subscribe('newMessages', function(err, subscription, apiResponse) {});
topic.subscription('message-logger').get({ autoCreate: true }, (err, subscription, apiResponse) => {
if (err) {
console.error(err)
return done()
}
let subscriptionName = subscription.name.split('/')[subscription.name.split('/').length - 1]
console.log('\n- subscription found:', subscriptionName)
console.log('- listing incoming messages:')
subscription.on('message', (msg) => {
console.log('received', msg)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment