Using Simple Notification Service (SNS) in CFML: Overview and Connecting to SNS
Posted 3 June 2018
We’re going to move on from the single AWS service that’s natively supported in CFML to the first service example that uses the AWS Java SDK from within CFML: Simple Notification Service, or SNS.
If you haven’t already read the entry on the basic setup needed to access AWS from CFML, please do so now.
What Can Simple Notification Service (SNS) Do for You?
SNS is a simple publish/subscribe service. Clients subscribe to a SNS topic, and when a message is published to that topic, all subscribers are notififed with a copy of the message. Publishers have no idea who is receiving the message, as clients of different types can subscribe to the topic. This ensures good decoupling between the publisher and the subscriber, which is good architectural practice. You can send emails, text messages, or native push notifications via SNS.
Some examples of using SNS are:
- Send an email to a subscriber when files are uploaded into a S3 bucket that exceed your file size limits.
- Send a verification code text message as part of two-factor authentication during login to your application.
- Publishing notifications to one or more SQS (Simple Queue Service) queues, from which individual messages can be picked up and processed by connected worker clients/applications.
- Pushing a message to a server endpoint (URL) in a CFML application to trigger or process work based on the content of the message.
- Pushing a message to a subscriber Lambda function for processing the message’s contents.
- Send a message to SNS when one of your EC2 instances gets shut down unexpectedly.
- Send a message to SNS when a CloudWatch alarm is triggered because you’ve exceeded more than a specified amount of storage in S3.
- Send a native iOS/Android notification to a customer when there is a new notification in your mobile app.
SNS is simple (as you’ll see), flexible, and powerful. It is important to note that SNS is not a classic message queue. If you want a message queue with acknowledgements, message ordering, retries, or dead letter queues, you need to use SQS (Simple Queue Service) instead.
Working with SNS from CFML
I’ll once again use my AWSPlaybox application for all the example code.
As with all AWS services, you need to first create a client for the service that you want to use. This was covered in the basic setup needed to access AWS from CFML post, but here are the basic steps:
- Create a Client Builder object for the service — AmazonSNSClientBuilder.
- Tell the Client Builder what kind of builder object you want to use. It’s simplest to use the standard builder.
- Pass in your credentials via the StaticCredentialsProvider object (created upon instantiation of awsPlaybox/model/awsServiceFactory.cfc).
- Tell the Client Builder which AWS region you’re working in.
- Tell the Client Builder to build (make) the connection.
Here’s the relevant code from AWSPlaybox/model/awsServiceFactory.cfc:
Now we can work with SNS from within our CFML application.
In the rest of this series of posts on working with SNS from CFML, I’ll cover creating topics, subscribing to topics, and sending messages.