In the today’s technogy age real time data has become an essential thing. So here, in this article i will share laravel real time event broadcasting with socket.io. Here in this example we will see how can we broadcast real times events.

Basically, to acheive this thing in our laravel application, we will use predis which is a laravel package, queue, socket.io, laravel-echo-server and event broadcasting.

So, we will need to install the above things which are not provided in the laravel appication by default. I will provide all the step to implement real time broadcasting event in the various steps from a fresh installation of laravel application. If you have already installed laravel application then you can directly jump on the next step. So without wasting our time let’s start the implementation process and follow all the steps as given below.

Step 1: Install Laravel

For installing fresh laravel application, you will just needto run the following command in your terminal or command prompt.

composer create-project --prefer-dist laravel/laravel realtimeapp

I have given a name realtimeapp to this application, you are frre to give the name of your choice.

Now, run the following command to give permission to the storage and cache directories.

sudo chmod -R 777 /var/www/html/realtimeapp/storage
sudo chmod -R 777 /var/www/html/realtimeapp/bootstrap/cache<br><br>

Step 2: Install predis

In this second step, we will need to install predis. So open your terminal and run the following command.

composer require predis/predis

composer require predis/predis

Step 3: Create event for broadcasting

Now we will need to create an event for broadcasting and in the event file we will set channal and write message. So run the following command to create event.

php artisan make:event SendMessage

Above command will create an event file SendMessage.php file under app/Events directory. So open this file and update it like below.

app/Events/SendMessage.php

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;

class SendMessage implements ShouldBroadcastNow
{
    use InteractsWithSockets, SerializesModels;

    public $data = ['asas'];

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

    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('user-channel');
    }

    /**
     * The event's broadcast name.
     *
     * @return string
     */
    public function broadcastAs()
    {
        return 'UserEvent';
    }
    /**
     * The event's broadcast name.
     *
     * @return string
     */
    public function broadcastWith()
    {
        return ['title'=>'Notification message will go here'];
    }
}

#laravel #how to broadcast events in laravel #laraevl socket.io #laravel broadcasting event #laravel event broadcasting #laravel realtime event broadcasting with socket.io

Laravel real time event broadcasting with socket.io example
5.15 GEEK