'How to listen for messages from SQS in nodesJS?

I am trying to use SQS the same way we used RabbitMQ or Kafka where we set up a listener.

But I dont see a way to setup a listener for SQS anywhere in their docs:

I am able to receive to message(s) like:

 const client = new SQSClient({
    region: 'ap-south-1',
    credentials: {
      accessKeyId: process.env.AWS_ACCESS_KEY!,
      secretAccessKey: process.env.AWS_SECRET_KEY!,
    },
  })

  const command = new ReceiveMessageCommand({ QueueUrl: process.env.SQS_URL! }) 
  const data = await client.send(command)
  console.log(data) // 👈 here 

But this is one time, how can I set up an active listener, which either:

  • continously listens (which I doubt SQS provides)
  • or keeps checking for new messages in some interval function

Thanks, would love the help



Solution 1:[1]

I'm looking into the same issue and came across this npm package sqs-consumer.

According to the documentation: " ... • The queue is polled continuously for messages using long polling. ..."

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 athurner