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

1

6

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.

Cytotaxonomy answered 18/6, 2015 at 13:39 Comment(9)
The given code is very sparse...Deduction
Well i can add more, this is not my entire code but i tried to keep it simple here to not bring any confusion. All the logic part to save in database as been removed from that snippet. This code is actually working. I'm not sure the front end code would help.Cytotaxonomy
Well this code is unusable for your question. Where is your thread pool? where do clients get registered and get their own object instantiated? where is the hashmap/arraylist/linkedqueue where you keep track?Deduction
Oh wait. I was confusing it with Java. Sorry. If I were you would not use php for websockets. I'd go with a more optimised language for websockets then php.Deduction
This is unfortunately not something i wish to do. I'd like to keep going with that. I'm pretty sure there might be a way to do it.Cytotaxonomy
there is a way... but you are choosing to trod over a dirt path and forgoing the luxery of a highwayDeduction
Every programming language has pro and cons. I'll choose the dirt path then ;).Cytotaxonomy
Have you tried with $conversation_id->count() (before casting it to string) - as described in the source github.com/ratchetphp/Ratchet/blob/master/src/Ratchet/Wamp/…Valval
Well that seems to work. I though this would return the total of clients despite their assigned conversation. I gotta try it in real situation but i don't have the proper server yet. I suggest you to submit it as an answer ;).Cytotaxonomy
V
5

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();
Valval answered 26/6, 2015 at 8:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.