Proof of concept, Laravel Chat App with PresenceChannels, using Echo Server as SocketIo server all connected with Redis.
Frontend use Vue and Vue-Router.
This is the flow when a user authenticate within Laravel App and then with Echo Server.
Once authenticated, user can send a private message to another authenticated user.
laravel new l58-EchoServer
composer require predis/predis
composer require laravel/passport
npm install -g laravel-echo-server
npm install --save socket.io-client laravel-echo vue-router
npm i
vim .env
- database
- redis
- session
CREATE DATABASE l58echo CHARACTER SET utf8 COLLATE utf8_general_ci;
php artisan make:auth
php artisan migrate
php artisan passport:install
Encryption keys generated successfully.
Personal access client created successfully.
Client ID: 1
Client secret: qw02adExQHk1YQoBSdxRIYmBrcJ663Y69m6bygE3
Password grant client created successfully.
Client ID: 2
Client secret: iVpG318MlLyQI1CAScjsTePP71POsrVqwAYw4heR
Add the Laravel\Passport\HasApiTokens trait to your App\User model.
Add Passport::routes method within the boot method of your AuthServiceProvider
Edit config/auth.php Api driver should be passport and delete 'hash' => false
Edit providers/BroadcastServiceProvider.php Broadcast::routes(['middleware' => ['auth:api']]);
php artisan passport:keys
Uncomment this provider in the providers array of your config/app.php
App\Providers\BroadcastServiceProvider::class
laravel-echo-server init
Will create laravel-echo-server.json file, heads up with Cors configuration.
In config/database.php disable prefix for redis. laravel/echo#232 (comment)
laravel-echo-server start
php artisan make:event ControlChannelEvent php artisan make:event ChatMessageEvent
broadcast(new App\Events\NewChatEvent($user, 'My Message')) Above command will not fire the event until __destruct method is called
broadcast(new App\Events\NewChatEvent($user, 'My Message'))->__destruct()
With event() it get fired without need to call extra methods
event(new App\Events\NewChatEvent($user, 'My Message'))
redis-cli -h redis.local monitor
How to issue Bearer Token
$user->createToken('Token Name')->accessToken;