laravelwebsocketlaravel-echophpwebsocketlaravel-websockets

laravel-echo and socket.io are unable to connect with laravel-echo-server


i am new to web sockets, i am using laravel echo, redis, socket.io with laravel-echo-server. But when i console.log(window.Echo.connector.socket.connected), it prints false in console.

My terminal after running laravel-echo-server start:
L A R A V E L  E C H O  S E R V E R

version 1.6.3

⚠ 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: bilmarken_database_custom_notifications
Event: App\Events\NewNotificationCreated
Channel: bilmarken_database_custom_notifications
Event: App\Events\NewNotificationCreated

it is working properly i guess.

here is my laravel-echo-server.json code:
{
    "authHost": "127.0.0.1:8000",
    "authEndpoint": "/broadcasting/auth",
    "clients": [],
    "database": "redis",
    "databaseConfig": {
        "redis": {
            "host": "127.0.0.1",
            "port": "6379",
            "password": null
        },
        "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": true,
        "allowOrigin": "*",
        "allowMethods": "GET, POST",
        "allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
    }
}

code in main.ts file

import io from 'socket.io-client';
import Echo from 'laravel-echo';

window.io = io;

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: "http://"+window.location.hostname + ':6001',
});

code for NewNotificationCreated.php:

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;

class NewNotificationCreated implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $msg;

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

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

can somebody tell where i am going wrong. I am uable to receive the events.


Solution

  • For those who might get stuck with this problem in the future, I solved the problem by adding in :

    REDIS_PREFIX=
    

    in my .env file

    After doing this, everything worked fine.