<?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: ahmadtibi</title>
    <description>The latest articles on DEV Community by ahmadtibi (@ahmadtibi).</description>
    <link>https://dev.to/ahmadtibi</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%2F537632%2Fcc638099-8245-4098-97b6-a96cef345e5d.png</url>
      <title>DEV Community: ahmadtibi</title>
      <link>https://dev.to/ahmadtibi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ahmadtibi"/>
    <language>en</language>
    <item>
      <title>Queueing up Laravel Fortify email verification for better performance</title>
      <dc:creator>ahmadtibi</dc:creator>
      <pubDate>Sat, 23 Oct 2021 13:41:38 +0000</pubDate>
      <link>https://dev.to/ahmadtibi/queueing-up-laravel-fortify-email-verification-for-better-performance-6k6</link>
      <guid>https://dev.to/ahmadtibi/queueing-up-laravel-fortify-email-verification-for-better-performance-6k6</guid>
      <description>&lt;p&gt;After a long break from laravel (last time I used it was around version 5.7), I recently got back into because of more free time. &lt;/p&gt;

&lt;p&gt;I noticed there was a relatively new package called Fortify which allows you to scaffold the authentication process, it seemed pretty useful; however after diving deep into it, I realized that after registering a new user and sending an email verification there would be blocking request for a few seconds so it was obvious to me that the email verification was in fact not being queued.&lt;/p&gt;

&lt;h1&gt;
  
  
  Queuing up the email verification
&lt;/h1&gt;

&lt;p&gt;I'm gonna start with a clean laravel 8 project and add fortify to it&lt;/p&gt;

&lt;h4&gt;
  
  
  Installing fortify
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;composer require laravel/fortify&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Overriding the email verification notification and making it a job that we can queue
&lt;/h4&gt;

&lt;p&gt;If we go into the user model there's a method called &lt;code&gt;sendEmailVerificationNotification()&lt;/code&gt; which we can actually override to modify how fortify sends the email verification notification.&lt;/p&gt;

&lt;p&gt;In order to override we first have to make a new notification that will implement the &lt;code&gt;ShouldQueue&lt;/code&gt; Interface, and extend the &lt;code&gt;VerifyEmail&lt;/code&gt; notification that is provided by Fortify.&lt;/p&gt;

&lt;h4&gt;
  
  
  Creating the notification and modifying it to our needs
&lt;/h4&gt;

&lt;p&gt;To create a new notification we can just use laravel's built in php artisan helper.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;php artisan make:notification VerifyEmailQueued&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After creating the notification we should make it extend &lt;code&gt;VerifyEmail&lt;/code&gt; and implement the &lt;code&gt;ShouldQueue&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The notification class should look like this:&lt;br&gt;
&lt;code&gt;VerifyEmailQueued.php&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

namespace App\Notifications;

use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class VerifyEmailQueued extends VerifyEmail implements ShouldQueue
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }


    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Overriding the &lt;code&gt;sendEmailVerificationNotification()&lt;/code&gt; in the User model
&lt;/h4&gt;

&lt;p&gt;Now there's one more thing left to do and it's overriding the &lt;code&gt;sendEmailVerificationNotification()&lt;/code&gt; method in the User model&lt;/p&gt;

&lt;p&gt;Go to &lt;code&gt;User.php&lt;/code&gt; and add the following method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function sendEmailVerificationNotification()
    {
        $this-&amp;gt;notify(new VerifyEmailQueued);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now there's one more thing left to do and it's running the artisan command to listen for jobs and queue them up.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;php artisan queue:work&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If everything is working correctly you should see that the VerifyEmailQueued notification should be processed and an email should be sent to the User. It will look something like this in the terminal:&lt;br&gt;
&lt;a href="https://ibb.co/vPhbMz0"&gt;Terminal image&lt;/a&gt;&lt;/p&gt;

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