Search code examples
javascriptlaravel-5socket.iovuejs2laravel-echo

Laravel Echo how to handle connected, disconnected, reconnecting and etc


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

I'm using VueJS btw


Solution

  • 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)
                    })