0

I want to implement pusher with php and javascript.

I have included pusher library for php with composer.

And my php code is

<?php
require_once "vendor/autoload.php";

$app_id = "XX";
$app_key = "XX";
$app_secret = "XX";
$cluster = "ap1";

$pusher = new Pusher( $app_key, $app_secret, $app_id, array( 'encrypted' => true ) );

$pusher->trigger( 'XX-channel', 'test', 'hello world' );

And in javascript

<head>
    <title>Pusher</title>
    <script src="https://js.pusher.com/4.1/pusher.min.js"></script>

</head>
<body>

<script type="text/javascript">
        const socket = new Pusher("XXX", {
            cluster: 'ap1'
        });
        const channel = socket.subscribe('XX-channel');

        channel.bind('test', function (data) {
          console.log(data);
        });
    </script>
</body>

But when I refresh page I dont receive any console logs.

Any idea what might be wrong?

5
  • Are you loading the JS before the PHP triggers ? Commented May 5, 2017 at 10:10
  • Also.. Are you not supposed to be setting up a websocket server (possibly nodeJS??) to listen to messages ? Are you seeing anything there ? Commented May 5, 2017 at 10:12
  • I just tried refreshing php page to receive log. Commented May 5, 2017 at 10:13
  • You should be able to identify whether the cause of the issue is your PHP or javascript code using the debug console in your pusher app dashboard. If you're able to see your console.logs when you send test events to XX-Channel using the event creator, the issue is in your PHP code and you should enable logging to try and help track this down. Commented May 5, 2017 at 10:20
  • @leesio Thanks. Issue is with php Commented May 5, 2017 at 10:24

1 Answer 1

1
$pusher = new Pusher( $app_key, $app_secret, $app_id, array( 'cluster'=> $cluster) );

$pusher->trigger( 'XX-channel', 'test', 'hello world' );

Found issue.

I forgot to add cluster.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.