6

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

I'm using VueJS btw

Skeeith22
  • 186
  • 2
  • 16

2 Answers2

9

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)
                })
The Architect
  • 313
  • 2
  • 10
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

maximus1127
  • 936
  • 11
  • 30