phpwebsocketratchetthruwayautobahnjs

How to parse message on server side with Thruway


I want to use websocket for a voting application. There is two parts: one is a chat channel and an other one is a command channel where information about the question, the available answers, the countdown etc will travel.

On the client side, I use Autobahn|JS as library. I can subscribe to my two channels easily (I can see it on server logs)

On the server side, I use Thruway because it's WAMP v2 compliant.

My problem: I don't know how I can hook the messages on the server side to broadcast them on each topics. Seems easy but I can find any solution.

I guess it's by using InternalClient, but I am not really sure…

Some code:

JS Client

var ws;

var connection = new autobahn.Connection({
    url: 'ws://dev.test.com:8080/',
    realm: 'com.test.dev'
});

connection.onopen = function (session) {
    ws = session;

    function onevent(args) {
        console.log("Event:", args[0]);
    }

    session.subscribe('chat.test', onevent);
    session.subscribe('cmd.test', onevent);
};

connection.open();

Just after, I add a listener on the chatbox:

$('#message').on('keydown', function(event) {
    var keycode = event.keyCode || event.which;

    if(keycode == '13') {
        event.preventDefault();

        ws.publish('chat.test', [{token: token, message: $('#message').val()}]);
        $('#message').val('');
    }
});

PHP Server

<?php

require 'vendor/autoload.php';

use Thruway\Peer\Router;
use Thruway\Transport\RatchetTransportProvider;

class TestInternalClient extends Thruway\Peer\Client {

    public function __construct() {
        parent::__construct('com.test.dev');
    }

    public function onSessionStart($session, $transport) {
        echo "--------------- Hello from InternalClient ------------\n";

        $session->subscribe('chat.test', [$this, 'chat']);
    }

    public function chat($args, $kwArgs, $options) {
        // Decode chat message here? How to broadcast them?
    }

}

$router = new Router();
$router->addTransportProvider(new RatchetTransportProvider("0.0.0.0", 8080));
$router->addInternalClient(new TestInternalClient());
$router->start();

Server logs

test@test:~/dev/wp-content/websockets$ php server.php 
2017-01-03T17:48:19.4812750 notice     Changing PHP precision from 14 to 16
2017-01-03T17:48:19.4831340 debug      [Thruway\Peer\Router 21425] New router created
2017-01-03T17:48:19.4838930 info       [TestInternalClient 21425] New client created
2017-01-03T17:48:19.4842930 info       [Thruway\Peer\Router 21425] Starting router
2017-01-03T17:48:19.4874870 info       [Thruway\Transport\RatchetTransportProvider 21425] Websocket listening on 0.0.0.0:8080
2017-01-03T17:48:19.4911550 info       [Thruway\RealmManager 21425] Got prehello...
2017-01-03T17:48:19.4912050 debug      [Thruway\RealmManager 21425] Creating new realm "com.test.dev"
2017-01-03T17:48:19.4928900 debug      [Thruway\RealmManager 21425] Adding realm "com.test.dev"
2017-01-03T17:48:19.4943690 debug      [TestInternalClient 21425] Client onMessage: [Thruway\Message\WelcomeMessage]
2017-01-03T17:48:19.4944080 info       [TestInternalClient 21425] We have been welcomed...
--------------- Hello from InternalClient ------------
2017-01-03T17:48:19.4955960 debug      [Thruway\Subscription\SubscriptionGroup 21425] Added subscription to "exact":"chat.test"
2017-01-03T17:48:19.4958000 debug      [TestInternalClient 21425] Client onMessage: [Thruway\Message\SubscribedMessage]
2017-01-03T17:48:19.4965720 info       [Thruway\Peer\Router 21425] Starting loop
2017-01-03T17:48:25.8358310 debug      [Thruway\Transport\RatchetTransportProvider 21425] RatchetTransportProvider::onOpen
2017-01-03T17:48:25.9890380 debug      [Thruway\Transport\RatchetTransportProvider 21425] onMessage: ([1,"com.test.dev",{"roles":{"caller":{"features":{"caller_identification":true,"progressive_call_results":true}},"callee":{"features":{"caller_identification":true,"pattern_based_registration":true,"shared_registration":true,"progressive_call_results":true,"registration_revocation":true}},"publisher":{"features":{"publisher_identification":true,"subscriber_blackwhite_listing":true,"publisher_exclusion":true}},"subscriber":{"features":{"publisher_identification":true,"pattern_based_subscription":true,"subscription_revocation":true}}}}])
2017-01-03T17:48:25.9895580 info       [Thruway\RealmManager 21425] Got prehello...
2017-01-03T17:48:26.0515670 debug      [Thruway\Transport\RatchetTransportProvider 21425] onMessage: ([32,2358366231642639,{},"chat.test"])
2017-01-03T17:48:26.0517410 debug      [Thruway\Subscription\SubscriptionGroup 21425] Added subscription to "exact":"chat.test"
2017-01-03T17:48:26.0519070 debug      [Thruway\Transport\RatchetTransportProvider 21425] onMessage: ([32,8132914407728460,{},"cmd.test"])
2017-01-03T17:48:26.0520030 debug      [Thruway\Subscription\SubscriptionGroup 21425] Added subscription to "exact":"cmd.test"
2017-01-03T17:48:34.0115900 debug      [Thruway\Transport\RatchetTransportProvider 21425] onMessage: ([16,3263467437225103,{},"chat.test",[{"token":"6b28af4056d650480e726e33986a2e790e34abb1","message":"Write from my chatbox"}]])
2017-01-03T17:48:34.0121500 debug      [TestInternalClient 21425] Client onMessage: [Thruway\Message\EventMessage]

If you have any idea… Thanks! :)


Solution

  • class TestInternalClient extends Thruway\Peer\Client {
    
        public function __construct() {
            parent::__construct('com.test.dev');
        }
    
        public function onSessionStart($session, $transport) {
            echo "--------------- Hello from InternalClient ------------\n";
    
            $session->subscribe('chat.test', function ($args, $kwArgs, $options) use ($session) {
                // Get message contents
                $token = $args[0]->token;
                $message = $args[0]->message;
    
                // publish to other people
                $session->publish('some.topic', [[ 'message' => $message ]]);
            });
    
            //////////////////////////////////////////
            // Subscribe to everything in the system
            $session->subscribe('', function ($args, $argsKw, $details, $publicationId) {
                $value = isset($args[0]) ? $args[0] : '';
                echo 'Received ' . json_encode($value) . ' on topic ' . $details->topic . PHP_EOL;
            }, [ 'match' => 'prefix' ]);
        }
    }