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.
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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With