PHP code example of tweet9ra / logux-processor

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

    

tweet9ra / logux-processor example snippets


/**
* password - logux controll password, that you specify in logux proxy
* control_url - logux proxy http endpoint (usually http://localhost:31338)
* protocol_version - version of protocol, default 2
*/
$app = new tweet9ra\Logux\App('password', 'control_url', 'protocol_version');

use \tweet9ra\Logux\ProcessableAction;

$app->setActionsMap([
    /**
    * If your logux proxy does not authenticate itself, you must specify this action
    */
    'auth' => function (array $loguxAuthCommand): bool {
        $userId = $loguxAuthCommand['userId'];
        $token = $loguxAuthCommand['token'];

        /* Anonymous login */
        if ($userId === 'false') {
            return true;
        }

        return function_that_validates_token($userId, $token);
    },

    /**
    * This action handle subscriptions to channels
    * You can use callback or match with assoative array
    */
    'logux/subscribe' => function (ProcessableAction $action) {},
    'logux/subscribe' => [
        'chats/:chatId' => function (ProcessableAction $action, $chatId) {},
        'users/:userId/alert/:alertType' => function (ProcessableAction $action, $userId, $alertType) {}
    ],

    // Your app actions
    'ADD_CHAT_MESSAGE' => function (ProcessableAction $action) {
        if (!user_can_add_messages($action->userId())) {
            // If you have an error while processing action you can use error method
            $action->error('You are not allowed to sent messages!');
            return;
        }

        /* Your logic */
    },

    // Or instead of closures you can specify callback method
    'ADD_CHAT_MESSAGE' => 'App\Controllers\ChatController@addMessage',
    'auth' => 'App\Controllers\AuthController@loguxAuth'
]);

/**
 * @property int $text Message text content
 * @property int $chatId Chat room id
 *
 * There is no useful features besides IDE autocompletion atm
*/
class AddChatMessage extends \tweet9ra\Logux\ProcessableAction {}

$app->setActionsMap([
    'ADD_CHAT_MESSAGE' => function (AddChatMessage $action) {
        create_chat_message($action->userId(), $action->text, $action->chatId);
        // Resend action to channels, users, clients or nodes
        $action->sendTo('channels', "chats/$action->chatId");
    }
]);

$input = file_get_contents('php://input');
$inputDecoded = json_decode($input, true);

$responseContent = $app->processRequest($inputDecoded);

echo json_encode($response);