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 don 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 AppEvents;
use IlluminateBroadcastingChannel;
use IlluminateQueueSerializesModels;
use IlluminateBroadcastingPrivateChannel;
use IlluminateBroadcastingPresenceChannel;
use IlluminateFoundationEventsDispatchable;
use IlluminateBroadcastingInteractsWithSockets;
use IlluminateContractsBroadcastingShouldBroadcast;
use IlluminateContractsBroadcastingShouldBroadcastNow;
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 IlluminateBroadcastingChannel|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 AppEventsSendMessage());
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'),
],
],
3
Answers
In laravel config/database.php file under REDIS_PORT use port 40302 that you are using with the redis server
In package.json socket.io-client can be work version
Please check your socket.io-client version in the package.json
Laravel Echo Server, Redis, Socket.IO: Can't seem to make them work