Creating/Managing private rooms using Ratchet?
Asked Answered
C

2

6

I am making iOS chat application. After doing study on needed technology and protocols, I decided to give a websockets try. For reasons our stack top is php based and I came to know about ratchet as websockets for PHP. I made simple chat server for ios front-end from reading documentation. Chat is working very fine and I am comfortable with it too. I wanted to know on how to create separate private chat rooms. Will different instance of socket event loop needed to run for separate rooms ?

The sample server I made is using single event loop for managing user connections and dispatching messages to different connection/user id's. I really tried to look out for creating private chat rooms but haven't found any information where I could be confident.

Will I have to manage each connection/user id's virtually on this event loop, like deciding which users can chat to each other directly by controlling the dispatching of messages ? Or is their really a separate way to do this ? This is the event loop sample as per documentation that I implemented:

   <?php
   use Ratchet\Server\IoServer;
   use Ratchet\Http\HttpServer;
   use Ratchet\WebSocket\WsServer;
   use MyApp\Chat;
   
       require dirname(__DIR__) . '/vendor/autoload.php';
   
       $server = IoServer::factory(
           new HttpServer(
               new WsServer(
                   new Chat()
               )
           ),
           8080
       );
   
       $server->run();

I want to tell that I am a iOS/Android developer and really no expert or have fluent knowledge on web frontend/backend technologies.

Corinnecorinth answered 26/12, 2014 at 11:48 Comment(0)
P
1

Will different instance of socket event loop needed to run for separate rooms ?

No. Only one loop is needed. Your snippet is fine. What you have to do is to adjust Chat class so that it accepts additional parameter from the user input - room id/name.

For example, a user sends the message {"cmd":"msg", "message":"Hi", "room": 1}, then Chat should send the message only to the users who joined that room. And, of course, you should implement other user methods, such as {"cmd":"join", "room": 1}, {"cmd":"leave", "room": 1}

Period answered 5/2, 2015 at 3:55 Comment(3)
i had considered something like this for an app im building, but my issue now is how do you stop someone altering the javascript on the page to send a message to a chat room that they are not currently in?Kight
@DanHastings Maybe you can create a hash token that is unique to the room so that when you send the "room", then the token must match. Or you can just send the token by itself as the room id.Entremets
keep a room id on the server when you join a room and check against that onMessage. if(client room id not same as server room id) { get out }Pteropod
S
0

Well i might be a little bit late to answer, but here how i did that.

You should implement WampServerInterface instead of MessageComponentInterface on your Chat class (If you are not already doing so).

As said above your snippet is fine.

Here my Chat class :

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){

        echo "Message sent to $conversation_id : $event";

        // ... save in Database or else

        // Send data to conversation
        $this->conversationId->broadcast($message);

    }

}

This is for a connection to one room only.

However if you want to have multiple chatrooms running in same time, you should have a look to Ratchet code on Github.

I don't know what did you use for the frontend, i personally use autobahn.js to instantiate the connection with the server (using ratchet).

Savadove answered 18/6, 2015 at 14:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.