Skip to content

Instantly share code, notes, and snippets.

@sekcompsci
Created August 4, 2020 09:14
Show Gist options
  • Save sekcompsci/de5dfb11ff59791d167daa010ac1ec4a to your computer and use it in GitHub Desktop.
Save sekcompsci/de5dfb11ff59791d167daa010ac1ec4a to your computer and use it in GitHub Desktop.
AWS SQS FIFO Example
const AWS = require('aws-sdk')
// TODO: Replase value with your config
const region = "ap-southeast-1"
// TODO: Replase {{id}} and {{name}} with your config
// e.g. https://sqs.${region}.amazonaws.com/000111000011/test-sqs.fifo
const queueURL = `https://sqs.${region}.amazonaws.com/{{id}}/{{name}}.fifo`
AWS.config.update({ region })
const sqs = new AWS.SQS({ apiVersion: '2012-11-05' })
const send = async (groupId, messageId) => {
try {
const sendRes = await sqs.sendMessage({
MessageGroupId: `group-${groupId}`,
MessageDeduplicationId: `m-${groupId}-${messageId}`,
MessageBody: `${messageId}`,
QueueUrl: queueURL
}).promise()
console.log(JSON.stringify(sentRes, 0, 2))
return sendRes
} catch (err) {
console.log('Sent message error:', err)
}
}
const receive = async () => {
try {
const res = await sqs.receiveMessage({
AttributeNames: ['SentTimestamp'],
MaxNumberOfMessages: 10,
MessageAttributeNames: ['All'],
QueueUrl: queueURL
}).promise()
console.log(JSON.stringify(res, 0, 2))
if (res.Messages && res.Messages.lengh !== 0) {
for (const message of res.Messages) {
const visibilityParams = {
QueueUrl: queueURL,
ReceiptHandle: message.ReceiptHandle,
VisibilityTimeout: 20
}
await remove(visibilityParams)
}
}
return res
} catch (err) {
console.log('Resive message error:', err)
}
}
const remove = async (visibilityParams) => {
try {
await sqs.changeMessageVisibility(visibilityParams).promise()
console.log('Timeout Changed', data)
} catch (err) {
console.log('Delete message error:', err)
}
}
const main = async () => {
const sentRes = await send('A', '1')
const receiveRes = await receive()
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment