简体   繁体   中英

How can i consume sqs messages at my existing nodejs server

I want to receive and trigger email when new message receive at SQS, Now I already have nodejs server running, How can I make it work? I dont really want to trigger that function. But I want that when there's new message in SQS this consumer will consume and do the business logic of sending email.

But I am not getting any trigger at my function. Note: I am not calling this function, I want that it automatically trigger when new message is available at SQS.

const AWS = require('aws-sdk');
const mongoose = require('mongoose');

//
// Configure the aws details
//
AWS.config.update({
    region: process.env['AWS_REGION'],
    accessKeyId: process.env['AWS_ACCESS_KEY_ID'],
    secretAccessKey: process.env['AWS_SECRET_ACCESS_KEY']
  });


const sqs = new AWS.SQS({apiVersion: '2012-11-05'});

var queueURL = "https://sqs.us-east-1.amazonaws.com/xxxxx/demo-lambda-to-email-sqs"



var params = {
    AttributeNames: [
        "SentTimestamp"
     ],
    MaxNumberOfMessages: 1,
    MessageAttributeNames: [
       "All"
    ],
    QueueUrl: queueURL,
    VisibilityTimeout: 20,
    WaitTimeSeconds: 0
   };

   sqs.receiveMessage(params, function(err, data) {
    if (err) {
      console.log("Receive Error", err);
    } else if (data.Messages) {
      console.log('--------------------------- MESSAGE RECEIVED -------------')
      var deleteParams = {
        QueueUrl: queueURL,
        ReceiptHandle: data.Messages[0].ReceiptHandle
      };
      sqs.deleteMessage(deleteParams, function(err, data) {
        if (err) {
          console.log("Delete Error", err);
        } else {
          console.log("Message Deleted", data);
        }
      });
    }
  });

SQS is a queuing service, for this reason it needs to be consumed via a pull based mechanism not a push based.

This function can only be invoked if you have functionality that will poll the SQS queue, then trigger the function when a message comes in.

If you do not want to maintain a consumer script, you should look at migrating this script into a Lambda function. When this option is used the Lambda Service will act as the consumer of the queue and trigger the Lambda function only when a message is added.

More information about Using AWS Lambda with SQS Queues is available in the documentation.

You are only call it once without using long polling. So it starts and its gets an empty response. So you need a pull based mechanism, a basic implementation could run receiveMessage inside a SetInterval. Something like:

setInterval(function() {
    sqs.receiveMessage(params, function(err, data){
      // your logic here!
    });
}, 10000);
// Run every 10s

WaitTimeSeconds greater than 1s enables long polling and helps reduce the cost of using Amazon SQS by eliminating the number of empty responses.

The following code calls SQS every 30 seconds asking for messages. Every call waits up to 20 seconds to receive messages.

const AWS = require('aws-sdk')
AWS.config.update({
    region: 'us-east-1',
    accessKeyId: '...',
    secretAccessKey: '...'
})
const sqs = new AWS.SQS()

receiveMessage = () => sqs.receiveMessage({
    QueueUrl: 'https://sqs.us-east-1.amazonaws.com/.../...',
    WaitTimeSeconds: 20
}, (error, data) => {
    if (error) console.error("ERROR:", error)
    if (data.Messages) data.Messages.forEach(m => console.info(m.Body))
})

setInterval(receiveMessage, 30000)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM