The SocketBus PHP Library
- Create an account -
- Minimum requirements -
- Install the library -
composer require socketbus/socketbus-php
- Using the library -
require 'vendor/autoload.php';
use SocketBus\SocketBus;
$config = [
'app_id' => 's-1-J2PCu8g8sAejZeXx',
'secret' => 'cdKBpcruwYQ96kvIaYiorbTFxRDCbVfj'
];
$socketBus = new SocketBus($config);
To ensure that your data is secure, you can enable End-to-end encryption under Application > Settings. This setting in combination with the option custom_encryption_key
encrypts the payload and decrypts in the client when an authenticated user receives a encrypted payload.
$config = [
'app_id' => 's-1-J2PCu8g8sAejZeXx',
'secret' => 'cdKBpcruwYQ96kvIaYiorbTFxRDCbVfj',
'custom_encryption_key' => 'my-unique-key'
];
$channelName = $request->channel_name;
if ($socketBus->isChannelPrivate($channelName)) {
// The channel is private
}
if ($socketBus->isChannelPresence($channelName)) {
// The channel is presence
}
if ($socketBus->isChannelPublic($channelName)) {
// The channel is public
}
$socketId = $request->socket_id;
$channelName = $request->channel_name;
if (/** verifies if user can access the request channel */) {
// returns the token to the client
return [
'auth' => $socketbus->auth($socketId, $channelName)
];
}
$socketId = $request->socket_id;
$channelName = $request->channel_name;
$userId = /** gets the current user id */;
if (/** verifies if user can access the request channel */) {
// returns the data
return $socketbus->authPresence($socketId, $channelName, $userId, true);
}
$payload = [
'type' => 'cupcake',
'flavor' => 'sweet'
];
$sockebus->broadcast('food-observer', 'new-food', $payload);
API Docs - Check-out the full documentation
SocketBus Laravel Library - Laravel library