voryx / ThruwayBundle

Bundle for building Real-time Apps in Symfony
98 stars 47 forks source link

unable to publish to client from controller #67

Closed momopaper closed 6 years ago

momopaper commented 7 years ago

im trying to publish message to connecting client from server but client was unable to receive any msg from server, i try to use xdebug to debug the code, somehow ClientManager's publish function cant find wamp_kernel in the container and thus skip $session.publish(...) code, i not sure if i missed out any configuration

this is how i publish my msg from server controller

public function testingAction() {
        $client = $this->container->get('thruway.client');
        $client->publish("com.myapp.hello_pub_sub", ['aaa']);
}

this is my browser code

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <script type="text/javascript" src="js/autobahn.min.js"></script>
</head>
<body>
<script>
    var user = "userid";
    var key = "12345";
    function onchallenge(session, method, extra) {
        console.log(method, extra);
        if (method === "wampcra") {
            var keyToUse = key;
            if (typeof extra.salt !== 'undefined') {
                keyToUse = autobahn.auth_cra.derive_key(key, extra.salt);
            }
            console.log("authenticating via '" + method + "' and challenge '" + extra.challenge + "'");
            return autobahn.auth_cra.sign(keyToUse, extra.challenge);
        } else {
            throw "don't know how to authenticate using '" + method + "'";
        }
    }
    var connection = new autobahn.Connection({
        url: 'ws://127.0.0.1:8082',
        realm: 'realm1',
        authmethods: ["wampcra"],
        authid: user,
        onchallenge: onchallenge
    });
    connection.onopen = function (session, details) {
        console.log("connected session with ID " + session.id);
        console.log("authenticated using method '" + details.authmethod + "' and provider '" + details.authprovider + "'");
        console.log("authenticated with authid '" + details.authid + "' and authrole '" + details.authrole + "'");
        /*session.call('com.example.add', [3, 5]).then(
                function (res) {
                    console.log("Call 'com.example.add' result:", res);
                },
                function (error) {
                    console.log("Call Error:", error.error);
                }
        );*/
        //subscribe to a topic
        function onevent(args) {
            console.log('Subscribe message:', args);
        }
        session.subscribe('com.myapp.hello_pub_sub', onevent).then(
                function (subscription) {
                    console.log("subscription", subscription);
                },
                function (error) {
                    console.log("subscription error:", error);
                }
        );

        session.publish('com.myapp.hello_pub_sub', ['Hello, world222!']);
    };

    connection.onclose = function () {
        console.log("disconnected", arguments);
    };
    connection.open();
</script>

</body>
</html>

my config:

voryx_thruway:
    user_provider: 'fos_user.user_provider.username'
    realm: 'realm1'
    url: 'ws://127.0.0.1:8082' 
    router:
        ip: '127.0.0.1'  
        port: '8082'  
        trusted_port: '8081' 
        authentication: true 
    locations:
        bundles: ["AppBundle"]
davidwdan commented 7 years ago

What version of symfony are you using?

momopaper commented 7 years ago

symfony 2.8

davidwdan commented 6 years ago

If you're subscribing to a topic that you're also publishing to, you'll need to set the exclude_me option to false.

   session.publish('com.myapp.hello_pub_sub', ['Hello, world222!'], [],['exclude_me' => false]);