PHP code example of pusher / pusher-php-server
1. Go to this page and download the library: Download pusher/pusher-php-server library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
pusher / pusher-php-server example snippets
$app_id = 'YOUR_APP_ID';
$app_key = 'YOUR_APP_KEY';
$app_secret = 'YOUR_APP_SECRET';
$app_cluster = 'YOUR_APP_CLUSTER';
$pusher = new Pusher\Pusher($app_key, $app_secret, $app_id, ['cluster' => $app_cluster]);
$options = [
'cluster' => $app_cluster,
'useTLS' => false
];
$pusher = new Pusher\Pusher($app_key, $app_secret, $app_id, $options);
// where $logger implements `LoggerInterface`
$pusher->setLogger($logger);
$custom_client = new GuzzleHttp\Client();
$pusher = new Pusher\Pusher(
$app_key,
$app_secret,
$app_id,
[],
$custom_client
);
$pusher->trigger('my-channel', 'my_event', 'hello world');
$pusher->trigger([ 'channel-1', 'channel-2' ], 'my_event', 'hello world');
$batch = [];
$batch[] = ['channel' => 'my-channel', 'name' => 'my_event', 'data' => ['hello' => 'world']];
$batch[] = ['channel' => 'my-channel', 'name' => 'my_event', 'data' => ['myname' => 'bob']];
$pusher->triggerBatch($batch);
$promise = $pusher->triggerAsync(['channel-1', 'channel-2'], 'my_event', 'hello world');
$promise->then(function($result) {
// do something with $result
return $result;
});
$final_result = $promise->wait();
$array['name'] = 'joe';
$array['message_count'] = 23;
$pusher->trigger('my_channel', 'my_event', $array);
$pusher->trigger('my-channel', 'event', 'data', ['socket_id' => $socket_id]);
$batch = [];
$batch[] = ['channel' => 'my-channel', 'name' => 'my_event', 'data' => ['hello' => 'world'], ['socket_id' => $socket_id]];
$batch[] = ['channel' => 'my-channel', 'name' => 'my_event', 'data' => ['myname' => 'bob'], ['socket_id' => $socket_id]];
$pusher->triggerBatch($batch);
$result = $pusher->trigger('my-channel', 'my_event', 'hello world', ['info' => 'subscription_count']);
$subscription_count = $result->channels['my-channel']->subscription_count;
$batch = [];
$batch[] = ['channel' => 'my-channel', 'name' => 'my_event', 'data' => ['hello' => 'world'], 'info' => 'subscription_count'];
$batch[] = ['channel' => 'presence-my-channel', 'name' => 'my_event', 'data' => ['myname' => 'bob'], 'info' => 'user_count,subscription_count'];
$result = $pusher->triggerBatch($batch);
foreach ($result->batch as $i => $attributes) {
echo "channel: {$batch[$i]['channel']}, name: {$batch[$i]['name']}";
if (isset($attributes->subscription_count)) {
echo ", subscription_count: {$attributes->subscription_count}";
}
if (isset($attributes->user_count)) {
echo ", user_count: {$attributes->user_count}";
}
echo PHP_EOL;
}
$pusher->trigger('my-channel', 'event', 'data', [], true);
$pusher->authenticateUser('socket_id', 'user-id');
$pusher->authorizeChannel('private-my-channel','socket_id');
$pusher->authorizePresenceChannel('presence-my-channel','socket_id', 'user_id', 'user_info');
$webhook = $pusher->webhook($request_headers, $request_body);
$number_of_events = count($webhook->get_events());
$time_received = $webhook->get_time_ms();
$pusher = new Pusher\Pusher(
$app_key,
$app_secret,
$app_id,
[
'cluster' => $app_cluster,
'encryption_master_key_base64' => "<your base64 encoded master key>"
]
);
$data['name'] = 'joe';
$data['message_count'] = 23;
$pusher->trigger(['channel-1', 'private-encrypted-channel-2'], 'test_event', $data);
header('Content-Type: application/json');
if (isset($_SESSION['user_id'])) {
$stmt = $pdo->prepare("SELECT * FROM `users` WHERE id = :id");
$stmt->bindValue(':id', $_SESSION['user_id'], PDO::PARAM_INT);
$stmt->execute();
$user = $stmt->fetch();
} else {
die(json_encode('no-one is logged in'));
}
$pusher = new Pusher\Pusher($key, $secret, $app_id);
$presence_data = ['name' => $user['name']];
echo $pusher->authorizePresenceChannel($_POST['channel_name'], $_POST['socket_id'], $user['id'], $presence_data);
$pusher->getChannelInfo($name);
$info = $pusher->getChannelInfo('channel-name');
$channel_occupied = $info->occupied;
$info = $pusher->getChannelInfo('presence-channel-name', ['info' => 'user_count']);
$user_count = $info->user_count;
$info = $pusher->getChannelInfo('presence-channel-name', ['info' => 'subscription_count']);
$subscription_count = $info->subscription_count;
$pusher->getChannels();
$result = $pusher->getChannels();
$channel_count = count($result->channels); // $channels is an Array
$pusher->getChannels(['filter_by_prefix' => 'some_filter']);
$results = $pusher->getChannels(['filter_by_prefix' => 'presence-']);
$channel_count = count($result->channels); // $channels is an Array
$pusher->get('/channels', ['filter_by_prefix' => 'presence-']);
$subscription_counts = [];
foreach ($pusher->getChannels()->channels as $channel => $v) {
$subscription_counts[$channel] =
$pusher->getChannelInfo(
$channel, ['info' => 'subscription_count']
)->subscription_count;
}
var_dump($subscription_counts);
$results = $pusher->getPresenceUsers('presence-channel-name');
$users_count = count($results->users); // $users is an Array
$response = $pusher->get('/channels/presence-channel-name/users');
Array (
[body] => {"users":[{"id":"a_user_id"}]}
[status] => 200
[result] => Array (
[users] => Array (
[0] => Array (
[id] => a_user_id
),
/* Additional users */
)
)
)
$pusher->get($path, $params);
$response = $pusher->get('/channels');
$http_status_code = $response['status'];
$result = $response['result'];
bash
$ composer
json
""pusher/pusher-php-server": "^7.2"
}
js
var pusher = new Pusher("app_key",
// ...
channelAuthorization: {
endpoint: "/presenceAuth.php",
},
);