alabama
alabama

Reputation: 422

Best Practice for Multiple Subscribe Methods in React / Ratchet / ZMQ

I try to build a little realtime websocket use-case, where users can login and see all other users logged in, get notified when a new user signs in or an existing user logs out.

For this scenario i use the ZMQ PUSH Socket in my UserController when a user logs in or logs out.

UserConstroller

public function login() {

        //... here is the auth code, model call etc...

        $aUserData = array();// user data comes from the database with username, logintime, etc....

        $context = new \ZMQContext();
        $oSocket = $context->getSocket(\ZMQ::SOCKET_PUSH, 'USER_LOGIN_PUSHER'); // use persistent_id
        if($oSocket instanceof \ZMQSocket) {

            $oSocket->connect("tcp://127.0.0.1:5555"); //
            $oSocket->send(json_encode($aUserData));
        }
    }

    public function logout() {
        //... here is the logout code, model call etc ....

        $aUserData = array();// user data comes from the SESSION with username, logintime, etc....

        $context = new \ZMQContext();
        $oSocket = $context->getSocket(\ZMQ::SOCKET_PUSH, 'USER_LOGOUT_PUSHER'); // use persistent_id
        if($oSocket instanceof \ZMQSocket) {

            $oSocket->connect("tcp://127.0.0.1:5555"); //
            $oSocket->send(json_encode($aUserData));
        }
    }

Then i've got a Pusher class like in the Ratchet docs: link

In this class there are two methods: onUserLogin and onUserLogout and of course all the other stuff like

onSubscribe, onOpen, onPublish

UserInformationPusher

 public function onUserLogin($aUserData) {
        //var_dump("onUserLogin");
        $sUserData = json_decode($aUserData, true);

        $oTopic = $this->subscribedTopics["user_login"];

        if($oTopic instanceof Topic) {
            $oTopic->broadcast($sUserData);
        } else {
            return;
        }
    }

    public function onUserLogout($aUserData) {
        //var_dump("onUserLogout");
        $entryData = json_decode($aUserData, true);

        $oTopic = $this->subscribedTopics["user_logout"];

        if($oTopic instanceof Topic) {
            $oTopic->broadcast($entryData);
        } else {
            return;
        }
    }

The last piece is the WampServer/WsServer/HttpServer with a Loop that listens to the incoming connections. There is also my ZMQ PULL socket

RatchetServerConsole

public function start_server() {

        $oPusher = new UserInformationPusher();

        $oLoop = \React\EventLoop\Factory::create();
        $oZMQContext = new \React\ZMQ\Context($oLoop);
        $oPullSocket = $oZMQContext->getSocket(\ZMQ::SOCKET_PULL);

        $oPullSocket->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
        $oPullSocket->on('message', array($oPusher, 'onUserLogin'));
        $oPullSocket->on('message', array($oPusher, 'onUserLogout'));


        $oMemcache = new \Memcache();
        $oMemcache->connect('127.0.0.1', 11211);
        $oMemcacheHandler = new Handler\MemcacheSessionHandler($oMemcache);

        $oSession = new SessionProvider(
            new \Ratchet\Wamp\WampServer(
                $oPusher
            ),
            $oMemcacheHandler
        );

        //$this->Output->info("Server start initiation with memcache!...");
        $webSock = new \React\Socket\Server($oLoop);
        $webSock->listen(8080, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect
        $oServer = new \Ratchet\Server\IoServer(
            new \Ratchet\Http\HttpServer(
                new \Ratchet\WebSocket\WsServer(
                    $oSession
                )
            ),
            $webSock
        );
        $this->Output->info("Server started ");
        $oLoop->run();

    }

In this example, the call from login() or logout() would always call both methods(onUserLogin and onUserLogout). I was not able to find some docs, which describe what events i can use in the on($event, callable $listener) method, does anyone have a link/knowledge base? What is the best approach to check which method from the UserController was fired?

No Client code needed cause it works fine

Upvotes: 3

Views: 2251

Answers (2)

asankasri
asankasri

Reputation: 486

In your RatchetServerConsole,

Remove,

$oPullSocket->on('message', array($oPusher, 'onUserLogin'));
$oPullSocket->on('message', array($oPusher, 'onUserLogout'));

Add,

$oPullSocket->on('message', array($oPusher, 'onUserActionBroadcast'));

.

In your UserInformationPusher,

Remove onUserLogin() and onUserLogout().

Add,

public function onUserActionBroadcast($aUserData)
{
    $entryData = json_decode($aUserData, true);

    // If the lookup topic object isn't set there is no one to publish to
    if (!array_key_exists($entryData['topic'], $this->subscribedTopics)) {
        return;
    }

    $topic = $this->subscribedTopics[$entryData['topic']];

    unset($entryData['topic']);

    // re-send the data to all the clients subscribed to that category
    $topic->broadcast($entryData);
}

.

Your UserConstroller (add the topic in $aUserData),

public function login() {

    //... here is the auth code, model call etc...

    $aUserData = array();// user data comes from the database with username, logintime, etc....

    $aUserData['topic'] = 'USER_LOGIN'; // add the topic name

    $context = new \ZMQContext();
    $oSocket = $context->getSocket(\ZMQ::SOCKET_PUSH, 'my pusher'); // use persistent_id
    if($oSocket instanceof \ZMQSocket) {

        $oSocket->connect("tcp://127.0.0.1:5555"); //
        $oSocket->send(json_encode($aUserData));
    }
}

public function logout() {
    //... here is the logout code, model call etc ....

    $aUserData = array();// user data comes from the SESSION with username, logintime, etc....

    $aUserData['topic'] = 'USER_LOGOUT'; // add the topic name

    $context = new \ZMQContext();
    $oSocket = $context->getSocket(\ZMQ::SOCKET_PUSH, 'my pusher'); // use persistent_id
    if($oSocket instanceof \ZMQSocket) {

        $oSocket->connect("tcp://127.0.0.1:5555"); //
        $oSocket->send(json_encode($aUserData));
    }
}

.

Finally in your view file,

<script>
var conn = new ab.Session('ws://yourdomain.dev:9000', // Add the correct domain and port here
    function() {
        conn.subscribe('USER_LOGIN', function(topic, data) {                
            console.log(topic);
            console.log(data);
        });

       conn.subscribe('USER_LOGOUT', function(topic, data) {                
            console.log(topic);
            console.log(data);
        });
    },
    function() {
        console.warn('WebSocket connection closed');
    },
    {'skipSubprotocolCheck': true}
);
</script>

.

NOTE: The basic idea was to use a single broadcast function in the pusher class.

Upvotes: 4

alabama
alabama

Reputation: 422

After one month of intensive handling with PHPs best practice in websockets i changed from my approach to the Crossbar.io, voryx/Thruway in the PHP Backend and Autobahn|JS in the Frontend. All of these componentes support the WAMP V2 Websocket Standard and are able to handle my requirements.

If there are some requests i can post the solution to my problem above, with the usage of the mentioned components.

Upvotes: 3

Related Questions