fs2-aws

fs2-aws-sqs

An FS2 Streams-based API for consuming and publishing AWS SQS messages.

These docs are for the 7.x release; the older 6.x docs are here.

Import

libraryDependencies += "io.laserdisc" %% "fs2-aws-sqs" % "6.6.0"

This module provides the SQS[F] algebra:

trait SQS[F[_]] {
    def sqsStream: Stream[F, Message]
    def sqsStreamBatched: Stream[F, Chunk[Message]]
    def changeMessageVisibilityPipe(timeout: FiniteDuration): Pipe[F, Message, Message]
    def deleteMessagePipe: Pipe[F, Message, DeleteMessageResponse]
    def sendMessagePipe: Pipe[F, SQS.MsgBody, SendMessageResponse]
}

Usage

To use SQS[F], you need an instance of SqsAsyncClientOp[F]:

The general usage pattern is as follows:

// create the tagless-final wrapper resource (pass an SqsAsyncClient.builder()
// if you need to configure credentials, region, etc.)
val sqsInterpreter = SqsInterpreter[IO].resource

// use the interpreter directly for effectful AWS SDK calls
sqsInterpreter.use { sqsOp =>
  SQS.create[IO](config, sqsOp).flatMap { sqs =>
    sqs.sqsStream
    .. etc ..
  }
}

Full Example

import cats.effect.*
import fs2.aws.sqs.{SQS, SqsConfig}
import io.laserdisc.pure.sqs.tagless.SqsInterpreter
import software.amazon.awssdk.services.sqs.model.ListQueuesResponse
import scala.concurrent.duration.*

val config = SqsConfig(
  queueUrl = "https://sqs.us-east-1.amazonaws.com/123456789012/my-queue",
  pollRate = 3.seconds,        // default
  fetchMessageCount = 10       // default; must be 1 to 10
)

object SQSExample {

  // use the tagless-final wrapper directly for effectful AWS SDK calls
  def basicExample: IO[ListQueuesResponse] =
    SqsInterpreter[IO].resource.use { client =>
      client.listQueues
    }

  // or make use of the streaming API for consuming and publishing messages
  def fs2StreamingExample: IO[Unit] =
    SqsInterpreter[IO].resource.use { sqsOp =>
      SQS.create[IO](config, sqsOp).flatMap { sqs =>
        for {
          // publish
          _ <- fs2.Stream("hello", "world")
            .through(sqs.sendMessagePipe)
            .compile
            .drain

          // consume
          _ <- sqs.sqsStream
            .evalTap(msg => IO.println(s"received: ${msg.body()}"))
            .through(sqs.deleteMessagePipe) // acknowledge by deleting
            .compile
            .drain
        } yield ()
      }
    }
}

Long polling

SQS.createWithReceiveRequest applies a function to every ReceiveMessageRequest. With long polling, each receive waits up to 20 seconds for messages, so there's no need to pause between receives:

import cats.effect.*
import fs2.aws.sqs.{SQS, SqsConfig}
import io.laserdisc.pure.sqs.tagless.SqsInterpreter
import scala.concurrent.duration.*

val longPolling: IO[Unit] =
  SqsInterpreter[IO].resource.use { sqsOp =>
    SQS
      .createWithReceiveRequest[IO](
        SqsConfig("https://sqs.us-east-1.amazonaws.com/123456789012/my-queue", pollRate = Duration.Zero),
        sqsOp,
        _.waitTimeSeconds(20)
      )
      .flatMap { sqs =>
        sqs.sqsStream
          .evalTap(msg => IO.println(msg.body()))
          .through(sqs.deleteMessagePipe)
          .compile
          .drain
      }
  }

Notes

Testing

The SQS algebra calls SqsAsyncClientOp[F]. The pure-sqs-tagless test jar contains MockSqsAsyncClientOp[F], a stub where every method throws NotImplementedError until you override it. Add the test jar as a test dependency:

libraryDependencies += "io.laserdisc" %% "pure-sqs-tagless" % "6.6.0" % "tests"

Override the calls your code makes, then build the streaming algebra on top of the stub:

import cats.effect.*
import cats.syntax.all.*
import fs2.aws.sqs.{SQS, SqsConfig}
import io.laserdisc.pure.sqs.tagless.MockSqsAsyncClientOp
import software.amazon.awssdk.services.sqs.model.{SendMessageRequest, SendMessageResponse}
import scala.concurrent.duration.*

val sqsOp = new MockSqsAsyncClientOp[IO] {
  override def sendMessage(a: SendMessageRequest): IO[SendMessageResponse] =
    IO.println(s"sent: ${a.messageBody()}").as(SendMessageResponse.builder().build())
}

SQS.create[IO](SqsConfig("https://sqs.us-east-1.amazonaws.com/123456789012/my-queue", pollRate = Duration.Zero), sqsOp)
  .flatMap(sqs => fs2.Stream.emit("hello").covary[IO].through(sqs.sendMessagePipe).compile.drain)