Publish and subscribe is a mechanism for passing information. It’s used in social media, and it’s also used internally in software applications. A producer publishes a message, and the subscribers receive the message. In software, publish and subscribe notifications make message passing flexible and robust. The producers of messages are decoupled from the consumers of messages.
Use the sample code in this folder to explore publishing and subscribing to a topic by using filters and queues. This tutorial does not create a complete end-to-end application. Instead, you can use it to play around with a publish and subscribe architecture.
You can create an Amazon SNS topic and subscribe two Amazon SQS queues to the topic. You can enable FIFO (First-In-First-Out) queueing, and you can add filtered subscriptions. Then, you can publish messages to the topic and see the results in the queues.
You can publish and subscribe using Amazon SNS alone. But combining Amazon SNS with Amazon SQS gives you more flexibility in how the messages are consumed.
Amazon SNS is a push service. It pushes to endpoints such as email addresses, mobile application endpoints, or SQS queues. (For a full list of endpoints, see SNS event destinations).
With Amazon SQS, messages are received from a queue by polling. With polling, the subscriber receives messages by calling a receive message API. Any code can poll the queue. Also, the messages stay in the queue until you delete them. This gives you more flexibility in how the messages are processed.
The sample code builds a command line application that asks you for input. This is implemented in multiple programming languages, and the interface can vary slightly between languages. The following shows the interface for the Go implementation.
Would you like to work with FIFO topics? (y/n)
You configure FIFO (First-In-First-Out) topics when you create them. Choosing a FIFO topic enables other options, too. To learn more, see FIFO topics example use case.
Do you want to use content-based deduplication instead of entering a deduplication ID? (y/n)
Deduplication is only available for FIFO topics. Deduplication prevents the subscriber from responding more than once to events that are determined to be duplicates. If a message gets published to an SNS FIFO topic and it’s found to be a duplicate within the five-minute deduplication interval, the message is accepted but not delivered. For more information, see Message deduplication for FIFO topics.
Content-based deduplication uses a hash of the content as a deduplication ID. If content-based deduplication is not enabled, you must include a deduplication ID with each message.
Enter a name for your SNS topic:
Topic names can have 1-256 characters. They can contain uppercase and lowercase ASCII letters, numbers, underscores, and hyphens. If you chose a FIFO topic, the application automatically adds a “.fifo” suffix, which is required for FIFO topics.
Now, configure two SQS queues to subscribe to your topic. Separate queues for each subscriber can be helpful. For instance, you can customize how messages are consumed and how messages are filtered.
Enter a name for the first SQS queue.
Queue names can have 1-80 characters. They can contain uppercase and lowercase ASCII letters, numbers, underscores, and hyphens. If you chose a FIFO topic, the application automatically adds a “.fifo” suffix, which is required for FIFO queues.
Do you want to filter messages that are sent to "<your queue>" from the "<your topic>" topic? (y/n)
If you chose FIFO topics, you can add a filter to the queue’s topic subscription. There are many ways to filter a topic. In this example code, you have the option to filter by a predetermined selection of attributes. For more information about filters, see Message filtering for FIFO topics.
You can filter messages by one or more of the following "tone" attributes.
Enter the number of the tone you want to filter by:
1. cheerful
2. funny
3. serious
4. sincere
Enter a choice between 1 and 4:
If you add a filter, you can select one or more “tone” attributes to filter by.
The application now prompts you to add the second queue. Repeat the previous steps for the second queue.
The following diagram shows the topic and queue options.
After you create the topic and subscribe both queues, the application lets you publish messages to the topic.
Enter a message to publish:
All configurations include a message text.
Enter a message group ID:
If this is a FIFO topic, then you must include a group ID. The group ID can contain up to 128 alphanumeric characters (a-z, A-Z, 0-9)
and punctuation (!"#$%&"()*+,-./:;<=>?@[\]^_``{|}~)
.
For more information about group IDs, see Message grouping for FIFO topics.
Enter a deduplication ID:
If this is a FIFO topic and content-based deduplication is not enabled, then you must enter a deduplication ID. The message deduplication ID can contain up to 128 alphanumeric characters (a-z, A-Z, 0-9)
and punctuation (!"#$%&"()*+,-./:;<=>?@[\]^_``{|}~)
.
Add a tone attribute so this message can be filtered? (y/n)
If you added a filter to one of the subscriptions, you can choose to add a filtering attribute to the message.
Enter the number of the tone you want to filter by:
1. cheerful
2. funny
3. serious
4. sincere
Enter a choice between 1 and 4:
Select a number for an attribute.
Do you want to publish another messsage? (y/n)
You can publish as many messages as you want.
When you are done publishing messages, the application polls the queues and displays their messages.
Do you want to remove all AWS resources created for this scenario? (y/n)
You can select to remove the topic and queues or keep them for further experimentation. If you keep them after the example exits, you must remove them yourself.
- Running this code might result in charges to your AWS account.
- Running the tests might result in charges to your AWS account.
- We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see Grant least privilege.
- This code is not tested in every AWS Region. For more information, see AWS Regional Services.
For prerequisites, see the README in the gov2
folder.
This scenario can be run with the cmd
runner.
go run ./cmd -scenario topics_and_queues
Running this example requires AWS Identity and Access Management (IAM) permissions for both SNS and SQS.