paint-brush
Fast setup Helloworld in AWS SQS(Message Queuing Service)by@peterchang_82818
9,003 reads
9,003 reads

Fast setup Helloworld in AWS SQS(Message Queuing Service)

by Peter Chang2mJanuary 17th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Setting up your own server will require good knowledge of the subject so that you do not leave any corner untouched. This is not the case with Amazon SQS as it is pretty quick to get started with.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Fast setup Helloworld in AWS SQS(Message Queuing Service)
Peter Chang HackerNoon profile picture

Setting up your own server will require good knowledge of the subject so that you do not leave any corner untouched. This is not the case with Amazon SQS as it is pretty quick to get started with.

The beauty of SQS is also that things do not get lost–independent of any server–and also it eliminates two threads trying to do the same job and bumping heads.

Once anything receives the message in the queue, they have an exclusive lease on it for a certain period of time. This makes it especially great once you need to scale to multiple worker servers. Each one just gets the next job in the queue and does its thing, independent of any of the others, and then removes it from the queue once done(from Jason Byrne).

Remark:

— Before starting steps below, Clone Sample Git, and do npm install.

Step:

  1. set up access-key and secret-key
  2. $ node sqs_createqueue.js
  3. $ node sqs_listqueues.js
  4. $ node sqs_getqueueurl.js (the reture url is used in step 5 and 6)
  5. $ node sendMessage.js
  6. $ node receiveMessage.js

1- set up access-key and secret-key

Go to IAM management console to create an user whom the system is going to generate access-key and secret-key to (for more information).

Before creating the user, a configured group with SQS permission is required. In the permission windows, select the click-box for AmazonSQSFullAccess, and next.

Add user to the group with access SQS permission.

Generating access-key and secret-key, which will be used in config.json later.

Create a config.json file under the Node.js directory, in which paste the access-key and secret-key which generated from the previous step to field accessKeyId and secretAccessKey.

2- Creating Queue

Amazon not only brings us diverse Another great things Amazon provides us are, very easy to read documentation and super easy to use sample code. With the doc and git example, running the Helloworld example, sending/receiving message is just a piece of cake.

Creating a Queue

run sqs_createqueue.js file

$ node sqs_createqueue.js

Success https://sqs.us-east-1.amazonaws.com/xxx/SQS_QUEUE_NAME

List Queue

$ node sqs_listqueues.js

Success [ 'https://sqs.us-east-1.amazonaws.com/xxx/SQS_QUEUE_NAME' ]

Get Queue Url

$ node sqs_getqueueurl.js

Success https://sqs.us-east-1.amazonaws.com/xxx/SQS_QUEUE_NAME

In sendMessage.js, set value to valuable QueueUrl from the return result, and the message “Information about current NY Times…” is going to be sent:

Sending Message to Queue

$ node sendMessage.js

Success 169607bb-xxx-xxx-xxx-d4d1cc270e9f

Receiving Message from Queue

In receiveMessage.js, Set value to QueueUrl from the sqs_getqueueurl.js result:

$ node receiveMessage.js

{ ResponseMetadata: { RequestId: 'f7ae9017-0692-5eae-874e-c0e0a13a070b' },

Messages:






[ { MessageId: '169607bb-3d5a-4dab-a8bf-d4d1cc270e9f',ReceiptHandle: 'xxx',MD5OfBody: 'xxx',Body: 'Information about current NY Times fiction bestseller for week of 12/11/2016.', Attributes: [Object],MD5OfMessageAttributes: 'xxxx',MessageAttributes: [Object] } ] }

Note

Message Attribute VS Message Body

Message attributes are supposed to be used as message metadata (like timestamp or possibly some category) and not the message itself.

Reference:

https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javascript/example_code/sqs

http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/sqs-examples-using-queues.html

https://github.com/wahengchang/aws-sqs-example