Skip to content

Instantly share code, notes, and snippets.

@ajaymathur
Created July 28, 2023 22:19
Show Gist options
  • Select an option

  • Save ajaymathur/c0eda6ab41359db63d5d6b23dee1020b to your computer and use it in GitHub Desktop.

Select an option

Save ajaymathur/c0eda6ab41359db63d5d6b23dee1020b to your computer and use it in GitHub Desktop.
using SQS with node
version: "3.8"
services:
localstack:
container_name: "localstack_main"
image: localstack/localstack
ports:
- "127.0.0.1:4566:4566" # LocalStack Gateway
environment:
- DOCKER_HOST=unix:///var/run/docker.sock
volumes:
- "./volume:/var/lib/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"
const {
SQSClient,
ListQueuesCommand,
SendMessageCommand,
ReceiveMessageCommand,
DeleteMessageCommand,
} = require("@aws-sdk/client-sqs");
const sqsClient = new SQSClient({
endpoint: "http://localhost:4566",
region: "us-west-2",
});
const queue_url = "<queue_url>";
async function main() {
// Get list of queues
const listQueuesCommand = new ListQueuesCommand({
QueueNamePrefix: "test",
});
const queues = await sqsClient.send(listQueuesCommand);
// Send message to the queue
const sendMessageCommand = new SendMessageCommand({
QueueUrl: queue_url,
MessageBody: JSON.stringify({
hello: "world",
}),
});
const messageInfo = await sqsClient.send(sendMessageCommand);
// Receive message from the queue
const receiveMessage = new ReceiveMessageCommand({
QueueUrl: queue_url,
});
const message = await sqsClient.send(receiveMessage);
// Process the message
// Get the receiptHandle of the message after processing
const receiptHandle = message.Messages[0].ReceiptHandle;
// Delete the message after processing
const deleteMessageCommand = new DeleteMessageCommand({
QueueUrl: queue_url,
ReceiptHandle: receiptHandle,
});
await sqsClient.send(deleteMessageCommand);
}
main()
.then(() => console.log("done"))
.catch((err) => console.log(err));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment