tweet9ra/logux-processor

5.0.3 2020-06-21 22:15 UTC

README

This package allows to use Logux server as proxy between logux server and your php app.

Laravel adapter for this package

Version compatibility with logux backend protocol:

Package version Logux backend protocol version
< 4.0.0 2
^4.0.0 3
^5.0.0 4

Quick start

composer require tweet9ra/logux-processor

Initialization

  • Load config
/**
* 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');
  • Set routes
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'
]);

All your callbacks takes \tweet9ra\Logux\ProcessableAction as first argument, but it can be replaced with child class:

/**
 * @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");
    }
]);

Process request from logux proxy

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

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

echo json_encode($response);