AWS SNS Connector

The AWS SNS connector provides an Akka Stream Flow and Sink for push notifications through AWS SNS.

For more information about AWS SNS please visit the official documentation.

Artifacts

sbt
libraryDependencies += "com.lightbend.akka" %% "akka-stream-alpakka-sns" % "0.14"
Maven
<dependency>
  <groupId>com.lightbend.akka</groupId>
  <artifactId>akka-stream-alpakka-sns_2.12</artifactId>
  <version>0.14</version>
</dependency>
Gradle
dependencies {
  compile group: 'com.lightbend.akka', name: 'akka-stream-alpakka-sns_2.12', version: '0.14'
}

Usage

Sources provided by this connector need a prepared AmazonSNSAsyncClient to publish messages to a topic.

Scala
val credentials = new BasicAWSCredentials("x", "x")
implicit val snsClient: AmazonSNSAsync =
  AmazonSNSAsyncClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials)).build()
Java
BasicAWSCredentials credentials = new BasicAWSCredentials("x", "x");
AmazonSNSAsync snsClient = AmazonSNSAsyncClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials)).build();

We will also need an ActorSystem and an ActorMaterializer.

Scala
implicit val system: ActorSystem = ActorSystem()
implicit val mat: ActorMaterializer = ActorMaterializer()
Java
ActorSystem system = ActorSystem.create();
ActorMaterializer materializer = ActorMaterializer.create(system);

This is all preparation that we are going to need.

Publish messages to a SNS topic

Now we can publish a String message to any SNS topic where we have access to by providing the topic ARN to the SnsPublisher Flow or Sink factory method.

Using a Flow

Scala
val flow: Future[Done] = Source.single("message").via(SnsPublisher.flow("topic-arn")).runWith(Sink.ignore)
Java
CompletionStage<Done> flow = Source.single("message").via(SnsPublisher.createFlow("topic-arn", snsClient)).runWith(Sink.ignore(), materializer);

As you can see, this would publish the messages from the source to the specified AWS SNS topic. After a message has been successfully published, a PublishResult will be pushed downstream.

Using a Sink

Scala
val sink: Future[Done] = Source.single("message").runWith(SnsPublisher.sink("topic-arn"))
Java
CompletionStage<Done> sink = Source.single("message").runWith(SnsPublisher.createSink("topic-arn", snsClient), materializer);

As you can see, this would publish the messages from the source to the specified AWS SNS topic.

Running the example code

The code in this guide is part of runnable tests of this project. You are welcome to edit the code and run it in sbt.

Scala
sbt
> sns/test
The source code for this page can be found here.