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:

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:

  1. Create a Client Builder object for the service — AmazonSNSClientBuilder.
  2. Tell the Client Builder what kind of builder object you want to use. It’s simplest to use the standard builder.
  3. Pass in your credentials via the StaticCredentialsProvider object (created upon instantiation of awsPlaybox/model/awsServiceFactory.cfc).
  4. Tell the Client Builder which AWS region you’re working in.
  5. Tell the Client Builder to build (make) the connection.

Here’s the relevant code from AWSPlaybox/model/awsServiceFactory.cfc:

serviceObject = CreateObject('java', '').standard().withCredentials(variables.awsStaticCredentialsProvider).withRegion('us-east-1').build();

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.

Categories: AWS ColdFusion