amazon simple queue service sqs example in node.js

Sample node.js code for sending messages to Amazon’s SQS, simple queue service. aws-sdk is the node.js module library for aws api. The access id and asscess key has to be obtained from aws’ iam dashboard. The region is the region where the SQS is, for example, us-east-1. The QueueUrl can be found in aws SQS admin page, it is the url for the queue you want to send the message to. The message has to be a string.

var aws = require('aws-sdk');
var sqs = new aws.SQS({"accessKeyId":"yourAccessId", "secretAccessKey": "yourAccessKey", "region": ""});

function send() {
  var params = {
    MessageBody: JSON.stringify({"msg":"hello"}),
    QueueUrl: "https://sqs.us-east-1.amazonaws.com/123456789/TEST",
    MessageAttributes: {
      someKey: { DataType: 'String', StringValue: "string"}
    }
  };
  sqs.sendMessage(params, function(err, data) {
    if (err) common.logError(err, err.stack); // an error occurred
    else     common.log(data);           // successful response
  });  
}

function recevie() {
  var params = {
      QueueUrl: "https://sqs.us-east-1.amazonaws.com/123456789/TEST",
      MaxNumberOfMessages: 1
  };
  sqs.receiveMessage(params, function(err, data) {
    console.log(data);
  });
}
  

Search within Codexpedia

Custom Search

Search the entire web

Custom Search