Get Pusher connected status real time

Viewed 698

I use Pusher.com to set up real time notification. I rather use it for real time table sync.

But sometimes, it doesn't work well due to connection issue. So I want to check if pusher connect is active or not. I'm using Laravel for this system and I displayed the status with icon(Green and red icon).

Here is the code what I written in blade.php file

    var pusher = new Pusher('dfgfg', {
                cluster: 'us3',
                encrypted: true
            });

            pusher.connection.bind('connected', function() {
                $('#pusheron').css('display','flex');
                $('#pusheroff').css('display','none');
            });
            pusher.connection.bind('error', function() {
                $('#pusheron').css('display','none');
                $('#pusheroff').css('display','flex');
            });

This is what I've done now. But it sometimes doesn't work correctly. I would be glad if someone teach me other way to get pusher connection status in real time. Regards

1 Answers

Pusher emits events when the connection state changes. You can use this to detect changes in real time and update the status display.

pusher.connection.bind('state_change', function(states) {
  // states = {previous: 'oldState', current: 'newState'}
  $('div#status').text("Channels current state is " + states.current);
});

See https://pusher.com/docs/channels/using_channels/connection#connection-states.

Related