{"id":31,"date":"2017-08-07T11:38:14","date_gmt":"2017-08-07T08:38:14","guid":{"rendered":"http:\/\/lyubomir-angelov.com\/?p=31"},"modified":"2017-08-07T11:38:14","modified_gmt":"2017-08-07T08:38:14","slug":"integrate-amazon-aws-sqs","status":"publish","type":"post","link":"https:\/\/lyubomir-angelov.com\/blog\/integrate-amazon-aws-sqs\/","title":{"rendered":"How to integrate Amazon AWS SQS"},"content":{"rendered":"<p>SQS stands for Simple Queue Service, is a highly scalable and reliable distributed queueing system, which can be used to make applications asynchronous.<\/p>\n<p>I will show you the simplest way of integrating the SQS, installing AWS SDK, Creating a Queue and a Worker to manage the queues and push tasks to Queues.<br \/>\n<!--more--><\/p>\n<h3>Install and use PHP AWS SDK<\/h3>\n<p>Please read my previous article about that here <a href=\"https:\/\/lyubomir-angelov.com\/blog\/how-to-use-amazon-php-aws-sdk\/\">How to use Amazon PHP AWS SDK<\/a><\/p>\n<h3>Creating Worker and Queue<\/h3>\n<p>To create a queue and worker is very easy, you need the SDK instance and SQS instance<\/p>\n<p>This script has to be run from the command line in order to start multiple workers to handle as much as you need tasks.<\/p>\n<pre class=\"language-php\"><code>\n\/\/ worker.php\n\/\/ $this-&gt;config is array with Amazon settings that are mentioned in the previous article about installing SDK\n        $sdk = new Sdk([\n            &#039;region&#039;   =&gt; $this-&gt;config[&#039;amazon&#039;][&#039;sqs&#039;][&#039;region&#039;],\n            &#039;version&#039;  =&gt; &#039;latest&#039;,\n        ]);\n\/\/ Create the SQS instance with the right credentials\n        $sqs = $sdk-&gt;createSqs([\n            &#039;region&#039;   =&gt; $this-&gt;config[&#039;amazon&#039;][&#039;sqs&#039;][&#039;region&#039;],\n            &#039;version&#039;  =&gt; &#039;latest&#039;,\n            &#039;credentials&#039; =&gt; [\n                &#039;key&#039;    =&gt; $this-&gt;config[&#039;amazon&#039;][&#039;sqs&#039;][&#039;access_key&#039;],\n                &#039;secret&#039; =&gt; $this-&gt;config[&#039;amazon&#039;][&#039;sqs&#039;][&#039;secret_access_key&#039;],\n            ],\n        ]);\n\/\/ You need to specify a Queue name, string, you can have multiple queues, and this depends on how much tasks you need to handle, in the most cases, 1 will do the perfect job\n        $queueName = $this-&gt;config[&#039;amazon&#039;][&#039;sqs&#039;][&#039;workers&#039;][&#039;queue&#039;];\n\/\/ I&#039;m using Symfony 2 console, that&#039;s why I have $output object\n        $output-&gt;writeln(&#039;[*] Waiting for SQS messages. - &#039; . date(self::DATEFORMAT));\n\/\/ We need to make a while loop in order to stay awake and wait for messeges\/tasks from the Queue\n        while (true) {\n            try {\n\/\/ Create the queue, this is done the first time, after that it just doesn&#039;t return anything, also you can create the Queue from the Amazon SQS profile\n                $sqs-&gt;createQueue([&#039;QueueName&#039; =&gt; $queueName]);\n\n                \/\/ Get the queue URL from the queue name.\n                $result = $sqs-&gt;getQueueUrl([&#039;QueueName&#039; =&gt; $queueName]);\n                $queueUrl = $result-&gt;get(&#039;QueueUrl&#039;);\n                \/\/ Receive a message from the queue\n                $result = $sqs-&gt;receiveMessage([\n                    &#039;QueueUrl&#039;        =&gt; $queueUrl,\n                    &#039;WaitTimeSeconds&#039; =&gt; 1 \/\/ Enable long polling\n                ]);\n\n                if ($result[&#039;Messages&#039;] == null) {\n                    \/\/ No message to process so continue to looooooooooooooooooooop\n                    continue;\n                }\n\n                \/\/ Get the message information\n                $message = array_pop($result[&#039;Messages&#039;]);\n                $queueHandle = $message[&#039;ReceiptHandle&#039;];\n                $messageBody = $message[&#039;Body&#039;];\n\n                $output-&gt;writeln(&quot;[x] Received &quot;. $messageBody . &#039; - &#039; . date(self::DATEFORMAT));\n                \/\/ Do what you want to do with the task, here I&#039;m processing user uploaded videos with FFMPEG making some GIFs and additional stuff\n                \/\/ My $messageBody contains video SLUG, according to it I can get the right video from the database and do the processing on it\n                $this-&gt;videoService-&gt;process($messageBody);\n\n                \/\/ Delete the message, it is processed\n                $sqs-&gt;deleteMessage(array(\n                    &#039;QueueUrl&#039; =&gt; $queueUrl,\n                    &#039;ReceiptHandle&#039; =&gt; $queueHandle\n                ));\n\n                $output-&gt;writeln(&#039;[x] Done - &#039; . date(self::DATEFORMAT));\n            } catch (Exception $e) {\n\/\/ Always log the errors, this scripts runs in commandline and if something happens, you will need the info\n                $output-&gt;writeln(&#039;[*] Error receiving message to SQS queue - &#039; . date(self::DATEFORMAT) . $e-&gt;getMessage());\n            }\n        }\n<\/code><\/pre>\n<h3>Adding tasks to the Queue<\/h3>\n<pre class=\"language-php\"><code>\n\/\/ sqsPushToQueue.php\n         try {\n            $sdk = new Sdk([\n                &#039;region&#039;   =&gt; $this-&gt;config[&#039;amazon&#039;][&#039;sqs&#039;][&#039;region&#039;],\n                &#039;version&#039;  =&gt; &#039;latest&#039;,\n            ]);\n            $sqs = $sdk-&gt;createSqs([\n                &#039;region&#039;   =&gt; $this-&gt;config[&#039;amazon&#039;][&#039;sqs&#039;][&#039;region&#039;],\n                &#039;version&#039;  =&gt; &#039;latest&#039;,\n                &#039;credentials&#039; =&gt; [\n                    &#039;key&#039;    =&gt; $this-&gt;config[&#039;amazon&#039;][&#039;sqs&#039;][&#039;access_key&#039;],\n                    &#039;secret&#039; =&gt; $this-&gt;config[&#039;amazon&#039;][&#039;sqs&#039;][&#039;secret_access_key&#039;],\n                ],\n            ]);\n            $queueName = $this-&gt;config[&#039;amazon&#039;][&#039;sqs&#039;][&#039;workers&#039;][&#039;queue&#039;];\n\n            \/\/ Get the queue URL from the queue name.\n            $result = $sqs-&gt;getQueueUrl([&#039;QueueName&#039; =&gt; $queueName]);\n            $queueUrl = $result-&gt;get(&#039;QueueUrl&#039;);\n\n            \/\/ Send the message\n            $sqs-&gt;sendMessage(array(\n                &#039;QueueUrl&#039; =&gt; $queueUrl,\n                &#039;MessageBody&#039; =&gt; $slug \/\/ As already mentioned above, I&#039;m sending the slug that I will need to get the right video to process\n            ));\n\n            $this-&gt;logger-&gt;debug(&#039;SQS: data sent! &#039; . $slug);\n        } catch (Exception $e) {\n            $this-&gt;logger-&gt;err(&#039;SQS: Error sending message to queue &#039; . $e-&gt;getMessage());\n        }\n<\/code><\/pre>\n<p>That&#8217;s it, if you have any comments, please fill them below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQS stands for Simple Queue Service, is a highly scalable and reliable distributed queueing system, which can be used to make applications asynchronous. I will show you the simplest way of integrating the SQS, installing AWS SDK, Creating a Queue and a Worker to manage the queues and push tasks to Queues.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[2,9,3],"class_list":["post-31","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-amazon","tag-aws-sqs","tag-php-aws-sdk"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to integrate Amazon AWS SQS - Lyubomir Angelov<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/lyubomir-angelov.com\/blog\/integrate-amazon-aws-sqs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to integrate Amazon AWS SQS - Lyubomir Angelov\" \/>\n<meta property=\"og:description\" content=\"SQS stands for Simple Queue Service, is a highly scalable and reliable distributed queueing system, which can be used to make applications asynchronous. I will show you the simplest way of integrating the SQS, installing AWS SDK, Creating a Queue and a Worker to manage the queues and push tasks to Queues.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/lyubomir-angelov.com\/blog\/integrate-amazon-aws-sqs\/\" \/>\n<meta property=\"og:site_name\" content=\"Lyubomir Angelov\" \/>\n<meta property=\"article:published_time\" content=\"2017-08-07T08:38:14+00:00\" \/>\n<meta name=\"author\" content=\"Lyubomir\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@angelovcode\" \/>\n<meta name=\"twitter:site\" content=\"@angelovcode\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Lyubomir\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/integrate-amazon-aws-sqs\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/integrate-amazon-aws-sqs\/\"},\"author\":{\"name\":\"Lyubomir\",\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/#\/schema\/person\/5e091f5c2fa79f57d30d482a7ef8cd92\"},\"headline\":\"How to integrate Amazon AWS SQS\",\"datePublished\":\"2017-08-07T08:38:14+00:00\",\"dateModified\":\"2017-08-07T08:38:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/integrate-amazon-aws-sqs\/\"},\"wordCount\":141,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/#\/schema\/person\/5e091f5c2fa79f57d30d482a7ef8cd92\"},\"keywords\":[\"amazon\",\"aws-sqs\",\"php-aws-sdk\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/lyubomir-angelov.com\/blog\/integrate-amazon-aws-sqs\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/integrate-amazon-aws-sqs\/\",\"url\":\"https:\/\/lyubomir-angelov.com\/blog\/integrate-amazon-aws-sqs\/\",\"name\":\"How to integrate Amazon AWS SQS - Lyubomir Angelov\",\"isPartOf\":{\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/#website\"},\"datePublished\":\"2017-08-07T08:38:14+00:00\",\"dateModified\":\"2017-08-07T08:38:14+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/integrate-amazon-aws-sqs\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/lyubomir-angelov.com\/blog\/integrate-amazon-aws-sqs\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/integrate-amazon-aws-sqs\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/lyubomir-angelov.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to integrate Amazon AWS SQS\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/#website\",\"url\":\"https:\/\/lyubomir-angelov.com\/blog\/\",\"name\":\"Lyubomir Angelov\",\"description\":\"The life of a Manager\",\"publisher\":{\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/#\/schema\/person\/5e091f5c2fa79f57d30d482a7ef8cd92\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/lyubomir-angelov.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/#\/schema\/person\/5e091f5c2fa79f57d30d482a7ef8cd92\",\"name\":\"Lyubomir\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f6b8c15868355fa857fad18647aeadd07814e3d9baa72a1c92e718056684fec7?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/f6b8c15868355fa857fad18647aeadd07814e3d9baa72a1c92e718056684fec7?s=96&d=mm&r=g\",\"caption\":\"Lyubomir\"},\"logo\":{\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:\/\/twitter.com\/angelovcode\"],\"url\":\"https:\/\/lyubomir-angelov.com\/blog\/author\/superfly\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to integrate Amazon AWS SQS - Lyubomir Angelov","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/lyubomir-angelov.com\/blog\/integrate-amazon-aws-sqs\/","og_locale":"en_US","og_type":"article","og_title":"How to integrate Amazon AWS SQS - Lyubomir Angelov","og_description":"SQS stands for Simple Queue Service, is a highly scalable and reliable distributed queueing system, which can be used to make applications asynchronous. I will show you the simplest way of integrating the SQS, installing AWS SDK, Creating a Queue and a Worker to manage the queues and push tasks to Queues.","og_url":"https:\/\/lyubomir-angelov.com\/blog\/integrate-amazon-aws-sqs\/","og_site_name":"Lyubomir Angelov","article_published_time":"2017-08-07T08:38:14+00:00","author":"Lyubomir","twitter_card":"summary_large_image","twitter_creator":"@angelovcode","twitter_site":"@angelovcode","twitter_misc":{"Written by":"Lyubomir","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/lyubomir-angelov.com\/blog\/integrate-amazon-aws-sqs\/#article","isPartOf":{"@id":"https:\/\/lyubomir-angelov.com\/blog\/integrate-amazon-aws-sqs\/"},"author":{"name":"Lyubomir","@id":"https:\/\/lyubomir-angelov.com\/blog\/#\/schema\/person\/5e091f5c2fa79f57d30d482a7ef8cd92"},"headline":"How to integrate Amazon AWS SQS","datePublished":"2017-08-07T08:38:14+00:00","dateModified":"2017-08-07T08:38:14+00:00","mainEntityOfPage":{"@id":"https:\/\/lyubomir-angelov.com\/blog\/integrate-amazon-aws-sqs\/"},"wordCount":141,"commentCount":0,"publisher":{"@id":"https:\/\/lyubomir-angelov.com\/blog\/#\/schema\/person\/5e091f5c2fa79f57d30d482a7ef8cd92"},"keywords":["amazon","aws-sqs","php-aws-sdk"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/lyubomir-angelov.com\/blog\/integrate-amazon-aws-sqs\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/lyubomir-angelov.com\/blog\/integrate-amazon-aws-sqs\/","url":"https:\/\/lyubomir-angelov.com\/blog\/integrate-amazon-aws-sqs\/","name":"How to integrate Amazon AWS SQS - Lyubomir Angelov","isPartOf":{"@id":"https:\/\/lyubomir-angelov.com\/blog\/#website"},"datePublished":"2017-08-07T08:38:14+00:00","dateModified":"2017-08-07T08:38:14+00:00","breadcrumb":{"@id":"https:\/\/lyubomir-angelov.com\/blog\/integrate-amazon-aws-sqs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/lyubomir-angelov.com\/blog\/integrate-amazon-aws-sqs\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/lyubomir-angelov.com\/blog\/integrate-amazon-aws-sqs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/lyubomir-angelov.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to integrate Amazon AWS SQS"}]},{"@type":"WebSite","@id":"https:\/\/lyubomir-angelov.com\/blog\/#website","url":"https:\/\/lyubomir-angelov.com\/blog\/","name":"Lyubomir Angelov","description":"The life of a Manager","publisher":{"@id":"https:\/\/lyubomir-angelov.com\/blog\/#\/schema\/person\/5e091f5c2fa79f57d30d482a7ef8cd92"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/lyubomir-angelov.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/lyubomir-angelov.com\/blog\/#\/schema\/person\/5e091f5c2fa79f57d30d482a7ef8cd92","name":"Lyubomir","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/lyubomir-angelov.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/f6b8c15868355fa857fad18647aeadd07814e3d9baa72a1c92e718056684fec7?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f6b8c15868355fa857fad18647aeadd07814e3d9baa72a1c92e718056684fec7?s=96&d=mm&r=g","caption":"Lyubomir"},"logo":{"@id":"https:\/\/lyubomir-angelov.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/twitter.com\/angelovcode"],"url":"https:\/\/lyubomir-angelov.com\/blog\/author\/superfly\/"}]}},"_links":{"self":[{"href":"https:\/\/lyubomir-angelov.com\/blog\/wp-json\/wp\/v2\/posts\/31","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lyubomir-angelov.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lyubomir-angelov.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lyubomir-angelov.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lyubomir-angelov.com\/blog\/wp-json\/wp\/v2\/comments?post=31"}],"version-history":[{"count":4,"href":"https:\/\/lyubomir-angelov.com\/blog\/wp-json\/wp\/v2\/posts\/31\/revisions"}],"predecessor-version":[{"id":35,"href":"https:\/\/lyubomir-angelov.com\/blog\/wp-json\/wp\/v2\/posts\/31\/revisions\/35"}],"wp:attachment":[{"href":"https:\/\/lyubomir-angelov.com\/blog\/wp-json\/wp\/v2\/media?parent=31"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyubomir-angelov.com\/blog\/wp-json\/wp\/v2\/categories?post=31"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyubomir-angelov.com\/blog\/wp-json\/wp\/v2\/tags?post=31"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}