Nate
Nate

Reputation: 28424

Client not receiving published events using demo code?

I'm trying out Thruway and am having trouble getting the demo code to work.

Javascript code:

<script src="https://autobahn.s3.amazonaws.com/autobahnjs/latest/autobahn.js"></script>
<script>
//    var autobahn = require('autobahn');

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

    connection.onopen = function (session) {

        // 1) subscribe to a topic
        function onevent(args) {
            console.log("Event:", args[0]);
        }
        session.subscribe('com.myapp.hello', onevent);

        // 2) publish an event
        session.publish('com.myapp.hello', ['Hello, world!']);
    };

    connection.open();
</script>

I have SimpleWsServer.php running:

<?php

require 'bootstrap.php';

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

$router = new Router();

$transportProvider = new RatchetTransportProvider("127.0.0.1", 9090);

$router->addTransportProvider($transportProvider);

$router->start();

I have SimpleClient.php running (I removed the RPC code since I only want to push messages from server to clients):

<?php

require 'bootstrap.php';

use Thruway\ClientSession;
use Thruway\Connection;

$onClose = function ($msg) {
    echo $msg;
};

$connection = new Connection(
    [
        "realm"   => 'realm1',
        "onClose" => $onClose,
        "url"     => 'ws://127.0.0.1:9090',
    ]
);

$connection->on(
    'open',
    function (ClientSession $session) {

        // 1) subscribe to a topic
        $onevent = function ($args) {
            echo "Event {$args[0]}\n";
        };
        $session->subscribe('com.myapp.hello', $onevent);

        // 2) publish an event
        $session->publish('com.myapp.hello', ['Hello, world from PHP!!!'], [], ["acknowledge" => true])->then(
            function () {
                echo "Publish Acknowledged!\n";
            },
            function ($error) {
                // publish failed
                echo "Publish Error {$error}\n";
            }
        );

//        // 3) register a procedure for remoting
//        $add2 = function ($args) {
//            return $args[0] + $args[1];
//        };
//        $session->register('com.myapp.add2', $add2);
//
//        // 4) call a remote procedure
//        $session->call('com.myapp.add2', [2, 3])->then(
//            function ($res) {
//                echo "Result: {$res}\n";
//            },
//            function ($error) {
//                echo "Call Error: {$error}\n";
//            }
//        );
    }

);

$connection->open();

It looks to me like the demo code sends the message Hello, world from PHP!!! to the client after it subscribes, but I'm not seeing a message in my browser's console.

I know the client is connecting to the server because SimpleClient.php outputs the following to the terminal:

2015-03-02T19:47:24.5464800 debug      [Thruway\Transport\PawlTransportProvider 13800] Received: [36,1574620859,33562629,{},["Hello, world!"]]
2015-03-02T19:47:24.5470880 debug      [Thruway\Peer\Client 13800] Client onMessage: [Thruway\Message\EventMessage]
Event Hello, world!

Am I missing something, or should Hello, world from PHP!!! have been printed out in the browser console?

Upvotes: 2

Views: 664

Answers (1)

Nate
Nate

Reputation: 28424

I found a post in a ticket in the github repo saying

the publishing client will not receive the event message by default.

Opening another tab with the JS resulted in the message being sent to the previously opened tab.

Mystery solved!

Upvotes: 3

Related Questions