Create Real-Time Component using Laravel Echo, Pusher & Vue.js

Real-Time Notification, Reply, Like & Dislike System

Mosharrf Hossain
Feb 19 ยท 4 min read

๐Ÿ’Ž Part 1 โ€” Real-Time Like and Dislike Component

Step: 1 โ€” Setting up your Pusher application

Create a Pusher account, if you have not already, and then set up your application as seen in the screenshot below.

Step: 2โ€” Configure Laravel application

๐Ÿ“ฆ Package Install: Laravel Pusher

we will need to install the Pusher PHP SDK, you can do this using Composer by running the command below:

composer require pusher/pusher-php-server "~3.0"

When Composer is done, open the .env file that is in the root directory of your Laravel installation. Update the values to correspond with your your Pusher credentials like:

BROADCAST_DRIVER=pusher // Get the credentials from your pusher dashboardPUSHER_APP_ID=XXXXX

Open config/app.php and providers


๐Ÿ“ฆ Package Install: Laravel Echo

Again we will need to install the Laravel Echo, you can do this using Composer by running the command below:

npm install --save laravel-echo pusher-js

When Composer is done, open the resources/js/bootstrap.js file and :

import Echo from 'laravel-echo'window.Pusher = require('pusher-js');window.Echo = new Echo({   broadcaster: 'pusher',   key: "your-pusher-key",   cluster: "your-pusher-cluster",   encrypted: true,});

Step: 3โ€” Creating our application (Back-end)

Now that we are done with configuration, let us create our application. First we would create an Event class that would broadcast to Pusher from our Laravel application. Events can be fired from anywhere in the application. you can do this by two way one is using Composer by running the command below:

php artisan make:event LikeEvent

Another is by editing app/providers/EventServiceProvider.php

๐Ÿ“ Edit: Event Service Provider File

protected $listen = [   Registered::class => [      SendEmailVerificationNotification::class,   ],   'App\Events\LikeEvent' => [      'App\Listerners\LikeEventListener',    ],];

Then run this command:

php artisan event:generate

This will create a new LikeEvent.php file in the app/Events directory and LikeEventListener.php file in the app/Listerners , Now open LikeEvent.php file and update to the following below:

๐Ÿ“ Edit: Like Event File

class LikeEvent implements ShouldBroadcast {   use Dispatchable, InteractsWithSockets, SerializesModels;   public $id;   public $type;   public function __construct($id, $type)   {       $this->id = $id;       $this->type = $type;   }   public function broadcastOn()   {       return new Channel('likeChannel');   }}

Above, we have implemented the ShouldBroadcast interface and this tells Laravel that this event should be broadcasted using whatever driver we have set in the configuration file.

Then we need to create the likeChannel Channel. For that, open routes/channel.php and add this code:

๐Ÿ“ Edit: Channel file

Broadcast::channel('likeChannel', function () {     return true; //Always return true or false});

๐Ÿšฆ Fire The Event

After creating even file we need to file that event by using broadcastfunction.

In out project we have LikeController.php file where like and dislike functions are stored. We will fire the event in this file.

๐Ÿ“ Edit: Like Controller file

public function likeIt(Reply $reply){    $reply->like()->create([       'user_id' => auth()->id(),     ]);     broadcast(new LikeEvent($reply->id, 1))->toOthers();}public function unLikeIt(Reply $reply){    $reply->like()->where('user_id', auth()->id())->first()->delete();    broadcast(new likeEvent($reply->id, 0))->toOthers();}

๐Ÿ‘€ Checking: Check Our application send data to pusher debug

Step: 4โ€” Work on our Front end

๐Ÿšฆ Listening That Event

We have to listen our event broadcasts. First, use the channel method to retrieve an instance of a channel, then call the listen method to listen for a specified event

In our existing application we have resources\js\components\Likes\like.vue File. We will work on this file.

๐Ÿ“ Edit: Like View file

mounted(){'likeChannel') //Should be Channel Name        .listen('LikeEvent', () => {           if( =={                 e.type== 1? .count ++ : .count --           }        });},

๐Ÿ‘€ Checking: Testing our set up

๐Ÿ™Œ Final Wrap Up

Thank for reading out. In the next part we will work on real-time reply and notification component. Click Here for the next part.

The code is available on GitHub, you can star, fork and play around with it.

Mh Mohon

This is My blog