andresusandi-pubsub
Version:
PubSub library that provides a number of different underlying technologies
26 lines (20 loc) • 1.03 kB
JavaScript
const GooglePubSubBase = require('./GooglePubSubBase');
const { name: packageName } = require('./package.json');
const { SUBSCRIPTION_EVENTS } = require('./Constants');
class GoogleSubscriber extends GooglePubSubBase {
constructor(projectId, keyFilename, topicName, subscriptionName, messageCallback) {
super(projectId, keyFilename, topicName, subscriptionName);
if (!messageCallback)
throw new Error("messageCallback is required");
this.messageCallback = messageCallback;
}
beginListening() {
console.log(`${packageName}/${this.constructor.name} Starting to listen ${SUBSCRIPTION_EVENTS.MESSAGE}`);
this.subscription.on(SUBSCRIPTION_EVENTS.MESSAGE, this.messageCallback);
}
stopListening() {
console.log(`${packageName}/${this.constructor.name} Stopping to listen ${SUBSCRIPTION_EVENTS.MESSAGE}`);
this.subscription.removeListener(SUBSCRIPTION_EVENTS.MESSAGE, this.messageCallback);
}
}
module.exports = GoogleSubscriber;