Using Simple Notification Service (SNS) in CFML: Sending Messages

Posted 8 July 2018

Sending a message to SNS from CMFL via the AWS Java SDK is easy. You need to:

  1. Give your message a subject
  2. Give your message a body (plain text only)
  3. Create a PublishRequest object
  4. Tell your SNS client to publish your PublishRequest

If you haven’t already read the entry on the basic setup needed to access AWS from CFML, please do so now.

Here’s the relevant code from /sns.cfm in the AWSPlaybox app:

subject = "AWS Playbox SNS CFML Demo";
message = "Hello there!" & chr(13) & chr(13) & "The current time is " & DateTimeFormat(Now(), "Full") & ".";

sns = application.awsServiceFactory.createServiceObject('sns');
publishRequest = CreateObject('java', 'com.amazonaws.services.sns.model.PublishRequest').init(application.awsResources.snsTopicARN, message,subject);

sns.publish(publishRequest);

If you actually subscribed to the topic that we created in the previous post in this series (or in the AWSPlaybox app), you should get a copy of the message within seconds. Here’s what the messages look like when delivered:

Email

Sample email message from SNS

SMS

Sample text message from SNS

You can see that in the SMS message, the text message was prefixed by the topic name. The subject value was omitted.

That’s it! It’s really quite simple to send a message to SNS via the AWS Java SDK from within your CFML application. If you are sending SMS messages, there are lots of options around price and SMS message type that you can set from within the Java SDK. You do not need to use these — they’re simply there to help you build a more robust SMS application (if that’s what you are doing).

Those are the basics of working with SNS from CMFL. In the next post, I’ll go over some caveats about working with SNS that I’ve encountered.

Categories: AWS ColdFusion