I'm using this tutorial for laravel broadcast with redis and socket-io,
https://www.itsolutionstuff.com/post/laravel-broadcast-redis-socket-io-tutorial-example.html.
But in the final result, I don't get any message on page, actually it doesn't listen to the event when it fires.
Redis out put
1622090795.392138 [0 127.0.0.1:40302] "SELECT" "0"
1622090795.392327 [0 127.0.0.1:40302] "EVAL" "for i = 2, #ARGV do\n redis.call('publish', ARGV[i], ARGV[1])\nend" "0" "{\"event\":\"UserEvent\",\"data\":{\"title\":\"This notification from ItSolutionStuff.com\",\"socket\":null},\"socket\":null}" "user-channel"
1622090795.392370 [0 lua] "publish" "user-channel" "{\"event\":\"UserEvent\",\"data\":{\"title\":\"This notification from ItSolutionStuff.com\",\"socket\":null},\"socket\":null}"
Laravel Echo Server
pc@pc-ThinkPad-T440:/opt/lampp/htdocs/app$ laravel-echo-server start
L A R A V E L E C H O S E R V E R
version 1.6.2
⚠ Starting server in DEV mode...
✔ Running at localhost on port 6001
✔ Channels are ready.
✔ Listening for http events...
✔ Listening for redis events...
Server ready!
Channel: user-channel
Event: UserEvent
Laravel Echo setup.js file
import Echo from 'laravel-echo';
window.io = require('socket.io-client');
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ":" + 6001
// host: window.location.hostname + ":" + window.laravel_echo_port
});
My Welcome blade file
<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>Laravel Broadcast Redis Socket io Tutorial - ItSolutionStuff.com</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>Laravel Broadcast Redis Socket io Tutorial - ItSolutionStuff.com</h1>
<div id="notification"></div>
</div>
</body>
<script>
window.laravel_echo_port='{{env("LARAVEL_ECHO_PORT")}}';
</script>
<script src="//{{ Request::getHost() }}:6001/socket.io/socket.io.js"></script>
<script src="{{ url('/js/laravel-echo-setup.js') }}" type="text/javascript"></script>
<script type="text/javascript">
var i = 0;
console.log(Echo);
Echo.channel('user-channel')
.listen('.UserEvent', (data) => {
i++;
console.log(data);
$("#notification").append('<div class="alert alert-success">'+i+'.'+data.title+'</div>');
});
</script>
</html>
And SendMessage.php event file,
<?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 Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public $data = ['asas'];
public function __construct()
{
//
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new Channel('user-channel');
// return new PrivateChannel('channel- name');
}
public function broadcastAs()
{
return 'UserEvent';
}
/**
* The event's broadcast name.
*
* @return string
*/
public function broadcastWith()
{
return ['title'=>'This notification from ItSolutionStuff.com'];
}
}
And the routes that I use
Route::get('/t', function () {
event(new \App\Events\SendMessage());
dd('Event Run Successfully.');
});
Route::get('/', function () {
return view('welcome');
});
My config/database.php looks same as what mentioned in tutorial
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
'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'),
],
],
My Answer:
't have a specific question or request in your statement. However, I can provide some information on how to set up Laravel broadcasting with Redis and Socket.io.
To set up Laravel broadcasting with Redis and Socket.io, you will need to follow these steps:
1. Install the Laravel Echo package by running the following command:
npm install laravel-echo socket.io-client
2. Configure your Laravel application to use Redis as the broadcasting driver. Update your `.env` file with the following configuration:
BROADCAST_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
3. Update your `config/broadcasting.php` file to use the Redis driver:
php
'connections' => [
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
],
4. Create an event that you want to broadcast. For example, you can create a `TestEvent` class in your `app/Events` directory:
php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class TestEvent implements ShouldBroadcast
{
public $message;
public function __construct($message)
{
$this->message = $message;
}
public function broadcastOn()
{
return new Channel('test-channel');
}
}
5. Broadcast the event from your controller or wherever you want to trigger the event:
php
use App\Events\TestEvent;
public function broadcastTestEvent()
{
event(new TestEvent('Hello, world!'));
}
6. Set up Laravel Echo in your frontend application to listen for the broadcasted event. For example, you can listen for the `TestEvent` event in your JavaScript file:
javascript
import Echo from 'laravel-echo'
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001'
});
window.Echo.channel('test-channel')
.listen('TestEvent', (e) => {
console.log(e.message);
});
7. Start the Laravel Echo server by running the following command:
php artisan queue:listen --queue=redis --tries=3
8. Start the Laravel WebSocket server by running the following command:
php artisan websockets:serve
With these steps, you should be able to set up Laravel broadcasting with Redis and Socket.io in your application. Let me know if you have any specific questions or issues with this setup.
Rate this post
4 of 5 based on 3508 votesComments