<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Avelino Puello</title>
    <description>The latest articles on DEV Community by Avelino Puello (@apuello).</description>
    <link>https://dev.to/apuello</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F305992%2Fb40601c4-199b-4376-b5de-ad857fe0dfa0.jpeg</url>
      <title>DEV Community: Avelino Puello</title>
      <link>https://dev.to/apuello</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/apuello"/>
    <language>en</language>
    <item>
      <title>AWS SNS, SQS and Laravel</title>
      <dc:creator>Avelino Puello</dc:creator>
      <pubDate>Wed, 08 Jan 2020 22:22:51 +0000</pubDate>
      <link>https://dev.to/apuello/aws-sns-sqs-and-laravel-2jn5</link>
      <guid>https://dev.to/apuello/aws-sns-sqs-and-laravel-2jn5</guid>
      <description>&lt;p&gt;Publish-Subscribe pattern(pub/sub) is a communication mechanism frequently used by micro-service architecture. AWS &lt;a href="https://aws.amazon.com/sns/"&gt;&lt;strong&gt;Simple Notification Service&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;(SNS)&lt;/strong&gt; and &lt;a href="https://aws.amazon.com/sqs/"&gt;&lt;strong&gt;Simple Queue Service&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;(SQS)&lt;/strong&gt; are great services  that help businesses to build scalable and fault tolerant systems bringing the flexibility and reliability of enterprise message-oriented applications*&lt;em&gt;.&lt;/em&gt;*&lt;/p&gt;

&lt;p&gt;Let’s build a micro-service architecture that implements SQS, SNS and Laravel, We are going to need a name for this new awesome architacture, let’s call it &lt;strong&gt;Service A,&lt;/strong&gt; let’s also create our AWS services needed, we are going to need a SNS topic, let’s name it &lt;strong&gt;sns_service_a_message&lt;/strong&gt; and a  SQS queue &lt;strong&gt;sqs_service_a.&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Create a SNS topic:
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yRHti1Dh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/1clyckujqlmkzwdwdehb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yRHti1Dh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/1clyckujqlmkzwdwdehb.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Create a new SQS and subscribe to the SNS topic previously created.
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EapbbEJy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/qovrzi6udoy9ehfrefdh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EapbbEJy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/qovrzi6udoy9ehfrefdh.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--B1tJ8nfT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/cn43ad52scgmbywxlkws.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--B1tJ8nfT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/cn43ad52scgmbywxlkws.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--f1MVNNN8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/is4p8b6bvfvzbpr9mdx7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f1MVNNN8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/is4p8b6bvfvzbpr9mdx7.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Create a IAM user with &lt;strong&gt;AmazonSQSFullAccess&lt;/strong&gt; permission.
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_vP-A6Jm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/y7h3fowveqxml9w85f4b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_vP-A6Jm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/y7h3fowveqxml9w85f4b.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Also make sure that you have your AWS credential configured, p&lt;a href="https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials_profiles.html"&gt;lease read this page&lt;/a&gt; if you need help doing so.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Laravel
&lt;/h1&gt;

&lt;p&gt;Now that we have our Amazon Web Services SQS, SNS configured and our IAM user created, let’s move on on how to subscribe and consume messages with Laravel.&lt;/p&gt;

&lt;p&gt;We are gonna make use of the &lt;a href="https://github.com/aws/aws-sdk-php"&gt;&lt;strong&gt;aws/aws-sdk-php&lt;/strong&gt;&lt;/a&gt; laravel package as it makes it easy for developers to access &lt;a href="http://aws.amazon.com/"&gt;Amazon Web Services&lt;/a&gt; from within PHP code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/aws/aws-sdk-php"&gt;aws/aws-sdk-php&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let’s see Laravel consuming SQS messages, for simplicity we are going to create a helper class that is going to configure the &lt;em&gt;queueURL&lt;/em&gt; and it is going to create an &lt;em&gt;SQSClient&lt;/em&gt; on its construct method and will &lt;em&gt;receiveMessages&lt;/em&gt; when its &lt;em&gt;handle&lt;/em&gt; function is called.&lt;/p&gt;

&lt;p&gt;And then we are just going to call this class from the API, in your &lt;a href="https://github.com/apuello/sqs-laravel/tree/master/routes"&gt;&lt;strong&gt;&lt;em&gt;routes&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;&lt;em&gt;/api.php&lt;/em&gt;&lt;/strong&gt; add the code snippet below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Route::get('sqs', function(){     
     (new ProcessMessageHelper())-&amp;gt;handle();  
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When a message is sent to t our SQS and we hit the URL(&lt;strong&gt;&lt;em&gt;&lt;a href="http://127.0.0.1:8000/api/sqs"&gt;http://127.0.0.1:8000/api/sqs&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;) we should see a payload like the below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array(5) {  
  \["MessageId"\]=&amp;gt;  
  string(36) "d26c79b2-b735-4d0d-b49b-3c26138de161"  
  \["ReceiptHandle"\]=&amp;gt;  
  string(412) "AQEBtts0V271WPq7l9Ut4CKNSDIlX9ljNsrl6JylEZkFk3ar8X9U8u5UtnXnTq4FH4IJvyMtdrKCLv4q+L2mkrg7SuEp9hBewy5umR7L5sbV2QzNMM2gZ8fOKi37i0VRVXiq9lX6JB/K4ssn4cx2Y51UECmo7c/yoTYUDkw0tqwnSic9KsYFXBtsydCZ3WmIVANkjYN53S9fIw1tXZLTEI2zjdioaSP6hkOfY+b5oHhFXOAU7DVa00JQorZ040sJ7aC9JywqGALhx/ORBRiHfA8G5Auk2SL77Cx+EpZvmZsY1T7UI1N5UPlCuIvAg6LYh7cNqAFILKYRRKvSGryMGxhDTcXYhWrSPXzoUMK2nNeGjLkGls2f+T5ag1EWHzaSiShTdZHQm1k9EDJRrywf4U/lqg=="  
  \["MD5OfBody"\]=&amp;gt;  
  string(32) "b2f79cdd35aaa0534584d0f29fbcff1e"  
  \["Body"\]=&amp;gt;  
  string(14) "hello there!!!"  
  \["Attributes"\]=&amp;gt;  
  array(1) {  
    \["SentTimestamp"\]=&amp;gt;  
    string(13) "1577917862259"  
  }  
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you would like to take this implementation a little further you may want to create a worker that checks for SQS messages in the background instead of relying on a URL.&lt;/p&gt;

&lt;p&gt;For a complete working repo please feel free to take a look at and play around with the repo -&amp;gt; &lt;a href="https://github.com/apuello/sqs-laravel"&gt;https://github.com/apuello/sqs-laravel&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Don't forget to follow me:&lt;br&gt;
Twitter: @monkeyswipes&lt;br&gt;
Medium:  @monkeyswipes&lt;/p&gt;

</description>
      <category>aws</category>
      <category>php</category>
      <category>laravel</category>
      <category>microservices</category>
    </item>
  </channel>
</rss>
