{"id":13455029,"url":"https://github.com/bbc/sqs-consumer","last_synced_at":"2026-02-22T22:31:11.259Z","repository":{"id":24772332,"uuid":"28185718","full_name":"bbc/sqs-consumer","owner":"bbc","description":"Build Amazon Simple Queue Service (SQS) based applications without the boilerplate","archived":false,"fork":false,"pushed_at":"2025-03-27T18:44:29.000Z","size":1331,"stargazers_count":1812,"open_issues_count":5,"forks_count":339,"subscribers_count":62,"default_branch":"canary","last_synced_at":"2025-05-09T00:56:56.249Z","etag":null,"topics":["aws","ignorearchive","javascript","nodejs","polling","sqs"],"latest_commit_sha":null,"homepage":"https://bbc.github.io/sqs-consumer/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bbc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2014-12-18T14:01:33.000Z","updated_at":"2025-05-07T16:26:14.000Z","dependencies_parsed_at":"2024-10-02T00:34:12.697Z","dependency_job_id":"226b6c62-df40-489e-9c64-9f880073347b","html_url":"https://github.com/bbc/sqs-consumer","commit_stats":{"total_commits":424,"total_committers":82,"mean_commits":5.170731707317073,"dds":0.6721698113207547,"last_synced_commit":"e09c7593f524471cf25ca5b4f7592d6a625e9d33"},"previous_names":[],"tags_count":117,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbc%2Fsqs-consumer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbc%2Fsqs-consumer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbc%2Fsqs-consumer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbc%2Fsqs-consumer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bbc","download_url":"https://codeload.github.com/bbc/sqs-consumer/tar.gz/refs/heads/canary","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254448578,"owners_count":22072764,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","ignorearchive","javascript","nodejs","polling","sqs"],"created_at":"2024-07-31T08:01:00.460Z","updated_at":"2025-12-29T04:20:18.202Z","avatar_url":"https://github.com/bbc.png","language":"TypeScript","readme":"# sqs-consumer\n\n[![NPM downloads](https://img.shields.io/npm/dm/sqs-consumer.svg?style=flat)](https://npmjs.org/package/sqs-consumer)\n[![Build Status](https://github.com/bbc/sqs-consumer/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/bbc/sqs-consumer/actions/workflows/test.yml)\n\nBuild SQS-based applications without the boilerplate. Just define an async function that handles the SQS message processing.\n\n## Installation\n\nTo install this package, simply enter the following command into your terminal (or the variant of whatever package manager you are using):\n\n```bash\nnpm install sqs-consumer\n```\n\nIf you would like to use JSR instead, you can find the package [here](https://jsr.io/@bbc/sqs-consumer).\n\n### Node version\n\nWe will only support Node versions that are actively or security supported by the Node team. You can find the list of versions that are actively supported [here](https://nodejs.org/en/about/releases/).\n\n## Documentation\n\nVisit [https://bbc.github.io/sqs-consumer/](https://bbc.github.io/sqs-consumer/) for the full API documentation.\n\n## Usage\n\n```js\nimport { Consumer } from \"sqs-consumer\";\n\nconst app = Consumer.create({\n  queueUrl: \"https://sqs.eu-west-1.amazonaws.com/account-id/queue-name\",\n  handleMessage: async (message) =\u003e {\n    // do some work with `message`\n  },\n});\n\napp.on(\"error\", (err) =\u003e {\n  console.error(err.message);\n});\n\napp.on(\"processing_error\", (err) =\u003e {\n  console.error(err.message);\n});\n\napp.start();\n```\n\n- **Polling Behavior**: The queue is polled continuously for messages using [long polling](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html).\n- **Message Processing Behavior**: By default messages are processed one at a time – a new message won't be received until the first one has been processed. To process messages in parallel, use the `batchSize` option [detailed here](https://bbc.github.io/sqs-consumer/interfaces/ConsumerOptions.html#batchSize).\n  - It's also important to await any processing that you are doing to ensure that messages are processed one at a time.\n- **Message Acknowledgment Behavior**:\n  - When `alwaysAcknowledge` is `false` (the default)\n    - **Returning `undefined`, an empty object `{}`, or an empty array `[]`**: Message(s) will **NOT** be deleted (left on queue for retry)\n      - For batch processing, return `undefined` or `[]` to prevent acknowledgment of all messages.\n      - For single message handling, return `undefined` or `{}` to prevent acknowledgment.\n    - **Returning the message object (or an array of messages in batch processing)**: Message(s) will be **acknowledged and deleted**\n      - Important: Only the message id(s) that are returned will be deleted.\n    - **Returning `void` is discouraged and will be deprecated in a future release.**\n    - **When `strictReturn` is `true`:** Returning `null` will throw an error. This will be the default behavior in a future release.\n  - **When `alwaysAcknowledge` is `true`:** All messages will be acknowledged and deleted regardless of return value.\n- **Error Handling**: Throwing an error (or returning a rejected promise) from the handler function will cause the message to be left on the queue. An [SQS redrive policy](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/SQSDeadLetterQueue.html) can be used to move messages that cannot be processed to a dead letter queue.\n- **Deletion Process** Messages are deleted from the queue once the handler function has completed successfully (the above items should also be taken into account).\n\n### FIFO Queue Support\n\nWhen using SQS Consumer with FIFO (First-In-First-Out) queues, you might see a warning message in your logs.\n\nAs mentioned in the warning, we do not explicitly test SQS Consumer with FIFO queues, this means that we cannot guarantee that the library will work as expected, however, with the correct configuration, it should. If you have done that and believe FIFO to be working as expected, you can suppress the warning by setting `suppressFifoWarning: true`.\n\nTo note: In order to maintain FIFO ordering, you should always use the `handleMessageBatch` method instead of `handleMessage`.\n\n### Credentials\n\nBy default the consumer will look for AWS credentials in the places [specified by the AWS SDK](https://docs.aws.amazon.com/AWSJavaScriptSDK/guide/node-configuring.html#Setting_AWS_Credentials). The simplest option is to export your credentials as environment variables:\n\n```bash\nexport AWS_SECRET_ACCESS_KEY=...\nexport AWS_ACCESS_KEY_ID=...\n```\n\nIf you need to specify your credentials manually, you can use a pre-configured instance of the [SQS Client](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-sqs/classes/sqsclient.html) client.\n\n```js\nimport { Consumer } from \"sqs-consumer\";\nimport { SQSClient } from \"@aws-sdk/client-sqs\";\n\nconst app = Consumer.create({\n  queueUrl: \"https://sqs.eu-west-1.amazonaws.com/account-id/queue-name\",\n  handleMessage: async (message) =\u003e {\n    // ...\n  },\n  sqs: new SQSClient({\n    region: \"my-region\",\n    credentials: {\n      accessKeyId: \"yourAccessKey\",\n      secretAccessKey: \"yourSecret\",\n    },\n  }),\n});\n\napp.on(\"error\", (err) =\u003e {\n  console.error(err.message);\n});\n\napp.on(\"processing_error\", (err) =\u003e {\n  console.error(err.message);\n});\n\napp.on(\"timeout_error\", (err) =\u003e {\n  console.error(err.message);\n});\n\napp.start();\n```\n\n### AWS IAM Permissions\n\nConsumer will receive and delete messages from the SQS queue. Ensure `sqs:ReceiveMessage`, `sqs:DeleteMessage`, `sqs:DeleteMessageBatch`, `sqs:ChangeMessageVisibility` and `sqs:ChangeMessageVisibilityBatch` access is granted on the queue being consumed.\n\n## API\n\n### `Consumer.create(options)`\n\nCreates a new SQS consumer using the [defined options](https://bbc.github.io/sqs-consumer/interfaces/ConsumerOptions.html).\n\n### `consumer.start()`\n\nStart polling the queue for messages.\n\n### `consumer.stop(options)`\n\nStop polling the queue for messages. [You can find the options definition here](https://bbc.github.io/sqs-consumer/interfaces/StopOptions.html).\n\nBy default, the value of `abort` is set to `false` which means pre existing requests to AWS SQS will still be made until they have concluded. If you would like to abort these requests instead, pass the abort value as `true`, like so:\n\n`consumer.stop({ abort: true })`\n\n#### Graceful shutdowns\n\nCalling `consumer.stop()` on its own only prevents new polls from being scheduled. The consumer will emit `stopped` immediately, even if the current long poll or message handler is still running. To wait for the final poll plus any in-flight message processing to finish, set `pollingCompleteWaitTimeMs` to the maximum amount of time you are willing to wait.\n\n- While waiting, the consumer emits `waiting_for_polling_to_complete` every second.\n- If the timeout elapses first, `waiting_for_polling_to_complete_timeout_exceeded` fires right before `stopped`.\n- For graceful shutdowns keep `abort: false` (the default). Passing `abort: true` cancels the shared `AbortController`, which halts heartbeat extensions and prevents acknowledgements/deletions from finishing.\n\nHere's an example of a graceful shutdown implementation:\n\n```js\nconst consumer = Consumer.create({\n  queueUrl: \"https://sqs.eu-west-1.amazonaws.com/account-id/queue-name\",\n  handleMessage: async (message) =\u003e {\n    await doWork(message);\n    return message;\n  },\n  pollingCompleteWaitTimeMs: 10_000, // This will allow up to 10s for the last poll + handler\n});\n\nconst shutdown = (signal) =\u003e {\n  console.log(`Received ${signal}, waiting for in-flight work...`);\n  consumer.stop();\n\n  consumer.once(\"waiting_for_polling_to_complete\", () =\u003e {\n    console.log(\"Still processing in-flight messages...\");\n  });\n\n  consumer.once(\"waiting_for_polling_to_complete_timeout_exceeded\", () =\u003e {\n    console.warn(\"Graceful shutdown timed out, continuing shutdown anyway.\");\n  });\n\n  consumer.once(\"stopped\", () =\u003e {\n    console.log(\"Consumer stopped cleanly\");\n    process.exit(0);\n  });\n};\n\nprocess.once(\"SIGINT\", shutdown);\nprocess.once(\"SIGTERM\", shutdown);\n\nconsumer.start();\n```\n\n### `consumer.status`\n\nReturns the current status of the consumer.\n\n- `isRunning` - `true` if the consumer has been started and not stopped, `false` if was not started or if it was stopped.\n- `isPolling` - `true` if the consumer is actively polling, `false` if it is not.\n\n\u003e **Note:**\n\u003e This method is not available in versions before v9.0.0 and replaced the method `isRunning` to supply both running and polling states.\n\n### `consumer.updateOption(option, value)`\n\nUpdates the provided option with the provided value.\n\nPlease note that any update of the option `pollingWaitTimeMs` will take effect only on next polling cycle.\n\nYou can [find out more about this here](https://bbc.github.io/sqs-consumer/classes/Consumer.html#updateOption).\n\n### Events\n\nEach consumer is an [`EventEmitter`](https://nodejs.org/api/events.html) and [emits these events](https://bbc.github.io/sqs-consumer/interfaces/Events.html).\n\n## Contributing\n\nWe welcome and appreciate contributions for anyone who would like to take the time to fix a bug or implement a new feature.\n\nBut before you get started, [please read the contributing guidelines](https://github.com/bbc/sqs-consumer/blob/main/.github/CONTRIBUTING.md) and [code of conduct](https://github.com/bbc/sqs-consumer/blob/main/.github/CODE_OF_CONDUCT.md).\n\n## License\n\nSQS Consumer is distributed under the Apache License, Version 2.0, see [LICENSE](https://github.com/bbc/sqs-consumer/blob/main/LICENSE) for more information.\n","funding_links":[],"categories":["Packages","TypeScript","Repository","包","目录"],"sub_categories":["Job queues","Job Queues","任务队列","工作队列"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbbc%2Fsqs-consumer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbbc%2Fsqs-consumer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbbc%2Fsqs-consumer/lists"}