<?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: Reda-Hammada</title>
    <description>The latest articles on DEV Community by Reda-Hammada (@reda-hammada).</description>
    <link>https://dev.to/reda-hammada</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%2F652323%2Ff8eb5507-abbd-487e-94b5-9e11a8d3986f.jpeg</url>
      <title>DEV Community: Reda-Hammada</title>
      <link>https://dev.to/reda-hammada</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/reda-hammada"/>
    <language>en</language>
    <item>
      <title>Event bus in Vue 3</title>
      <dc:creator>Reda-Hammada</dc:creator>
      <pubDate>Sun, 30 Apr 2023 11:49:08 +0000</pubDate>
      <link>https://dev.to/reda-hammada/event-bus-in-vue-3-4ai5</link>
      <guid>https://dev.to/reda-hammada/event-bus-in-vue-3-4ai5</guid>
      <description>&lt;h2&gt;
  
  
  Introduction :
&lt;/h2&gt;

&lt;p&gt;Vue.js provides an efficient seamless way to communicate with sibling components using emit, but sometimes we want to trigger a method and pass data to it from a sibling component, this scenario sounds familiar, right?&lt;/p&gt;

&lt;p&gt;No worries a global event bus comes to the rescue, which is what we would usually use to trigger a method from a sibling component, but that got deprecated in Vue 3.&lt;/p&gt;

&lt;p&gt;in Vue 2 it would be something like that&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const bus = new Vue();

bus.$on(...)

bus.$emit(...)

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

&lt;/div&gt;



&lt;p&gt;In Vue 3 we would need to install an external library called Mitt&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install --save mitt&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Scenario :
&lt;/h2&gt;

&lt;p&gt;now let's picture a scenario, where we have component A and component B, they are both sibling components, component A has a button, which if clicked will invoke a method in component B, let see how this works.&lt;/p&gt;
&lt;h2&gt;
  
  
  in main.js
&lt;/h2&gt;

&lt;p&gt;We have registered the mitt globally we will inject it in both components to trigger the method by injecting it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createApp } from "vue";
import mitt from 'mitt';

const app = createApp(App);

const eventBus = mitt();

// we need to provide it to the app to be used globally
app.provide('eventBus',eventBus);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  in Component A:
&lt;/h2&gt;

&lt;p&gt;component A will emit the event when the button is clicked, which will result in passing a param to component B once mounted&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;script&amp;gt;

export default {
  name: "AppSideNav",
  // we inject the event bus in the component
  inject: ["eventBus"],
  data() {
    return {

    };
  },
  methods:{
    triggerMethod(param){
        this.eventBus.emit("trigger-method-in-componentB", param);

    }
  }

}
&amp;lt;/script&amp;gt;
&amp;lt;template&amp;gt;
  &amp;lt;div&amp;gt; 
    &amp;lt;button @click='triggerMethod(param)' &amp;gt;
       click me !
     &amp;lt;/button&amp;gt;
   &amp;lt;/div&amp;gt;
&amp;lt;/template&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  in Component B:
&lt;/h2&gt;

&lt;p&gt;we subscribe to the emitted event that will trigger the method sayMessage() in component B to be invoked.&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;script&amp;gt;
export default {
  name: "AppBoard",
    // we inject the eventbus in the component B

  inject: ["eventBus"],

  data(){
    return{
      message:'',
    },

  },
   methods:{
       sayMessage(){
          console.log(this.message)
       }
    },
   mounted() {
    this.eventBus.on("trigger-method-in-componentB", (param) =&amp;gt; {
      this.message = param;
      this.sayMessage();

    });

  },
 &amp;lt;/script&amp;gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Thank you for reading!
&lt;/h2&gt;

</description>
    </item>
    <item>
      <title>Queues in Laravel</title>
      <dc:creator>Reda-Hammada</dc:creator>
      <pubDate>Sun, 30 Apr 2023 11:40:06 +0000</pubDate>
      <link>https://dev.to/reda-hammada/queues-in-laravel-2fc0</link>
      <guid>https://dev.to/reda-hammada/queues-in-laravel-2fc0</guid>
      <description>&lt;h2&gt;
  
  
  Introduction :
&lt;/h2&gt;

&lt;p&gt;When building our application, we may face some tasks that take a long time to perform, such as uploading files and sending emails, etc …, which leads our user to wait for the task to finish, so that he can proceed.&lt;/p&gt;

&lt;p&gt;I am sure that is not a good experience we want to give to our users for them to wait for all the tasks inside the request to finish, you are thinking now of running these tasks asynchronously in the background, which can be a complicated task without Laravel framework, so lucky for us Laravel provides us with Queues.&lt;/p&gt;

&lt;p&gt;The queue component creates queued jobs to be processed in the background by moving time-consuming tasks to a queue, which leads to a better user experience for your users.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scenario :
&lt;/h2&gt;

&lt;p&gt;After we defined what queues are, now we going to picture a scenario to see how we can use them practically.&lt;/p&gt;

&lt;p&gt;You want to send your user an email, which can be time-consuming and it will make your user wait. As a result, we are going to use a queue to process that task in the background.&lt;/p&gt;

&lt;p&gt;before starting with queues, we need to provide a connection in our that can be done through a variety of different queue backends, such as Amazon SQS, Redis, or even a relational database, The connection configuration can be found in config/queue.php&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DZ-gcW_t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w6pk0n7qshs4cwwgvluw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DZ-gcW_t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w6pk0n7qshs4cwwgvluw.png" alt="Image description" width="800" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;we can configure the connection in the .env file, so no need to edit the config/queue.php file, with that being said we are going to use the database driver for our queues connection.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;QUEUE_CONNECTION=database&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
now we have configured our connection, let's jump to using the database driver.&lt;/p&gt;

&lt;p&gt;In order to use the database queue driver, you will need a database table to hold the jobs.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;php artisan queue:table&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;this command will create a migration queue table for our queue jobs to be stored there to run in the background.&lt;/p&gt;

&lt;p&gt;after creating the migration we need to run this command to migrate our table&lt;/p&gt;

&lt;p&gt;&lt;code&gt;php artisan migrate&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
now we are ready to create our first queue job that will handle our email logic, keep in mind that this article assumes that you already know how email sending works in Laravel&lt;/p&gt;

&lt;p&gt;&lt;code&gt;php artisan make:job SendMail&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
all of the queueable jobs for your application are stored in the app/Jobs directory. If the app/Jobs directory doesn't exist, it will be created when you run the previous command&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\Jobs;

use Illuminate\Bus\Queueable; 
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;


class SendMail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // 
        Mail::raw('This is  a queued job email', function($message){
            $message-&amp;gt;to('queueexample@gmail.com')-&amp;gt;subject('queued job ');
        });
    }
}

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

&lt;/div&gt;



&lt;p&gt;our queue logic goes inside the handle function, we have put our mail logic inside it, and now we can dispatch it for this task to be queued when called to run in the background without making our user waits.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Route::get('send-mail', function(){

    SendMail::dispatch();

    dd('the mail has been sent');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;when our user hit the route, he is going to receive the message of the mail has been sent without waiting for the email to be sent, and what really happened when we call called for the SendMail queued job dispatch is that we stored our queued job in the database in the jobs tables pending waiting to be processed, so the user will not receive the email until, we process our jobs we can do that by running the following command.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Thank your for reading
&lt;/h2&gt;

</description>
    </item>
  </channel>
</rss>
