Ratchet PHP WAMP - React / ZeroMQ - Specific user broadcast

后端 未结 2 1589
粉色の甜心
粉色の甜心 2021-02-02 13:54

Note: This is not the same as this question which utilises MessageComponentInterface. I am using WampServerInterface<

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-02 14:06

    Note: My answer here does not include references to ZeroMQ, as I am not using it any more. However, I'm sure you will be able to figure out how to use ZeroMQ with this answer if you need to.

    Use JSON

    First and foremost, the Websocket RFC and WAMP Spec state that the topic to subscribe to must be a string. I'm cheating a little here, but I'm still adhering to the spec: I'm passing JSON through instead.

    {
        "topic": "subject here",
        "userId": "1",
        "token": "dsah9273bui3f92h3r83f82h3"
    }
    

    JSON is still a string, but it allows me to pass through more data in place of the "topic", and it's simple for PHP to do a json_decode() on the other end. Of course, you should validate that you actually receive JSON, but that's up to your implementation.

    So what am I passing through here, and why?

    • Topic

    The topic is the subject the user is subscribing to. You use this to decide what data you pass back to the user.

    • UserId

    Obviously the ID of the user. You must verify that this user exists and is allowed to subscribe, using the next part:

    • Token

    This should be a one use randomly generated token, generated in your PHP, and passed to a JavaScript variable. When I say "one use", I mean every time you reload the page (and, by extension, on every HTTP request), your JavaScript variable should have a new token in there. This token should be stored in the database against the User's ID.

    Then, once a websocket request is made, you match the token and user id to those in the database to make sure the user is indeed who they say they are, and they haven't been messing around with the JS variables.

    Note: In your event handler, you can use $conn->remoteAddress to get the IP of the connection, so if someone is trying to connect maliciously, you can block them (log them or something).

    Why does this work?

    It works because every time a new connection comes through, the unique token ensures that no user will have access to anyone else's subscription data.

    The Server

    Here's what I am using for running the loop and event handler. I am creating the loop, doing all the decorator style object creation, and passing in my EventHandler (which I'll come to soon) with the loop in there too.

    $loop = Factory::create();
    
    new IoServer(
        new WsServer(
            new WampServer(
                new EventHandler($loop) // This is my class. Pass in the loop!
            )
        ),
        $webSock
    );
    
    $loop->run();
    

    The Event Handler

    class EventHandler implements WampServerInterface, MessageComponentInterface
    {
        /**
         * @var \React\EventLoop\LoopInterface
         */
        private $loop;
    
        /**
         * @var array List of connected clients
         */
        private $clients;
    
        /**
         * Pass in the react event loop here
         */
        public function __construct(LoopInterface $loop)
        {
            $this->loop = $loop;
        }
    
        /**
         * A user connects, we store the connection by the unique resource id
         */
        public function onOpen(ConnectionInterface $conn)
        {
            $this->clients[$conn->resourceId]['conn'] = $conn;
        }
    
        /**
         * A user subscribes. The JSON is in $subscription->getId()
         */
        public function onSubscribe(ConnectionInterface $conn, $subscription)
        {
            // This is the JSON passed in from your JavaScript
            // Obviously you need to validate it's JSON and expected data etc...
            $data = json_decode(subscription->getId());
            
            // Validate the users id and token together against the db values
            
            // Now, let's subscribe this user only
            // 5 = the interval, in seconds
            $timer = $this->loop->addPeriodicTimer(5, function() use ($subscription) {
                $data = "whatever data you want to broadcast";
                return $subscription->broadcast(json_encode($data));
            });
    
            // Store the timer against that user's connection resource Id
            $this->clients[$conn->resourceId]['timer'] = $timer;
        }
    
        public function onClose(ConnectionInterface $conn)
        {
            // There might be a connection without a timer
            // So make sure there is one before trying to cancel it!
            if (isset($this->clients[$conn->resourceId]['timer']))
            {
                if ($this->clients[$conn->resourceId]['timer'] instanceof TimerInterface)
                {
                    $this->loop->cancelTimer($this->clients[$conn->resourceId]['timer']);
                }
            }
        
            unset($this->clients[$conn->resourceId]);
        }
    
        /** Implement all the extra methods the interfaces say that you must use **/
    }
    

    That's basically it. The main points here are:

    • Unique token, userid and connection id provide the unique combination required to ensure that one user can't see another user's data.
    • Unique token means that if the same user opens another page and requests to subscribe, they'll have their own connection id + token combo so the same user won't have double the subscriptions on the same page (basically, each connection has it's own individual data).

    Extension

    You should be ensuring all data is validated and not a hack attempt before you do anything with it. Log all connection attempts using something like Monolog, and set up e-mail forwarding if any critical's occur (like the server stops working because someone is being a bastard and attempting to hack your server).

    Closing Points

    • Validate Everything. I can't stress this enough. Your unique token that changes on every request is important.
    • Remember, if you re-generate the token on every HTTP request, and you make a POST request before attempting to connect via websockets, you'll have to pass back the re-generated token to your JavaScript before trying to connect (otherwise your token will be invalid).
    • Log everything. Keep a record of everyone that connects, asks for what topic, and disconnects. Monolog is great for this.

提交回复
热议问题