Ratchet: Is there any way to create multiple websockets, and having access to all sessions?

Created on 4 Aug 2015  路  12Comments  路  Source: ratchetphp/Ratchet

Hi everyone!

Is there any way, to run 2 websockets at the same time, and a way to exchange clients between them? I mean for sending messages between those websockets.

question

Most helpful comment

You now have all the Connection objects in memory of one running application. You just need to bridge your 2 different app classes to share them. One way is to follow the architecture of Ratchet using decoration:

class WebSocketBridge implements MessageComponentInterface {
    public function onOpen(ConnectionInterface $conn) { }
    // etc
}

class WebSocketClients implements MessageComponentInterface {
    private $delegate;

    public function __construct(MessageComponentInterface $delegate) {
        $this->delegate = $delegate;
    }

    public function onOpen(ConnectionInterface $conn) {
        $conn->source = 'MainClients';
        $this->delegate->onOpen($conn);
    }
}

class WebSocketOthers { // etc, like WebSocketClients
}

    $pool = new WebSocketBridge;    
    $one = new WsServer(new WebSocketClients($pool));
    $two = new WsServer(new WebSocketOthers($pool));
    // etc

You then have one instance with all your connections pooled together and can route messages how ever you choose.

All 12 comments

Any ideas?

I have this implemented on my server, since I am using a cluster server each of my server is connected to each other all request coming to one server is automatically forwarded to the other server.

Well, could you describe it more clearly, because it doesn't make for me any sense. Do you have a multiple websockets running on different ports, and there is possibility to send a message from one websocket to another?

I didn't try such things, but can't you open a socket with React, and use that to communicate between the two WS server?

@NightShifter42 Do you mean 2 different servers clients can connect to or do you mean to load balance?

each of my servers have extra bindings on a port which they use to communicate with each other. then I use fsockopen to connect and send to another server.

I mean, I have one server. And two running websockets on different ports. So the clients of first socket, can interact (send messages) to clients of the second socket.

If I have an already running websocket. And I'm trying to get clients from let's say like test.php script, it returns null (even if clients are static).

I'm new to php, so before I have worked with C#. Could someone give me an idea, of how those clients can interact between two websockets?

PS. I really need help, because I'm no longer can go any further without this thing.

You can use ZMQ to communicate across webscoket server like the push integration tutorial.

In advanced way you can pipe the stream from ws1 to ws2 (Read WS1 -> Write WS2) via https://github.com/reactphp/stream/blob/master/src/Util.php#L9.

Just care about full duplex (WS1 -> WS2, WS2 -> WS1) you will need to check the origin of stream before copying it, or you will fall in infinite recopy :(

@NightShifter42 You can run both WebSockets servers from one script. Checkout this issue which runs multiple servers bound to different IP addresses, you can use similar code to host different Apps bound to different ports.

@cboden That makes sense! But I'm still don't know how can clients from different apps interact with each other? Maybe code tell you more.

<?php
class WebSocketClients{
protected $clients;

public function onOpen(ConnectionInterface $conn)
{
//some code
$conn->authenticated = false; //dynamic field
$clients->attach($conn);
//some code
}

public function onMessage(ConnectionInterface $from, $response)
{
//more code
$from->authenticated = true;
//more code;
}
}

And then, we have second class (app).

<?php
class WebSocketOthers{
protected $others;

//pretty same things.

//GET ALL  AUTHENTICATED CLIENTS FROM WebSocketClients
}

And how can I get all authenticated clients of WebSocketClients here, to send messages them from here???

You now have all the Connection objects in memory of one running application. You just need to bridge your 2 different app classes to share them. One way is to follow the architecture of Ratchet using decoration:

class WebSocketBridge implements MessageComponentInterface {
    public function onOpen(ConnectionInterface $conn) { }
    // etc
}

class WebSocketClients implements MessageComponentInterface {
    private $delegate;

    public function __construct(MessageComponentInterface $delegate) {
        $this->delegate = $delegate;
    }

    public function onOpen(ConnectionInterface $conn) {
        $conn->source = 'MainClients';
        $this->delegate->onOpen($conn);
    }
}

class WebSocketOthers { // etc, like WebSocketClients
}

    $pool = new WebSocketBridge;    
    $one = new WsServer(new WebSocketClients($pool));
    $two = new WsServer(new WebSocketOthers($pool));
    // etc

You then have one instance with all your connections pooled together and can route messages how ever you choose.

@cboden YOU THE BEST! Thanks for the great idea!

PS.Thanks everyone for trying to help me. Sorry for stupid qustion, I'm first course student:)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Lugia101101 picture Lugia101101  路  7Comments

clarkk picture clarkk  路  6Comments

Rinum picture Rinum  路  6Comments

rukavina picture rukavina  路  4Comments

nazar-pc picture nazar-pc  路  8Comments