├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── receive.js └── send.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SQS-node-intro 2 | Code that backs up the [SQS node.js introduction](https://medium.com/@drwtech/a-node-js-introduction-to-amazon-simple-queue-service-sqs-9c0edf866eca) 3 | 4 | # Requirements 5 | You must have an AWS account, your Account ID noted and a valid AWS credentials file setup on your system. 6 | 7 | Details can be found in the referenced article. 8 | 9 | # Usage 10 | You must have node installed on your system or a node docker alias. If using docker you should make sure you map your local ~/.aws file so it's available in the correct path inside docker. 11 | 12 | To send a message: 13 | ``` 14 | node send.js 15 | ``` 16 | 17 | To receive a message: 18 | ``` 19 | node receive.js 20 | ``` 21 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sqs-node-intro", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "aws-sdk": { 8 | "version": "2.437.0", 9 | "resolved": "https://registry.npmjs.org/aws-sdk/-/aws-sdk-2.437.0.tgz", 10 | "integrity": "sha512-sDZb5QBOO6FOMvuKDEdO16YQRk0WUhnQd38EaSt0yUCi4Gev8uypODyYONgODZcXe8Cr1GMwC8scUKr00S/I5w==", 11 | "requires": { 12 | "buffer": "4.9.1", 13 | "events": "1.1.1", 14 | "ieee754": "1.1.8", 15 | "jmespath": "0.15.0", 16 | "querystring": "0.2.0", 17 | "sax": "1.2.1", 18 | "url": "0.10.3", 19 | "uuid": "3.3.2", 20 | "xml2js": "0.4.19" 21 | } 22 | }, 23 | "base64-js": { 24 | "version": "1.3.0", 25 | "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.3.0.tgz", 26 | "integrity": "sha512-ccav/yGvoa80BQDljCxsmmQ3Xvx60/UpBIij5QN21W3wBi/hhIC9OoO+KLpu9IJTS9j4DRVJ3aDDF9cMSoa2lw==" 27 | }, 28 | "buffer": { 29 | "version": "4.9.1", 30 | "resolved": "https://registry.npmjs.org/buffer/-/buffer-4.9.1.tgz", 31 | "integrity": "sha1-bRu2AbB6TvztlwlBMgkwJ8lbwpg=", 32 | "requires": { 33 | "base64-js": "1.3.0", 34 | "ieee754": "1.1.8", 35 | "isarray": "1.0.0" 36 | } 37 | }, 38 | "events": { 39 | "version": "1.1.1", 40 | "resolved": "https://registry.npmjs.org/events/-/events-1.1.1.tgz", 41 | "integrity": "sha1-nr23Y1rQmccNzEwqH1AEKI6L2SQ=" 42 | }, 43 | "ieee754": { 44 | "version": "1.1.8", 45 | "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.8.tgz", 46 | "integrity": "sha1-vjPUCsEO8ZJnAfbwii2G+/0a0+Q=" 47 | }, 48 | "isarray": { 49 | "version": "1.0.0", 50 | "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", 51 | "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" 52 | }, 53 | "jmespath": { 54 | "version": "0.15.0", 55 | "resolved": "https://registry.npmjs.org/jmespath/-/jmespath-0.15.0.tgz", 56 | "integrity": "sha1-o/Iiqarp+Wb10nx5ZRDigJF2Qhc=" 57 | }, 58 | "punycode": { 59 | "version": "1.3.2", 60 | "resolved": "https://registry.npmjs.org/punycode/-/punycode-1.3.2.tgz", 61 | "integrity": "sha1-llOgNvt8HuQjQvIyXM7v6jkmxI0=" 62 | }, 63 | "querystring": { 64 | "version": "0.2.0", 65 | "resolved": "https://registry.npmjs.org/querystring/-/querystring-0.2.0.tgz", 66 | "integrity": "sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=" 67 | }, 68 | "sax": { 69 | "version": "1.2.1", 70 | "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.1.tgz", 71 | "integrity": "sha1-e45lYZCyKOgaZq6nSEgNgozS03o=" 72 | }, 73 | "url": { 74 | "version": "0.10.3", 75 | "resolved": "https://registry.npmjs.org/url/-/url-0.10.3.tgz", 76 | "integrity": "sha1-Ah5NnHcF8hu/N9A861h2dAJ3TGQ=", 77 | "requires": { 78 | "punycode": "1.3.2", 79 | "querystring": "0.2.0" 80 | } 81 | }, 82 | "uuid": { 83 | "version": "3.3.2", 84 | "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz", 85 | "integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==" 86 | }, 87 | "xml2js": { 88 | "version": "0.4.19", 89 | "resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.19.tgz", 90 | "integrity": "sha512-esZnJZJOiJR9wWKMyuvSE1y6Dq5LCuJanqhxslH2bxM6duahNZ+HMpCLhBQGZkbX6xRf8x1Y2eJlgt2q3qo49Q==", 91 | "requires": { 92 | "sax": "1.2.1", 93 | "xmlbuilder": "9.0.7" 94 | } 95 | }, 96 | "xmlbuilder": { 97 | "version": "9.0.7", 98 | "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-9.0.7.tgz", 99 | "integrity": "sha1-Ey7mPS7FVlxVfiD0wi35rKaGsQ0=" 100 | } 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sqs-node-intro", 3 | "version": "1.0.0", 4 | "description": "Code that backs up the SQS node.js introduction", 5 | "main": "send.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/methodin/SQS-node-intro.git" 12 | }, 13 | "author": "", 14 | "license": "ISC", 15 | "bugs": { 16 | "url": "https://github.com/methodin/SQS-node-intro/issues" 17 | }, 18 | "homepage": "https://github.com/methodin/SQS-node-intro#readme", 19 | "dependencies": { 20 | "aws-sdk": "^2.437.0" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /receive.js: -------------------------------------------------------------------------------- 1 | // Load the AWS SDK for Node.js 2 | const AWS = require('aws-sdk'); 3 | // Set the region 4 | AWS.config.update({region: 'us-east-1'}); 5 | 6 | // Create SQS service object 7 | const sqs = new AWS.SQS({apiVersion: '2012-11-05'}); 8 | 9 | // Replace with your accountid and the queue name you setup 10 | const accountId = '1234567'; 11 | const queueName = 'test'; 12 | const queueUrl = `https://sqs.us-east-1.amazonaws.com/${accountId}/${queueName}`; 13 | 14 | // Setup the receiveMessage parameters 15 | const params = { 16 | QueueUrl: queueUrl, 17 | MaxNumberOfMessages: 1, 18 | VisibilityTimeout: 0, 19 | WaitTimeSeconds: 0 20 | }; 21 | 22 | sqs.receiveMessage(params, (err, data) => { 23 | if (err) { 24 | console.log(err, err.stack); 25 | } else { 26 | if (!data.Message) { 27 | console.log('Nothing to process'); 28 | return; 29 | } 30 | 31 | const orderData = JSON.parse(data.Messages[0].Body); 32 | console.log('Order received', orderData); 33 | 34 | // orderData is now an object that contains order_id and date properties 35 | // Lookup order data from data storage 36 | // Execute billing for order 37 | // Update data storage 38 | 39 | // Now we must delete the message so we don't handle it again 40 | const deleteParams = { 41 | QueueUrl: queueUrl, 42 | ReceiptHandle: data.Messages[0].ReceiptHandle 43 | }; 44 | sqs.deleteMessage(deleteParams, (err, data) => { 45 | if (err) { 46 | console.log(err, err.stack); 47 | } else { 48 | console.log('Successfully deleted message from queue'); 49 | } 50 | }); 51 | } 52 | }); 53 | -------------------------------------------------------------------------------- /send.js: -------------------------------------------------------------------------------- 1 | // Load the AWS SDK for Node.js 2 | const AWS = require('aws-sdk'); 3 | // Set the region we will be using 4 | AWS.config.update({region: 'us-east-1'}); 5 | 6 | // Create SQS service client 7 | const sqs = new AWS.SQS({apiVersion: '2012-11-05'}); 8 | 9 | // Replace with your accountid and the queue name you setup 10 | const accountId = '1234567'; 11 | const queueName = 'test'; 12 | 13 | // Setup the sendMessage parameter object 14 | const params = { 15 | MessageBody: JSON.stringify({ 16 | order_id: 1234, 17 | date: (new Date()).toISOString() 18 | }), 19 | QueueUrl: `https://sqs.us-east-1.amazonaws.com/${accountId}/${queueName}` 20 | }; 21 | 22 | sqs.sendMessage(params, (err, data) => { 23 | if (err) { 24 | console.log("Error", err); 25 | } else { 26 | console.log("Successfully added message", data.MessageId); 27 | } 28 | }); 29 | --------------------------------------------------------------------------------