Search code examples
laravellaravel-5pusherlaravel-echo

Laravel Echo error handling (with Pusher)


Is there any guide about this topic? I have read the pusher documentation and it seems fairly easy to manage a disconnect with a code similar to the following:

pusher.connection.bind('disconnected', function() {
    // Do Something
})

I'm not sure how to integrate it with Echo, since my code is as follows:

window.EchoConnection = new Echo({
    broadcaster: 'pusher',
    key: window.EchoKey,
    cluster: 'eu',
    encrypted: true
});

EDIT: in order to check a disconnect event, run window.EchoConnection.connector.pusher.connection.disconnect() in your console


Solution

  • I haven't tried it yet, but according to the github repo this should work for pusher:

    window.EchoConnection is an Echo Object. When you create a new pusher instance with echo, the connector variable will be a PusherConnector:

    if (this.options.broadcaster == 'pusher') {
       this.connector = new PusherConnector(this.options);
    }
    

    Over this variable you can find the Pusher instance that is created:

    connect(): void {
        this.pusher = new Pusher(this.options.key, this.options);
    }
    

    The theoretical solution for binding events to the pusher would be:

    window.EchoConnection.connector.pusher.connection.bind('disconnected', function() {
        // Do Something
    })