AWS-Fanout
A library wrapping SNS and SQS to allow for human readable names when using a fanout technique. Now you use the fanout pattern without needing to hard code ARNS throughout your application!
Installation
This module is installed via npm:
$ npm install aws-fanout
Example Usage
await subscribeQueuesToTopicscredentials, topicNames, queueName await publishcredentials, 'send-email',
Environment Variables
AWS_FANOUT_RATE_LIMIT_MS
Default value: 10
The API calls to SQS/SNS are rate limited.
By default there is a maximum of one request made per 10ms. This results in a maximum of 100 requests per second.
API v2 Documentation
createQueue(credentials, { queueName })
createTopic(credentials, { topicName })
deleteQueue(credentials, { queueName })
deleteTopic(credentials, { topicName })
publishMessage(credentials, { topicName, message })
receiveMessage(credentials, { queueName, maxNumberOfMessages, visibilityTimeout })
deleteMessage(credentials, { queueName, receiptHandle })
setQueuePolicy(credentials, { queueName, topicNames, ignoreExistingPolicy })
setQueueRedrivePolicy(credentials, { queueName, deadLetterQueueName, maxReceiveCount })
subscribeQueueToTopic(credentials, { queueName, topicName })
v2.createQueue
Create a single queue on SQS.
credentials
: Credentialsoptions.queueName
: name of the queue to create
await createQueuecredentials,
v2.createTopic
Create a single topic on SNS.
credentials
: Credentialsoptions.topicName
: name of the topic to create
await createTopiccredentials,
v2.deleteQueue
Delete a queue.
credentials
: Credentialsoptions.queueName
: name of the queue to delete
await deleteQueuecredentials,
v2.deleteTopic
Delete a topic.
credentials
: Credentialsoptions.topicName
: name of the topic to delete
await deleteTopiccredentials,
v2.publishMessage
Publish a message with a particular topic. Any queues that are subscribed to the topic will receive a copy of it.
credentials
: Credentialsoptions.topicName
: name of the topicoptions.message
: message payload to send, must be a string
await publishcredentials, topicName: 'create', message: JSON.stringify
v2.receiveMessage
Listen for messages on the queue.
credentials
: Credentialsoptions.maxNumberOfMessages
: Maximum number of messages to retrieveoptions.visibilityTimeout
: The duration (in seconds) that the received messages are hidden from subsequent retrieve requestsoptions.queueName
: Name of the queue to receive messages from
v2.deleteMessage
Remove a message from a queue.
After you have finished receiving a message from the queue, you should remove it so that it does not get sent again.
credentials
: Credentialsoptions.queueName
: name of the queue to delete the message fromoptions.receiptHandle
: the receipt handle of the mesage to delete
if messages.length > 0
v2.setQueuePolicy
Subscribes a queue to a list of topics.
If the queue or topics do not exist, they will be created.
credentials
: Credentialsoptions.queueName
: queue to forward topics tooptions.topicNames
: list of topics to subscribe tooptions.ignoreExistingPolicy
: whether to preserve any existing topics that have previously been allowed to post to this queue.
await subscribeQueuesToTopicscredentials,
If you have a large number of topics to create, you may start hitting the AWS limit on how large the queue policy can be. Instead you can define the queue to accept any topic that matches a wildcard pattern.
await subscribeQueuesToTopicscredentials,
v2.setQueueRedrivePolicy
credentials
: Credentialsoptions.deadLetterQueueName
: (optional) The name of dead-letter queue to which SQS moves messages after the value of "maxReceiveCount" is exceeded.options.maxReceiveCount
: (optional, default = 5) The number of times a message is delivered to the source queue before being moved to the dead-letter queue. When the ReceiveCount for a message exceeds the maxReceiveCount for a queue, SQS moves the message to the dead-letter-queue.
await setQueueRedrivePolicycredentials,
v2.subscribeQueueToTopic
Subscribe a queue to a topic.
When the topic is published, a copy of it will be sent to the queue.
credentials
: Credentialsoptions.queueName
: name of the queueoptions.topicName
: name of the topic
await subscribeQueueToTopiccredentials,
API v1 Documentation
registerQueues(credentials, queueNames)
registerTopics(credentials, topicNames)
deleteQueue(credentials, queueName)
deleteTopic(credentials, topicName)
publish(credentials, topicName, message)
receiveMessage(credentials, maxNumberOfMessages, visibilityTimeout, queueName)
deleteMessage(credentials, queueName, receiptHandle)
subscribeQueueTopicsByTheirPrefix(credentials, topicNames, queueName, [deadLetterQueueName], [maxReceiveCount=5]
subscribeQueuesToTopics(credentials, topicNames, queueName, [deadLetterQueueName], [maxReceiveCount=5])
v1.registerQueues
Create multiple queues on SQS.
credentials
: CredentialsqueueNames
: list of queues to create
await registerQueuescredentials, queueNames
v1.registerTopics
Create multiple topics on SNS.
credentials
: CredentialstopicNames
: list of topics to create
await registerTopicscredentials, topicNames
v1.deleteQueue
Delete a queue.
credentials
: CredentialsqueueName
: name of the queue to delete
await deleteQueuecredentials, queueName
v1.deleteTopic
Delete a topic.
credentials
: CredentialstopicName
: name of the topic to delete
await deleteTopiccredentials, topicName
v1.publish
Publish a message with a particular topic. Any queues that are subscribed to the topic will receive a copy of it.
The message will be serialized using JSON.stringify
.
credentials
: CredentialstopicName
: name of the topicmessage
: message payload to send
await publishcredentials, topicName, message
v1.receiveMessage
Listen for messages on the queue.
credentials
: CredentialsmaxNumberOfMessages
: Maximum number of messages to retrievevisibilityTimeout
: The duration (in seconds) that the received messages are hidden from subsequent retrieve requestsqueueName
: Name of the queue to receive messages from
v1.deleteMessage
Remove a message from a queue.
After you have finished receiving a message from the queue, you should remove it so that it does not get sent again.
credentials
: CredentialsqueueName
: name of the queue to delete the message fromreceiptHandle
: the receipt handle of the mesage to delete
if messages.length > 0
v1.subscribeQueuesToTopics
Subscribes a queue to a list of topics.
If the queue or topics do not exist, they will be created.
credentials
: CredentialstopicNames
: list of topics to subscribe toqueueName
: queue to forward topics todeadLetterQueueName
: (optional) The name of dead-letter queue to which SQS moves messages after the value of "maxReceiveCount" is exceeded.maxReceiveCount
: (optional, default = 5) The number of times a message is delivered to the source queue before being moved to the dead-letter queue. When the ReceiveCount for a message exceeds the maxReceiveCount for a queue, SQS moves the message to the dead-letter-queue.
await subscribeQueuesToTopics credentials, topicNames, queueName, deadLetterQueueName, maxReceiveCount
v1.subscribeQueueTopicsByTheirPrefix
If you have a large number of topics to create, you may start hitting the AWS limit on how large the queue policy can be.
Instead you can define the queue to accept any topic that matches a wildcard.
credentials
: CredentialstopicNames
: list of topics to subscribe toqueueName
: queue to forward topics todeadLetterQueueName
: (optional) The name of dead-letter queue to which SQS moves messages after the value of "maxReceiveCount" is exceeded.maxReceiveCount
: (optional, default = 5) The number of times a message is delivered to the source queue before being moved to the dead-letter queue. When the ReceiveCount for a message exceeds the maxReceiveCount for a queue, SQS moves the message to the dead-letter-queue.
await subscribeQueueTopicsByTheirPrefix credentials, topicNames, queueName, deadLetterQueueName, maxReceiveCount
Credentials
The credentials
object is passed through to the SNS
/SQS
constructor.
If you are using IAM roles or a shared credentials file, you can just leave this empty.
Reference: Setting AWS Credentials in Node.js
If you want to load credentials from environment variables, then you can do something like this: