1. SNS Subscriptions
In the last lesson, we explored how to create, list and delete SNS topics. Now, let's explore how to manage subscriptions to those topics. This is how we choose who gets the notifications and how they get them. Let's dive in.
2. Subscription Listing
In the last lesson, we looked at the Topic page and the subscription listing below. Every subscription has a unique ID, a protocol and a status.
3. Subscription Listing
There are several protocols in SNS, but we will only work with Email and SMS - or text messages.
4. Subscription Listing
The endpoint is the specific phone number or email address where the message should be sent.
5. Subscription Listing
Lastly, the status can either be confirmed or pending confirmation. Phone numbers are automatically confirmed, but for email, the user has to click a unique link to authorize the subscription.
6. Creating an SMS subscription.
To create an SMS subscription, we initialize the boto3 SNS client.
Then, we call SNS subscribe passing the TopicArn of the city_alerts topic we created in the last lesson. The protocol is SMS and the endpoint is a phone number.
7. Create an SMS subscription.
In our response, we get a dictionary that contains a 'SubscriptionArn' key. If we want this ARN for later use, we can grab it from the dictionary using that key.
8. Creating an email subscription
Creating an email subscription is very similar. We call SNS subscribe passing email as the protocol and an email address as an endpoint.
9. Creating an email subscription
This time, however, we see "pending confirmation" instead of a subscription ARN.
10. Creating an email subscription
Meanwhile, the user has received an email with a link to click. Once the subscriber confirms, he will be able to receive emails and the address will show up as confirmed in the subscription list.
11. Listing subscriptions by Topic
Let's list of the subscriptions we made for city_alerts topic. Listing is essential for bulk operations like un-subscribing users. To list the subscriptions for a TopicARN, we call the SNS client's list_subscriptions_by_topic method, and we pass in the TopicARN.
12. Listing subscriptions
Our response will contain a "Subscriptions" key, with a list of subscription dictionaries. These will include the ARN of the subscription - or "pending confirmation" if the subscription has not been confirmed. It will also include the protocol - email or SMS, and the endpoint.
13. Listing subscriptions
We can also list all subscriptions. This will give us back the same response, but for all topics, instead of just one that we specify.
14. Deleting subscriptions
Lastly, to delete a subscription, we simply call the SNS unsubscribe method.
15. Deleting multiple subscriptions
What if we choose to stop sending notifications via SMS? We delete all subscriptions that use the SMS protocol. We list all subscriptions and grab the Subscriptions key from the response.
For every subscription, if SMS, we call the unsubscribe method, passing it the subscription ARN.
16. Review
In this lesson, we learned about SNS subscriptions. A subscription is when someone listens for messages on a topic.
During the request to create a subscription, the protocol can be SMS or email. The endpoint is the phone number or email address.
Once the subscription is created, it's automatically confirmed for SMS. For e-mails, it's "pending confirmation" until the receiver verifies it.
17. Review
We learned to create a subscription with the subscribe method.
We learned how to list subscriptions by topic with the list_subscriptions_by_topic method
18. Review
We have learned how to list all subscriptions unfiltered by topic. Lastly, we learned how to unsubscribe a user.
19. Let's practice!
SNS subscriptions and topics are a powerful way to manage who gets what notifications when. In the next lesson, we'll get to the really cool stuff - sending messages to the people that need to receive them! Let's practice!