ClientX/_opt/messageQueue-example.js

29 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2025-05-01 20:14:48 +00:00
// _opt/messageQueue-example.js
import { onMessageQueueEvent } from './pbUtils.js';
/**
* Example module that listens for 'test' messages in the message_queue collection.
*/
2025-05-08 01:52:12 +00:00
export async function init(client, _config) {
client.logger.info('[module:messageQueueExample] Message Queue Example module initialized');
onMessageQueueEvent(client, async (action, record) => {
2025-05-01 20:14:48 +00:00
// Only process newly created records
2025-05-08 01:52:12 +00:00
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;
2025-05-01 20:14:48 +00:00
2025-05-08 01:52:12 +00:00
// At this point we have a test message for us
client.logger.info('[module:messageQueueExample] Test message received');
2025-05-01 20:14:48 +00:00
2025-05-08 01:52:12 +00:00
// Delete the processed message from the queue
try {
await client.pb.deleteMessageQueue(record.id);
client.logger.debug(`[module:messageQueueExample] Deleted message_queue record ${record.id}`);
} catch (err) {
client.logger.error(`[module:messageQueueExample] Failed to delete message_queue record ${record.id}: ${err.message}`);
}
});
}