A small, simple, robust asynchronous queue reader for Amazon Simple Queue Service (SQS) written in Typescript.
- Configurable exponential delay between empty receive attempts to limit polling costs when idle
- Can force polling to resume during idle delay
- Supports immediate abort of receive/delay for graceful shutdown
- Abstract logging mechanism uses console by default but can easily use frameworks like Winston
npm install sqs-reader
import { SQSReader } from 'sqs-reader';
const { AWS_SQS_QUEUE_URL } = process.env;
if (!AWS_SQS_QUEUE_URL) {
throw new Error('AWS_SQS_QUEUE_URL is not configured');
}
const queueReader = new SQSReader(AWS_SQS_QUEUE_URL, async (message) => {
console.log('Received message', message.MessageId);
});
let shuttingDown = false;
async function shutDown(): Promise<void> {
if (!shuttingDown) {
shuttingDown = true;
console.info('Shutting down...');
setTimeout((): void => {
console.warn('Timed out waiting for shutdown');
process.exit(1);
}, 30000);
try {
queueReader.stop();
await queueReader.join();
} catch (err) {
console.warn('Error in queue reader:', err);
}
process.exit(0);
}
}
process.on('SIGINT', shutDown);
process.on('SIGTERM', shutDown);
(async function () {
queueReader.start();
await queueReader.join();
shutDown();
})();
sqs-reader
is available under the ISC license.