UNPKG

sqs-producer-lib

Version:

Reusable AWS SQS producer library for Node.js microservices

134 lines (95 loc) โ€ข 3.81 kB
# ๐Ÿ“จ AWS SQS Client (Class-based) A reusable Node.js library using a **class-based wrapper** to send messages to AWS SQS queues. Seamlessly supports both **LocalStack for local testing** and **AWS SQS for production** with dynamic queue URLs. --- ## ๐Ÿš€ Features - โœ… Class-based, object-oriented design - ๐ŸŒ Works with both **LocalStack** and **AWS** - โš™๏ธ Customizable with constructor config - ๐Ÿ›ก Helpful console logs and robust error handling - ๐Ÿ“ฆ Easy to plug into any Node.js project --- ## ๐Ÿ“ฆ Installation ```bash # Local path install npm install /path/to/your/sqs-client-lib # OR if published as npm package npm install @your-org/sqs-client ``` --- ## ๐Ÿ› ๏ธ Usage ### 1. Import and create an instance of `SQSClient` ```js const SQSClient = require('./src/SQSClient'); const sqsClient = new SQSClient({ accessKeyId: process.env.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, region: process.env.AWS_REGION || 'us-east-1', useLocalstack: process.env.USE_LOCALSTACK === 'true', localstackEndpoint: process.env.LOCALSTACK_ENDPOINT || 'http://localhost:4566', accountId: process.env.AWS_ACCOUNT_ID || '000000000000', }); ``` ### 2. Send a message to the queue ```js const message = { orderId: '123456', action: 'order_placed', }; sqsClient.sendMessage('your-queue-name', message) .then((res) => console.log('โœ… Message ID:', res.MessageId)) .catch((err) => console.error('โŒ Error:', err.message)); ``` --- ## โš™๏ธ Config Options You can pass these to the constructor: | Option | Type | Description | Default | |----------------------|----------|------------------------------------------|-------------------------| | `accessKeyId` | string | AWS access key | **Required** | | `secretAccessKey` | string | AWS secret key | **Required** | | `region` | string | AWS region | `us-east-1` | | `useLocalstack` | boolean | Enable LocalStack | `false` | | `localstackEndpoint` | string | LocalStack endpoint | `http://localhost:4566` | | `accountId` | string | AWS account ID | `000000000000` | --- ## ๐Ÿ“‚ Project Structure Example ``` your-project/ โ”œโ”€โ”€ src/ โ”‚ โ””โ”€โ”€ SQSClient.js # This file โ”œโ”€โ”€ example.js # How you use the client โ”œโ”€โ”€ .env # Store environment variables here โ””โ”€โ”€ README.md # You're here! ``` --- ## ๐Ÿงช LocalStack Testing ### Step-by-step: 1. Run LocalStack via Docker ```bash docker run --rm -it -p 4566:4566 localstack/localstack ``` 2. Create a queue via AWS CLI: ```bash aws --endpoint-url=http://localhost:4566 sqs create-queue --queue-name your-queue-name ``` 3. Set `.env` values: ```env USE_LOCALSTACK=true AWS_ACCESS_KEY_ID=test AWS_SECRET_ACCESS_KEY=test AWS_REGION=us-east-1 LOCALSTACK_ENDPOINT=http://localhost:4566 AWS_ACCOUNT_ID=000000000000 ``` --- ## ๐Ÿ“œ License MIT License ยฉ 2025 [Kenit Goswami](https://github.com/your-github) --- ## ๐Ÿ™‹โ€โ™‚๏ธ Questions or Contributions? Feel free to open issues or PRs. Feedback and improvements are always welcome! --- ## ๐Ÿ”— Useful Links - [AWS SQS Documentation](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/welcome.html) - [LocalStack GitHub](https://github.com/localstack/localstack) - [AWS SDK for JavaScript](https://github.com/aws/aws-sdk-js)