Sergalas
Sergalas

Reputation: 111

Laravel-echo-server no connection client to the server

laravel-echo-server.json

{
  "authHost": "http://want_can.lc",
  "authEndpoint": "/broadcasting/auth",
  "clients": [],
  "database": "redis",
  "databaseConfig": {
    "redis": {
            "port": 6379,
            "host":"127.0.0.1",
            "keyPrefix":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": false,
    "allowOrigin": "",
    "allowMethods": "",
    "allowHeaders": ""
  }
}

bootstrap.js

if (typeof io !== 'undefined') {
window.Echo = new Echo({
     broadcaster: 'socket.io',
     host:window.location.hostname +":"+ '6001',
 });
}else{
    console.log('AAA!')
}

PrivateChat event

    class PrivateChat implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $data;
    public $user;

    public function __construct($data,User $user)
    {

        $this->data=$data;
        $this->user=$user;
        $this->dontBroadcastToCurrentUser();
    }

    public function broadcastOn()
    {
        Message::create($this->data,$this->user);
        return new PrivateChannel('room.'.$this->data['room_id']);
    }
}

controller

class MessageController
{
    public $service;

    public function __construct(ChatroomService $service)
    {
        $this->service=$service;
    }

    public function message(ChatRequest $request,User $user)
    {
        event ( new PrivateChat($request->all(), $user));
    }
}

channel.php

Broadcast::channel('room.{room_id}', function ( $user, $room_id) {
    return $user->chatroomsCustomer->contains($room_id)||$user->chatroomsExecutor->contains($room_id);
});

config/database.php key-perfix settings for redis

'options' => [
            'cluster' => env('REDIS_CLUSTER', 'redis'),
            'prefix' => null,
        ],

vue.js component

    methods:{
        update:function (id) {
            axios.post('chatroom',{
            room_id:id
            }).then((response)=>{
                this.message=response.data;
            });

            window.Echo
            .private('room'+{id})
            .listen('PrivateChat',
                ({data})=>{
                    console.log(data)
                    this.message.messages.push(data.body)
                })
        },
        sendMessage(room_id){
            axios.post('/message/'+this.user,{
                text:this.textMessage,
                room_id:room_id
            }).then((response)=>{});
            this.message.messages.push({user_id:this.user,'text':this.textMessage})
            this.textMessage='';
        }
    }
}

start laravel-echo-server. Tab network https://prnt.sc/xyunid the socket does not make sid. If the user types a message in the terminal

Channel: private-room.1
Event: App\Events\PrivateChat

here's what I get in the queue

[2021-02-01 06:50:25][RXvfnlILeDt6J2MTWAXPA9EStl1Cgi5X] Processing: App\Events\PrivateChat
[2021-02-01 06:50:25][RXvfnlILeDt6J2MTWAXPA9EStl1Cgi5X] Processed:  App\Events\PrivateChat

Upvotes: 0

Views: 509

Answers (1)

Sergalas
Sergalas

Reputation: 111

The problem was in the sockets.io-clent version. They are already the third version and laravel-echo-server has not been redone yet.

Upvotes: 1

Related Questions