laravel echo , broadcast , socket.io , redis configration

Posted 1 week ago by seantiago24

i'm starting a laravel project configuration , for chat app , so for that i'm using laravel-echo-server and socket.io and redis , no pusher .

i've configured all and it seems ok cause i'm not getting any errors in my chrome console , but i'm not getting any response , for now i'm just trying to console a random message ( console.log('test ok') ) and still not getting it . route file web.php

Route::get('/', function () { return view('welcome'); });

Route::get('/event' , '[email protected]');

Controller tesController.php

public function event (){ Broadcast(new ExampleEvent('message de test')); return 'Broadcasted'; }

view file welcome.blade.php

    <title>Laravel</title>

    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">
    
    <meta name="csrf-token" content="{{ csrf_token() }}">
   
</head>
<body>

    <div class="flex-center position-ref full-height">
        @if (Route::has('login'))
            <div class="top-right links">
                @auth
                    <a href="{{ url('/home') }}">Home</a>
                @else
                    <a href="{{ route('login') }}">Login</a>

                    @if (Route::has('register'))
                        <a href="{{ route('register') }}">Register</a>
                    @endif
                @endauth
            </div>
        @endif

        <div id="app" class="content">
            <div class="title m-b-md">
                Chat
            </div>
                @yield('section')
            
        </div>
    </div>
    <script src="{{ asset('js/app.js') }}"></script>
    <script type="text/javascript">
        Echo.channel('testevent')
        .listen('ExampleEvent', (e) => {
            console.log(e);
            alert('here');
            console.log('here');
        });
    </script>
</body>

laravel-echo-server.json

{ "authHost": "http://localhost", "authEndpoint": "/broadcasting/auth", "clients": [], "database": "redis", "databaseConfig": { "redis": {}, "sqlite": { "databasePath": "/database/laravel-echo-server.sqlite" } }, "devMode": true, "host": null, "port": "6001", "protocol": "http", "socketio": {}, "sslCertPath": "", "sslKeyPath": "", "sslCertChainPath": "", "sslPassphrase": "", "subscribers": { "http": true, "redis": true }, "apiOriginAllow": { "allowCors": false, "allowOrigin": "", "allowMethods": "", "allowHeaders": "" } }

and i've added this in bootstrap file

import Echo from 'laravel-echo' window.io = require('socket.io-client');

window.io = require('socket.io-client'); window.Echo = new Echo({ broadcaster: 'socket.io', host: 'localhost:6001' });

the Event file :

class ExampleEvent implements ShouldBroadcast {

public $message;
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
 * Create a new event instance.
 *
 * @return void
 */
public function __construct($message)
{
    $this->message = $message;
}

/**
 * Get the channels the event should broadcast on.
 *
 * @return \Illuminate\Broadcasting\Channel|array
 */
public function broadcastOn()
{
    return new Channel('testevent');
}



public function broadcastWith()
{

    return [
        'data' => 'key'
    ];
}

}

and yes i've already started laravel-echo-server start and installed all the packages needed (predis , socket.io-client ...) and redis is lunched and working .. i've uncommented the line of broadcastProvider in app.php

in the cmd that started the laravel-echo server it shows that an event is fired , but not in redis .. that'sthe message :

[00:50:54] - LkC1zwJemGYIVcI1AAAA joined channel: testevent Channel: laravel_database_testevent Event: App\Events\ExampleEvent

as i said i'm just testing to console any random message to continue working and the other treatment , but not getting any thing in the console , no errors , no response ..

any body know whyy

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.