Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 620
Skeeith22 Avatar asked Jan 28 '23 12:01

Skeeith22


1 Answers

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)
                })
like image 150
The Architect Avatar answered Jan 31 '23 09:01

The Architect