This is an example source for a real-time event broadcasting system in Laravel using Redis and Socket.io without relying on third-party servers like Pusher.
- Laravel Mix
- Redis Server
- Predis Package
- Socket.io-client
- Laravel-echo-server
- Laravel-echo
- Clone this repository and rename the
.env.copy
file to.env
- Run the following commands:
composer install npm install
- Install Redis Server see below, point number 9 about how to Install Redis Server
- Install laravel-echo-server see below, point number 5 about how to install, Initialize(Configuration)
- Start the Laravel development server using the command:
php artisan serve
- Start Redis Server
- Start laravel echo server by following command:
- for Linux: Run
laravel-echo-server start
- for Windows: Run
npx laravel-echo-server start
- for Linux: Run
- Open following urls in browser:
-
Install Predis using the following command:
composer require predis/predis
-
Create an event for broadcasting. Run the command:
php artisan make:event SendMessage
Then, set the channel and message in app/Events/SendMessage.php as shown in this example file.
-
Update the configuration in the
.env
file:BROADCAST_DRIVER=redis REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379 LARAVEL_ECHO_PORT=6001
-
Update the configuration in
config/database.php
and run:php artisan migrate
// config/database.php ... 'redis' => [ // 'client' => env('REDIS_CLIENT', 'phpredis'), 'client' => env('REDIS_CLIENT', 'predis'), 'options' => [ 'cluster' => env('REDIS_CLUSTER', 'redis'), // 'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'), 'prefix' => env('REDIS_PREFIX', ''), ], 'default' => [ 'url' => env('REDIS_URL'), 'host' => env('REDIS_HOST', '127.0.0.1'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', '6379'), 'database' => env('REDIS_DB', '0'), ], 'cache' => [ 'url' => env('REDIS_URL'), 'host' => env('REDIS_HOST', '127.0.0.1'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', '6379'), 'database' => env('REDIS_CACHE_DB', '1'), ], ] ...
-
Install laravel-echo-server using the following command:
npm install -g laravel-echo-server
After installing, run
laravel-echo-server init
(Linux) ornpx laravel-echo-server init
(Windows) to initialize Laravel Echo Server and set up configurations. The configurations will be saved inlaravel-echo-server.json
.{ "authHost": "http://localhost:8000", "authEndpoint": "/broadcasting/auth", "clients": [], "database": "redis", "databaseConfig": { "redis": { "port": "6379", "host": "127.0.0.1" }, "sqlite": { "databasePath": "/database/laravel-echo-server.sqlite" } }, "devMode": true, "host": null, "port": "6001", "protocol": "http", "socketio": {}, "secureOptions": 67108864, "sslCertPath": "", "sslKeyPath": "", "sslCertChainPath": "", "sslPassphrase": "", "subscribers": { "http": true, "redis": true }, "apiOriginAllow": { "allowCors": false, "allowOrigin": "", "allowMethods": "", "allowHeaders": "" } }
-
Install Node Modules by running:
npm install
-
Install Laravel Echo and Socket.io-client:
npm install laravel-echo [email protected]
-
Create the
resources/js/laravel-echo-setup.js
file. Add the following towebpack.mix.js
and compile usingnpm run dev
:mix.js('resources/js/laravel-echo-setup.js', 'public/js');
-
Create a view file like
resources/views/notification-test.blade.php
. -
Create a route in
routes/web.php
:Route::get('/', function () { return view('notification-test'); // return view('welcome'); }); Route::get('/test', function () { event(new \App\Events\SendMessage()); echo 'Event Run Successfully.'; });
-
-
Install Redis Server using:
- For Linux:
sudo apt install redis-server
- For Windows: Download from Microsoft Archive Redis Releases and extract zip file to specific location. Follow the instructions for installation. You can either run
redis-server.exe
andredis-cli.exe
directly or set the extracted folder location in the Windows environment variables globally.for my case: I put extracted folder in C:\redis\Redis-x64-3.0.504 directory & I set that path
C:\redis\Redis-x64-3.0.504
in the Windows environment variables globally. then I need to run onlyredis-server
command for start the redis-server (we can run that command from any location) & run onlyredis-cli
command for start redis-cli
Note: For more details about Redis on Windows, refer to this Stack Overflow question.
- For Linux:
All Done! Now, start the Laravel development server, Laravel Echo Server, and Redis Server to run the project See above steps.
If you encounter any issues, refer to this text note in the repository.
I hope this helps! Happy coding!