on

The on method sets up a message handler for a specific topic. It works just like subscribe in PUB/SUB with the difference that there is no communication with the server.

The basic sample will use the on method to bind a method for messages with topic 'foo' on the generic controller. The invoke call at the end is just to actually trigger a message so round trip to the server and come back. If you open several clients they will all get the message.

<script>
    var conn = new xsockets.client('ws://localhost:4502', ['generic']);        
    var generic = conn.controller('generic');
    generic.onOpen = function () {
        generic.on('foo', function(data) {
            console.log('Foo topic handled with data:', data);
        });

        //send a message just to trigger the message handler above
        generic.invoke('foo', { say: 'hello world', from: 'sample' });
    };
    conn.open();
</script>

results matching ""

    No results matching ""