Message Queue Handlers (Stack9-core v2.0)
In Stack9 v2.0.0 was introduced the ability to interact with message queue services (AWS SQS & Azure Queue Storage), in order to work with message queues, a message queue handler needs to be configured.
Getting started
Create a new file in stack9/server/mq-handlers
called my-example-handler.js
@prop context
- object - context
@prop message
- object - queue message
class ExampleHandler {
constructor(message, context) {
this.message = message;
this.context = context;
}
async handleMessage() {
// ...
}
}
module.exports = {
queue: "my-example-handler",
handleMessage: (m, c) => new ExampleHandler(m, c).handleMessage()
};
or in TypeScript
class ExampleMqHandler {
constructor(private message: IMessageQueueMessage, private context: MQHandlerContext) {
super();
}
async handleMessage() {
// ...
}
}
export {
queue: 'my-example-handler',
handleMessage: (m, c) => new ExampleMqHandler(m, c).handleMessage()
};
If an error is threw in handleMessage, the MQS service will try to process the message again.
Queuing a message
Context provides a service called message where you can queue a message to the handler.
@param queue
- string - should match the queue name in the handler
@param body
- string - body payload
await context.services.message.sendMessage({
queue: 'my-example-handler',
body: JSON.stringify({ data: {} }),
});
Registration
Register in the stack9/app.json
.
@prop command
- string - should match the filename without extension.
"mqHandlers": [
{
"command": "my-example-handler"
}
]
Native handlers
Stack9 has some predefined handlers and their queue name are prefixed with underscore.
- _sendNotificationHandler - used to dispatch notifications messages