Skip to content

Publish to Just One User

David Dan edited this page Feb 14, 2015 · 1 revision

Create an internal client that stores the user/session info in memory:

<?php

$sessions = [];

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

    $session->subscribe('wamp.metaevent.session.on_join', function($args) use ($sessions){
                $sessions[$args[0]['authid']][] = $args->session;
    });

    $session->subscribe('wamp.metaevent.session.on_leave', function($args) use ($sessions){
        //remove the session
        if (!isset($sessions[$args[0]['authid']])){
            return;
        }
        
        foreach ($sessions[$args[0]['authid']] as $k => $session) {
            if ($session === $args->session){
                unset($sessions[$args[0]['authid']][$k]);
            }
        }
        $sessions[$args[0]['authid']] = $args->session;
    });

    $session->register('com.example.get_user_sessions', function($args) use ($sessions){
        return $sessions[$args[0]];
    });
    
});

Then from the client that you want to publish to just one user:

// Publish to all sessions associated with one authid
$session->call('com.example.get_user_sessions', [$authid])->then(
    function ($res) use ($session) {

        $session->publish('com.example.test_publish', array('Testing'), [], ['eligible' => $res[0]]);

    }
);
Clone this wiki locally