How to log Pusher PHP event trigger fails in CodeIgniter?

787 Views Asked by At

So I have a problems working with Pusher on wamp server using CodeIgniter. Client side works fine, but PHP does not triger events. PHP does authenticate channels, but does not trigger events. If I use Pusher event creator clients get events.

Here is code:

function send_event($channel, $event, $message, $socket_id = NULL){
    $channel_array = array();
    array_push($channel_array, $channel);
    $data['message'] = $message;
    if(is_null($socket_id)){
        $response = $this->pusher->trigger($channel_array, $event, $data);
    }else{
        $response = $this->pusher->trigger($channel_array, $event, $data, $socket_id);
    }
    var_dump($response);        
}

var_dump return FALSE. I would want to understand the issue more but I am stuck on how to debug. From pusher PHP docs

class MyLogger {
  public function log( $msg ) {
    print_r( $msg . "\n" );
  }
}

$pusher->set_logger( new MyLogger() );

But if I use this code I get CI errors. I tried to use myLogger in my Pusher library like this:

function __construct()
    {
        class MyLogger {
          public function log( $msg ) {
            print_r( $msg . "\n" );
          }
        }

        $this->CI =& get_instance();
        $this->CI->load->library('tank_auth');

        require_once( 'Pusher.php' );

        $this->options['encrypted'] = true;
        $this->pusher = new Pusher(
            $this->key,
            $this->secret,
            $this->app_id,
            $this->options
        );
        $logger= new MyLogger;
        $this->pusher->set_logger( $logger );
    }

    function send_event($channel, $event, $message, $socket_id = NULL){
        $channel_array = array();
        array_push($channel_array, $channel);
        $data['message'] = $message;
        if(is_null($socket_id)){
            $response = $this->pusher->trigger($channel_array, $event, $data);
        }else{
            $response = $this->pusher->trigger($channel_array, $event, $data, $socket_id);
        }
        var_dump($response);        
    }

but got error:

Class declarations may not be nested
1

There are 1 best solutions below

0
On BEST ANSWER
function __construct()
{
    class MyLogger {
      public function log( $msg ) {
        print_r( $msg . "\n" );
      }
    }
}

This is invalid code, you cant declare a class inside a function or other class.

It must be declared in gobal scope or namespace:

// declare first class
class MyLogger {
    public function log( $msg ) {
        print_r( $msg . "\n" );
    }
}

// now you can instantiate the class inside you second class
class PusherOrWhatever {
    public function __construct() {   
        $this->pusher->set_logger( new MyLogger );
    }
}