Topic
Source:
src/AWS/SNS/Topic.ts
An Amazon SNS topic for fan-out messaging and notifications.
Topic owns the SNS topic lifecycle while raw AWS topic attributes remain
available through the attributes prop so the full core pub/sub surface can
be configured without waiting on additional typed wrappers. A topic name is
auto-generated unless you provide one explicitly.
Creating Topics
Section titled “Creating Topics”Standard Topic
import * as SNS from "alchemy/AWS/SNS";
const topic = yield* SNS.Topic("OrdersTopic");Topic with Display Name
const topic = yield* SNS.Topic("NotificationsTopic", { attributes: { DisplayName: "App Notifications", },});FIFO Topic
const topic = yield* SNS.Topic("OrdersFifoTopic", { fifo: true, attributes: { ContentBasedDeduplication: "true", },});Runtime Publishing
Section titled “Runtime Publishing”Bind publish operations in the init phase and use them in runtime handlers.
// initconst publish = yield* SNS.Publish.bind(topic);
return { fetch: Effect.gen(function* () { // runtime yield* publish({ Message: JSON.stringify({ orderId: "123" }), Subject: "OrderCreated", }); return HttpServerResponse.text("Published"); }),};Subscriptions
Section titled “Subscriptions”Subscribe a Lambda function to process messages published to the topic. The subscription and invoke permissions are created automatically.
// inityield* SNS.notifications(topic).subscribe((stream) => stream.pipe( Stream.runForEach((message) => Effect.log(`Received: ${message.Message}`), ), ),);