Ratchet / Websockets : How many clients subscribing to an object?

2.1k views Asked by At

I would like to know how many clients are actually subscribing to a chatroom / conversation.

To be more precise, i just want to know if there is more than 1 client. (Chatroom are actually a private conversation between two users).

There is only one chatroom / private conversation at a time (per user).

class Chat implements WampServerInterface
{
    protected $conversationId;
    public function __construct(){
        $this->conversationId = null;
    }
    public function onSubscribe(ConnectionInterface $conn, $conversation_id){
        $this->conversationId = $conversation_id;
        echo "Client $conn->resourceId assigned to the conversation : $conversation_id\n";
    }
    public function onPublish(ConnectionInterface $conn, $conversation_id, $event, array $exclude, array $eligible){
        // How to get $nb_clients ?
        echo "$nb_clients User(s) in conversation";
        echo "Message sent to $conversation_id : $event";
        // ...
        $message = $event; 
        // Send data to conversation
        $this->conversationId->broadcast($message);
    }
}

So in the given code, how to get $nb_clients?


Update:

I guess I'm starting to see a solution.

Here my second attempt:

class Chat implements WampServerInterface
{
    protected $conversation  = array();
    public function onSubscribe(ConnectionInterface $conn, $conversation_id){
        $conversation_id = (string) $conversation_id;
        if(!array_key_exists($conversation_id, $this->conversation)){
            $this->conversation[$conversation_id] = 1;
        }
        else{
            $this->conversation[$conversation_id]++;
        }
        echo "{$this->conversation[$conversation_id]}\n";
        echo "Client $conn->resourceId assigned to the conversation : {$conversation_id}\n";
    }
    public function onUnSubscribe(ConnectionInterface $conn, $conversation_id){
        // Foreach conversations or given conversation remove one client
        $this->conversation[$conversation_id]--;
        echo "$this->conversation[$conversation_id]\n";
        echo "Client $conn->resourceId left the conversation : $conversation_id\n";
    }
    public function onOpen(ConnectionInterface $conn){
        echo "New connection! ({$conn->resourceId})\n";
    }
    public function onClose(ConnectionInterface $conn){
        $this->onUnsubscribe($conn, $this->conversation);
        echo "Connection closed!\n";
    }
    public function onCall(ConnectionInterface $conn, $id, $fn, array $params){
    }
    public function onPublish(ConnectionInterface $conn, $conversation_id, $event, array $exclude, array $eligible){
        $conversation_id = (string) $conversation_id;
        $nb_clients = $this->conversation[$conversation_id]
        echo "$nb_clients User(s) in conversation";
        echo "Message sent to $conversation_id : $event";
        // ...
        $message = $event; 
        // Send data to conversation
        $this->conversation[$conversation_id]->broadcast($message);
    }
    public function onError(ConnectionInterface $conn, \Exception $e){
        echo "An error has occurred: {$e->getMessage()}\n";
        $conn->close();
    }
}

Any ideas if that would properly work? It actually seems to work but i'm still not sure if it is the best solution. I actually got inspired from Ratchet github.

1

There are 1 answers

0
VolenD On BEST ANSWER

The second argument of onPublish is a Topic object (Interface WampServerInterface):

onPublish( Ratchet\ConnectionInterface $conn, string|Ratchet\Wamp\Topic $topic, string $event, array $exclude, array $eligible )

so according to Ratchet's documentation, you can use the count() method on the topic to get the subscribers:

$nb_clients = $conversation_id->count();