Skip to main content
AWS SQS

SQS Messaging Service in AWS

AWS SQS (Simple Queue Service), as the name indicates is a fully managed messaging FIFO (First In First Out) queue service that receives and sends messages from any software system. But it is generally used for distributed computing. AWS SQS is secure, durable, scalable and a reliable service.  AWS provides SDK’s in various languages to access SQS services.

In this blog I will use PHP AWS SDK to send, receive and delete messages from SQS.

Given below are the steps to be followed:

Please log into: https://docs.aws.amazon.com/aws-sdk-php/v3/guide/getting-started/installation.html

and install/configure AWS SDK on to a local system.  I am assuming that there is already an available AWS account.

AWS accepts only https requests, so  install dummy ssl certifcate in a local WAMP/XAMPP.

AWS SQS

After installation of  AWS SDK and SSL,  SQS needs to be configured.

  1. Go to the AWS console, choose `preferred availability zone’ and `simple queue service’.  Click on `create new queue’.
  2. Enter the name of the queue, for example, php-demo-queue
  3. Set Default Visibility Timeout to 5 minutes. This option is to make the messages invisible for 5 minutes once it goes for processing from the queue. The maximum time is 12 hours.
  4. Set Message Retention Period to 14 days. This option is to make the messages available in the queue for maximum of two weeks if we do not delete the message.
  5. Set Maximum Message Size to 256 kb. The message should not exceed 256 kb.
  6. Set Delivery Delay to 0. This will tell the queue to show the message as soon as it comes to the queue. If one does not  want the message to be visible instantly, then give Delivery Delay up to 15 minutes.
  7. Set Receive Message Wait Time to 0 and click on Create Queue.

Note 1: Once the queue is created, it is possible to access edit/attribute options, rather than edit them via API Calls.

Note 2:  To create a FIFO Queue, the queue name has to be prefixed with .fifo For Ex. php-demo-queue.fifo

Transform your applications to the cloud

After the Queue is created,  now is the time to add/edit permissions.

1) Select the queue name to add permissions.

2) Click Permissions tab and click on Add a Permission button.

3) Select Effect to Allow, Principal to Everybody and Actions to All SQS Actions and Click on Save Changes

List of Methods available in AWS SQS

  1. changeMessageVisibility()
  2. changeMessageVisibilityBatch()
  3. createQueue()
  4. deleteMessage()
  5. deleteMessageBatch()
  6. deleteQueue()
  7. getQueueAttributes()
  8. getQueueUrl()
  9. listDeadLetterSourceQueues()
  10. listQueues()
  11. purgeQueue()
  12. receiveMessage()
  13. removePermission()
  14. sendMessage()
  15. sendMessageBatch()
  16. setQueueAttributes()

Example 1:- Get url of the queue, get queue attributes, send messages, receive message and delete the message from queue. This example is about a Film ticket booking system. The user has provided his information and seating capacity. The main server receives user information, stores info, payment done. Now to generate a QR Code, this is sent to another dedicated server where only QR Codes are generated and messaged to user and updates DB.

$config = [
     'region' => 'ap-south-1',
     'version' => 'latest',
     'credentials' => [
         'key' => AWS_ACCESS_KEY_ID,
         'secret' => AWS_SECRET_ACCESS_KEY,
     ]
];

Try:

{

     $sqsClient = new \Aws\Sqs\SqsClient($config);

     $stdUrl = $sqsClient->getQueueUrl(array('QueueName' => "test-std-queue"));
     $queueUrl = $stdUrl->get('QueueUrl');


     $queueAttributes = $sqsClient->getQueueAttributes(['QueueUrl' => $queueUrl, 'AttributeNames' => ['All']]);
     $attributes = $queueAttributes->get('Attributes');

     $message = [
         'id' => uniqid(),
         'cust_name' => 'Demo User',
         'cust_email' => 'testemail@test.com',
         'cust_phone' => '987654321',
         'cust_seating' => ['A1','A2','A3'],
         'theatre_id' => 500,
         'amount_paid' => 1000,
         'discount' => 100
     ];
     $messageResult = $sqsClient->sendMessage(['QueueUrl' => $queueUrl, 'MessageBody' => json_encode($message)]);

     $receiveMessages = $sqsClient->receiveMessage(['QueueUrl' => $queueUrl, 'AttributeNames' => ['All']]);
     $msg = $receiveMessages->get('Messages');
     $receiptHandle = $msg[0]['ReceiptHandle'];

     $sqsClient->deleteMessage(array(
         'QueueUrl' => $queueUrl,
         'ReceiptHandle' => $receiptHandle,
     ));
} catch (\Aws\Exception\AwsException $ex) {
     echo $ex->getMessage();
} catch (Exception $ex) {
     echo $ex->getMessage();
}

Note 1: All methods will return appropriate messages and status codes.

One can store the responses for future investigations.

Note 2: The body of the message can be in any format, for example,  JSON, XML, Text, Paths to files or images which should not exceed 256 kb.

Dead Letter Queue: If a message is received for X number of times, then it is considered as a Dead Letter Queue. Supposing  a message is received by the server over 50 times and still not processed successfully, then it is considered as Dead Letter Queue and sent to Dead Letter Queue.

To configure Dead Letter Queues, we need to create a queue as mentioned in the above steps. For example test-std-queue-dlq.

Then add the queue to Dead Letter Queue Settings for test-std-queue.

 

 

 

Chand Basha

Chand Basha

Chand Basha works as PHP Developer with Trigent Software. He has nearly 6 years of experience in PHP/MVC technology. He also has strong experience in AWS Services,ZF1,ZF2 Framework, MySql,MongoDb,AngularJs/JQuery/Java script etc.

Leave a Reply

Your email address will not be published. Required fields are marked *

thirty three + = 36