28 lines
1.0 KiB
JavaScript
28 lines
1.0 KiB
JavaScript
// _opt/messageQueue-example.js
|
|
import { onMessageQueueEvent } from './pbUtils.js';
|
|
|
|
/**
|
|
* Example module that listens for 'test' messages in the message_queue collection.
|
|
*/
|
|
export const init = async (client, config) => {
|
|
client.logger.info('Initializing Message Queue Example module');
|
|
onMessageQueueEvent(client, async (action, record) => {
|
|
// Only process newly created records
|
|
if (action !== 'create') return;
|
|
// Only process messages meant for this client
|
|
if (record.destination !== client.config.id) return;
|
|
// Only handle test dataType
|
|
if (record.dataType !== 'test') return;
|
|
|
|
// At this point we have a test message for us
|
|
client.logger.info('test received');
|
|
|
|
// Delete the processed message from the queue
|
|
try {
|
|
await client.pb.deleteMessageQueue(record.id);
|
|
client.logger.debug(`Deleted message_queue record ${record.id}`);
|
|
} catch (err) {
|
|
client.logger.error(`Failed to delete message_queue record ${record.id}: ${err.message}`);
|
|
}
|
|
});
|
|
}; |