sqs-reader
v1.2.0
Published
AWS SQS queue reader for Typescript
Downloads
3
Readme
sqs-reader: AWS SQS queue reader for Typescript
A small, simple, robust asynchronous queue reader for Amazon Simple Queue Service (SQS) written in Typescript.
Features
- 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
Installation
npm install sqs-reader
Usage
import { SQSReader } from 'sqs-reader';
const { AWS_SQS_INDEX_QUEUE_URL } = process.env;
if (!AWS_SQS_INDEX_QUEUE_URL) {
throw new Error('AWS_SQS_INDEX_QUEUE_URL is not configured');
}
const queueReader = new SQSReader(AWS_SQS_INDEX_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();
})();
License
sqs-reader
is available under the ISC license.