Today, out leading topic is event broadcasting with redis and socket.io in laravel application. i want to give you very simple example of laravel broadcast using rest and socket io in laravel application. you can send real time chat message using rest socket io and event broadcasting with laravel 6, laravel 7, laravel 8 and laravel 9 application.
Laravel provide event broadcasting topic. event broadcast is a very interesting and it's also difficult to implement with redis and socket.io as specially. but i will give you step by step instruction of how to send real time message with rest and socket io in laravel 6 application.
You need to just follow few step to done this following thing. so let's follow bellow steps and done as real time notification with laravel.
Step 1: Install Laravel 6
First of all, we need to get fresh Laravel 6 version application using bellow command because we are going from scratch, So open your terminal OR command prompt and run bellow command:
composer create-project --prefer-dist laravel/laravel blog
Step 2: Install predis
In this step, we need to install predis as bellow command. so let's run following command to install predis in laravel app.
Here, we need to create event for broadcasting. in event file we need to set channel and send message array with key. so, let's run following command for creating event.
php artisan make:event SendMessage
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'=>'This notification from ItSolutionStuff.com']; }}
Step 4: Update configuration File
In this step, we need to add set configuration on env file and database configuration file. you you need to set env file with BROADCAST_DRIVER as redis and database configuration and also database redis configuration.
In this step, we need to install laravel-echo-server in your system and in your project. so let's run following command to install laravel-echo-server and init.
Install laravel-echo-server
npm install -g laravel-echo-server
Init laravel-echo-server
laravel-echo-server init
You have to setup your configuration. you can see bellow screenshot:
It will create new file laravel-echo-server.json file as like bellow: