├── .gitignore ├── README.md ├── app.js ├── config-sample.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | config.json 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AWS SQS NodeJS Example 2 | 3 | Follow the instructions below on an EC2 Ubuntu instance. This tutorial will help you setup the AWS SDK using NodeJS. If you have any questions please contact me! 4 | 5 | ``` 6 | sudo su 7 | apt-get update 8 | apt-get upgrade -y 9 | apt-get dist-upgrade -y 10 | apt-get autoremove -y 11 | apt-get install nodejs npm git -y 12 | ln -s /usr/bin/nodejs /usr/bin/node 13 | git clone https://github.com/andrewpuch/aws-sqs-node-js-examples.git 14 | cd aws-sqs-node-js-examples 15 | npm install 16 | cp config-sample.json config.json 17 | ``` 18 | 19 | ***NOTE: Here you will want to edit config.json with your AWS keys.*** 20 | 21 | ``` 22 | node app.js 23 | ``` 24 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | // Require objects. 2 | var express = require('express'); 3 | var app = express(); 4 | var aws = require('aws-sdk'); 5 | var queueUrl = ""; 6 | var receipt = ""; 7 | 8 | // Load your AWS credentials and try to instantiate the object. 9 | aws.config.loadFromPath(__dirname + '/config.json'); 10 | 11 | // Instantiate SQS. 12 | var sqs = new aws.SQS(); 13 | 14 | // Creating a queue. 15 | app.get('/create', function (req, res) { 16 | var params = { 17 | QueueName: "MyFirstQueue" 18 | }; 19 | 20 | sqs.createQueue(params, function(err, data) { 21 | if(err) { 22 | res.send(err); 23 | } 24 | else { 25 | res.send(data); 26 | } 27 | }); 28 | }); 29 | 30 | // Listing our queues. 31 | app.get('/list', function (req, res) { 32 | sqs.listQueues(function(err, data) { 33 | if(err) { 34 | res.send(err); 35 | } 36 | else { 37 | res.send(data); 38 | } 39 | }); 40 | }); 41 | 42 | // Sending a message. 43 | // NOTE: Here we need to populate the queue url you want to send to. 44 | // That variable is indicated at the top of app.js. 45 | app.get('/send', function (req, res) { 46 | var params = { 47 | MessageBody: 'Hello world!', 48 | QueueUrl: queueUrl, 49 | DelaySeconds: 0 50 | }; 51 | 52 | sqs.sendMessage(params, function(err, data) { 53 | if(err) { 54 | res.send(err); 55 | } 56 | else { 57 | res.send(data); 58 | } 59 | }); 60 | }); 61 | 62 | // Receive a message. 63 | // NOTE: This is a great long polling example. You would want to perform 64 | // this action on some sort of job server so that you can process these 65 | // records. In this example I'm just showing you how to make the call. 66 | // It will then put the message "in flight" and I won't be able to 67 | // reach that message again until that visibility timeout is done. 68 | app.get('/receive', function (req, res) { 69 | var params = { 70 | QueueUrl: queueUrl, 71 | VisibilityTimeout: 600 // 10 min wait time for anyone else to process. 72 | }; 73 | 74 | sqs.receiveMessage(params, function(err, data) { 75 | if(err) { 76 | res.send(err); 77 | } 78 | else { 79 | res.send(data); 80 | } 81 | }); 82 | }); 83 | 84 | // Deleting a message. 85 | app.get('/delete', function (req, res) { 86 | var params = { 87 | QueueUrl: queueUrl, 88 | ReceiptHandle: receipt 89 | }; 90 | 91 | sqs.deleteMessage(params, function(err, data) { 92 | if(err) { 93 | res.send(err); 94 | } 95 | else { 96 | res.send(data); 97 | } 98 | }); 99 | }); 100 | 101 | // Purging the entire queue. 102 | app.get('/purge', function (req, res) { 103 | var params = { 104 | QueueUrl: queueUrl 105 | }; 106 | 107 | sqs.purgeQueue(params, function(err, data) { 108 | if(err) { 109 | res.send(err); 110 | } 111 | else { 112 | res.send(data); 113 | } 114 | }); 115 | }); 116 | 117 | // Start server. 118 | var server = app.listen(80, function () { 119 | var host = server.address().address; 120 | var port = server.address().port; 121 | 122 | console.log('AWS SQS example app listening at http://%s:%s', host, port); 123 | }); 124 | -------------------------------------------------------------------------------- /config-sample.json: -------------------------------------------------------------------------------- 1 | { 2 | "accessKeyId": "ACCESS_KEY", 3 | "secretAccessKey": "SECRET_KEY", 4 | "region": "us-east-1" 5 | } 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aws-sqs-tutorial", 3 | "version": "0.0.0", 4 | "private": true, 5 | "dependencies": { 6 | "express": "~4.12.4", 7 | "aws-sdk": "~2.1.36" 8 | } 9 | } 10 | --------------------------------------------------------------------------------