diff --git a/elasticmq/receive-test.ts b/elasticmq/receive-test.ts new file mode 100644 index 0000000..9385cb6 --- /dev/null +++ b/elasticmq/receive-test.ts @@ -0,0 +1,53 @@ +import { + SQSClient, + ReceiveMessageCommand, + DeleteMessageCommand, + DeleteMessageBatchCommand, + ReceiveMessageCommandInput, + DeleteMessageCommandInput, + DeleteMessageBatchCommandInput +} from "@aws-sdk/client-sqs"; +import { v4 as uuid } from "uuid"; +import { SqsConfig } from "./sqs.config"; + + +const client: SQSClient = new SQSClient( + { + region: SqsConfig.region, + endpoint: SqsConfig.endpoint, + credentials: SqsConfig.credentials, + } +); + +const params: ReceiveMessageCommandInput = { + AttributeNames: ["SentTimestamp"], + MaxNumberOfMessages: 10, + MessageAttributeNames: ["All"], + QueueUrl: `${SqsConfig.endpoint}/${SqsConfig.accountId}/${SqsConfig.queueName}`, + VisibilityTimeout: 20, + WaitTimeSeconds: 0, +}; + +const command: ReceiveMessageCommand = new ReceiveMessageCommand(params); + + +client.send(command).then((response) => { + if (response.Messages) { + console.log(JSON.stringify(response.Messages, null, 2)); + const deleteParams: DeleteMessageBatchCommandInput = { + QueueUrl: `${SqsConfig.endpoint}/${SqsConfig.accountId}/${SqsConfig.queueName}`, + Entries: response.Messages.map((message) => ({ + ReceiptHandle: message.ReceiptHandle, + Id: uuid(), + })) + }; + + client.send(new DeleteMessageBatchCommand(deleteParams)).then(() => { + console.log("Message deleted"); + }).catch((error) => { + console.log(error); + }); + } +}).catch((error) => { + console.error(error); +}); \ No newline at end of file diff --git a/elasticmq/send-test.ts b/elasticmq/send-test.ts new file mode 100644 index 0000000..0452c7b --- /dev/null +++ b/elasticmq/send-test.ts @@ -0,0 +1,42 @@ +import { SQSClient, SendMessageCommand, SendMessageCommandInput } from "@aws-sdk/client-sqs"; +import { v4 as uuid } from "uuid"; +import { SqsConfig } from "./sqs.config"; + +const client: SQSClient = new SQSClient( + { + region: SqsConfig.region, + endpoint: SqsConfig.endpoint, + credentials: SqsConfig.credentials, + } +); + +const params: SendMessageCommandInput = { + // DelaySeconds: 10, // remove when non FIFO + MessageAttributes: { + Title: { + DataType: "String", + StringValue: "Test message", + }, + Author: { + DataType: "String", + StringValue: "Michal Adamski", + }, + SomeNumberAttribute: { + DataType: "Number", + StringValue: "6", + }, + }, + MessageBody: "This is test message body for Engage SaySimple project", + MessageDeduplicationId: uuid(), // Required for FIFO queues + MessageGroupId: "Group1", // Required for FIFO queues + QueueUrl: `${SqsConfig.endpoint}/${SqsConfig.accountId}/${SqsConfig.queueName}` //SQS_QUEUE_URL; e.g., 'https://sqs.REGION.amazonaws.com/ACCOUNT-ID/QUEUE-NAME' +}; + +const command: SendMessageCommand = new SendMessageCommand(params); + + +client.send(command).then((response) => { + console.log(response); +}).catch((error) => { + console.error(error); +}); \ No newline at end of file diff --git a/elasticmq/sqs.config.ts b/elasticmq/sqs.config.ts new file mode 100644 index 0000000..ccea2ba --- /dev/null +++ b/elasticmq/sqs.config.ts @@ -0,0 +1,24 @@ +import "dotenv/config"; + +export interface SqsConfig { + endpoint: string; + region: string; + credentials: { + accessKeyId: string; + secretAccessKey: string; + }; + queueName: string; + accountId: string; + +} + +export const SqsConfig: SqsConfig = { + endpoint: process.env.ENDPOINT ?? "http://localhost:9324", + region: process.env.REGION ?? "elasticmq", + credentials: { + accessKeyId: process.env.ACCESS_KEY_ID ?? "x", + secretAccessKey: process.env.SECRET_ACCESS_KEY ?? "x", + }, + queueName: process.env.QUEUE_NAME ?? "messages.fifo", + accountId: process.env.ACCOUNT_ID ?? "accountId", +};