Skip to main content
Version: v1

Subscriptions

Create a Subscriber to define a message handler for messages that are published on the corresponding topic.

Subscribers are contained in PUBSUB_ROOT_DIR/subscriptions.

tip

Files ending in .sub.js or .sub.ts in PUBSUB_ROOT_DIR/subscriptions will be autoloaded by the subscription server.

Examples#

Typescript subscription example#

/pubsub/subscriptions/simple.topic.name.console-log.sub.ts
import { SubscriberObject, Message } from "@honestfoodcompany/pubsub"; // this import is optional, it's gives us the interfaces to use below
export default: SubscriberObject = {
topicName: 'simple.topic',
subscriptionName: 'simple.topic.console-log.sub',
description: 'Will console log messages published on test.topic',
handleMessage: function(message: Message): void {
console.log(this.subscriptionName, 'received message');
console.log(message.data.toString());
message.ack();
},
handleError: function(error: Error): void {
// default error handler for subscriber
}
};

Javascript subscription example#

/pubsub/subscriptions/simple.topic.name.sub.js
exports.default = {
topicName: 'test.topic',
subscriptionName: 'test.topic.sub',
description: 'Will console log messages published on test.topic',
handleMessage: function (message) {
console.log(this.subscriptionName, 'received message');
console.log(message.data.toString());
message.ack();
},
};

Subscription example with custom subscriber options#

"/pubsub/subscriptions/simple.topic.name.subscription.js
exports.default = {
topicName: 'test.topic',
subscriptionName: 'test.topic.subscription',
description: 'Will console log messages published on test.topic',
options: {
flowControl: {
maxMessages: 500, // max messages in progress
},
},
handleMessage: function (message) {
console.log(`received a message on ${this.subscriptionName}`);
console.log(message.data.toString());
message.ack();
},
};

Subscriber Options#

See full options available in API Docs.