{"id":27039951,"url":"https://github.com/0xkalvin/sqs-poller","last_synced_at":"2025-04-05T03:19:54.457Z","repository":{"id":43330502,"uuid":"400534126","full_name":"0xkalvin/sqs-poller","owner":"0xkalvin","description":"A complete SQS poller implementation that makes message processing easier, written on top of AWS SDK V3","archived":false,"fork":false,"pushed_at":"2022-03-08T00:13:05.000Z","size":271,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-27T14:55:45.462Z","etag":null,"topics":["aws","consumer","javascript","poll","poller","queue","sqs"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/sqs-poller","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/0xkalvin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-08-27T14:22:08.000Z","updated_at":"2023-03-04T03:52:00.000Z","dependencies_parsed_at":"2022-08-31T05:12:27.388Z","dependency_job_id":null,"html_url":"https://github.com/0xkalvin/sqs-poller","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0xkalvin%2Fsqs-poller","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0xkalvin%2Fsqs-poller/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0xkalvin%2Fsqs-poller/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0xkalvin%2Fsqs-poller/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/0xkalvin","download_url":"https://codeload.github.com/0xkalvin/sqs-poller/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247281213,"owners_count":20913125,"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","consumer","javascript","poll","poller","queue","sqs"],"created_at":"2025-04-05T03:19:53.735Z","updated_at":"2025-04-05T03:19:54.448Z","avatar_url":"https://github.com/0xkalvin.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sqs-poller\n\nA complete SQS poller implementation to make message processing easier, written on top of [Amazon SDK V3](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-sqs/index.html).\n\n## Installation\n```sh\nnpm i --save sqs-poller\n```\n\n## Usage\n\n```javascript\nconst { SQS } = require('@aws-sdk/client-sqs')\nconst { Poller } = require('sqs-poller')\n\nconst sqs = new SQS({\n  endpoint: 'your sqs endpoint',\n  region: 'your sqs region',\n})\n\nconst poller = new Poller({\n  queueUrl: 'your sqs endpoint + queue name',\n  sqsClient: sqs,\n})\n\npoller.start({\n  eachMessage: async (message) =\u003e {\n    await doSomethingWithMessage(message)\n  },\n  // or\n  eachBatch: async function (messages) {\n    await doSomethingWithBatch(messages)\n  }\n})\n\npoller.on('error', console.error)\n```\n\n## API Reference\n- [new Poller ([options])](#new-poller-options)\n- [poller.start (options)](#pollerstart-options)\n- [poller.stop () : Promise\u003cany\u003e](#pollerstop---promiseany)\n\n## new Poller ([options])\n\n- options `\u003cobject\u003e`\n  - `queueUrl` `\u003cstring\u003e` The complete URL for the queue, consisting of your AWS SQS endpoint + queue name. *Required*.\n  -  `sqsClient` `\u003csqsClient\u003e` Instance of the sqsClient class provided by [AWS SDK v3](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-sqs/classes/sqsclient.html). *Required*.\n  - `messageAttributeNames` `\u003cstring | QueueAttributeName \u003e` The names of the message attributes. *Optional*.\n  - `maxNumberOfMessages` `\u003cnumber\u003e` The maximum number of messages to return. *Optional*. __Default__: `10`\n  - `visibilityTimeout` `\u003cnumber\u003e` The duration (in seconds) that the received messages are hidden from subsequent retrieve requests after being retrieved by a ReceiveMessage request. *Optional*. __Default__: `20`\n  - `waitTimeSeconds` `\u003cnumber\u003e` The duration (in seconds) for which the call waits for a message to arrive in the queue before returning. *Optional*. __Default__: `10`\n  - `pollingTimeout` `\u003cnumber\u003e` The interval (in miliseconds) between a finished poll call and the next one. *Optional*. __Default__: `1`\n  - `shutdownTimeout` `\u003cnumber\u003e` The duration (in miliseconds) for which the poller should wait for in flight messages before stopping it *Optional*. __Default__: `5000`\n\n\nCreate a new Poller class instance.\n\n## poller.start (options)\n\n- `options` `\u003cobject\u003e`\n  - `eachMessage` `\u003cFunction\u003e` `(message) =\u003e Promise\u003cany\u003e` The handler to process one message at a time. Either `eachMessage` or `eachBatch` must be passed. If both are available, the `eachMessage` handler will be used. *Conditionally Required*\n  - `eachBatch` `\u003cFunction\u003e` `(message) =\u003e Promise\u003cany\u003e` The handler to process a message batch (up to ten messages) at a time. It is required if `eachMessage` is not passed. *Conditionally Required*\n  - `beforePoll` `\u003cFunction\u003e` `(message) =\u003e Promise\u003cany\u003e` The optional handler executed before performing a receiveMessage call. It can be used in scenarios where a business rule needs to be verified before polling new messages, such as rate limiting. *Optional*\n\nStart message processing by passing a handler for each message.\n\n## poller.stop () : Promise\u003cany\u003e\n\nStop message processing, waiting for in-fligth messages to be finished before resolving the promise. It can be used for proper graceful shutdown as following.\n```javascript\nprocess.once('SIGTERM', async (signal) =\u003e {\n  await poller.stop()\n\n  console.log('Poller has stopped')\n\n  process.exit(0)\n})\n```\n\n## Diagnostics Channels\n\nThe `sqs-poller` supports diagnostics channels (feature currently available only on Node.js v16+). It is the preferred way to instrument this package. The data is only published to these channels in case there are subscribers listening to them. The available channels are the following:\n- `sqspoller:poller:eachMessage:start`\n  - Before a SQS message gets processed by the `eachMessage` handler, the SQS message gets published to this channel.\n\n  ```js\n  const diagnosticsChannel = require('diagnostics_channel')\n\n  diagnosticsChannel.channel('sqspoller:poller:eachMessage:start').subscribe(({ message }) =\u003e {\n  console.log('body', message.Body)\n  console.log('ReceiptHandle', message.ReceiptHandle)\n  })\n  ```\n\n- `sqspoller:poller:eachMessage:end`\n   - When the `eachMessage` handler has been either resolved or rejected, the SQS message gets published to this channel.\n\n  ```js\n  diagnosticsChannel.channel('sqspoller:poller:eachMessage:end').subscribe(({ message }) =\u003e {\n  console.log('body', message.Body)\n  console.log('ReceiptHandle', message.ReceiptHandle)\n  })\n  ```\n- `sqspoller:poller:eachMessage:error`\n     - When the `eachMessage` handler rejects, the SQS message and the error get published to this channel.\n\n  ```js\n  diagnosticsChannel.channel('sqspoller:poller:eachMessage:error').subscribe(({ message, error }) =\u003e {\n  console.log('body', message.Body)\n  console.log('ReceiptHandle', message.ReceiptHandle)\n  console.log('error', error)\n  })\n  ```\n- `sqspoller:poller:eachBatch:start`\n  - Before a SQS message batch gets processed by the `eachBatch` handler, the batch (`messages`) gets published to this channel.\n- `sqspoller:poller:eachBatch:end`\n  - When the `eachBatch` resolves or rejects, the message batch (`messages`)  gets published to this channel.\n- `sqspoller:poller:eachBatch:error`\n  - When the `eachBatch` rejects, the message batch (`messages`) and `error` get published to this channel.\n- `sqspoller:poller:deleteMessage:start`\n    - Before deleting a message from SQS, the `message` gets published to this channel.\n- `sqspoller:poller:deleteMessage:end`\n    - When the deleteMessage call resolves or rejects, the `message` gets published to this channel.\n- `sqspoller:poller:deleteMessage:error`\n    - When the deleteMessage call rejects, the `message` and `error` get published to this channel.\n- `sqspoller:poller:deleteBatch:start`\n    - Before deleting a message batch from SQS, the batch (`messages`) gets published to this channel.\n- `sqspoller:poller:deleteBatch:end`\n    - When the deleteBatch call resolves or rejects, the batch (`messages`) gets published to this channel.\n- `sqspoller:poller:deleteBatch:error`\n    - When the deleteBatch call rejects, the batch (`messages`) and `error` get published to this channel.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F0xkalvin%2Fsqs-poller","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F0xkalvin%2Fsqs-poller","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F0xkalvin%2Fsqs-poller/lists"}