PHP code example of socketbus / socketbus-php

1. Go to this page and download the library: Download socketbus/socketbus-php 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/ */

    

socketbus / socketbus-php example snippets




use SocketBus\SocketBus;

$config = [
    'app_id' => 's-1-J2PCu8g8sAejZeXx',
    'secret' => 'cdKBpcruwYQ96kvIaYiorbTFxRDCbVfj'
];

$socketBus = new SocketBus($config);

$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);
bash
    composer