Laravel Echo how to handle connected, disconnected, reconnecting and etc
Asked Answered
I

2

8

Does anyone know how to handle the connected, disconnected, reconnecting and etc on Laravel Echo?

I'm using VueJS btw

Impasse answered 14/3, 2018 at 3:49 Comment(0)
C
13

to connect you do this:

import Echo from 'laravel-echo'

in your function or on load you then do this:

                window.Echo = new Echo({
                    broadcaster: 'socket.io',
                    host: socketServerURL, //whatever url you need
                    auth: {headers: {Authorization: 'Bearer ' + Vue.auth.getToken() }}
                });

                window.Echo.connector.socket.on('connect', function(){
                    this.isConnected = true
                })

                window.Echo.connector.socket.on('disconnect', function(){
                    this.isConnected = false
                })

                window.Echo.private('contacts').listen('ContactUpdated', event => {
                    console.log(event)
                })
Credible answered 6/9, 2018 at 19:50 Comment(2)
The second chunk of code solved my problem for X-Socket-Id not found.Eggshaped
When you get disconnect event by the way?Saad
T
2

Late to the party but I tried the selected answer with no success. I suppose maybe laravel echo updated some stuff since this was answered. I've found the correct and updated answer here:

Binding callbacks on Laravel Echo with Laravel Websockets

Tenno answered 16/9, 2021 at 17:58 Comment(1)
this only works for Pusher, how about Redis (Socket.io)Impasse

© 2022 - 2024 — McMap. All rights reserved.