Swoole-src: WebSocket: How to send data to specific group connected/subscribed to specific topic

Created on 6 Jul 2020  ·  5Comments  ·  Source: swoole/swoole-src

I've checked the documentation but it is not stated their on how to send to specific group connected to specific topic. I'm using the code below to broadcast message but the problem is it sends to all clients connected.

$output = json_decode($frame->data, true);

foreach ($server->connections as $fd) {         
    if ($server->exist($fd) && $server->isEstablished($fd)) {
        $server->push($fd, json_encode($output), SWOOLE_WEBSOCKET_OPCODE_TEXT, SWOOLE_WEBSOCKET_FLAG_FIN | SWOOLE_WEBSOCKET_FLAG_COMPRESS);
    } else { 
        $server->close($fd); 
    }
}
question

All 5 comments

I dont have much idea about json, but judging from the description, these grouping could be easily done by a database.

In SQL, it is just as simple as: SELECT GROUP WHERE TOPIC = "mytopic"

I think you have got to extract, who belongs to which so that you can send whatever you want for that group.

I've checked the documentation but it is not stated their on how to send to specific group connected to specific topic. I'm using the code below to broadcast message but the problem is it sends to all clients connected.

$output = json_decode($frame->data, true);

foreach ($server->connections as $fd) {           
    if ($server->exist($fd) && $server->isEstablished($fd)) {
        $server->push($fd, json_encode($output), SWOOLE_WEBSOCKET_OPCODE_TEXT, SWOOLE_WEBSOCKET_FLAG_FIN | SWOOLE_WEBSOCKET_FLAG_COMPRESS);
    } else { 
        $server->close($fd); 
    }
}

You need to group the connections yourself. For example:

/**@var array */
$subscribeFds['group1'] = [1, 2, 3];
$subscribeFds['group2'] = [4, 5, 6];

foreach ($subscribeFds['group1'] as $fd) {          
    if ($server->exist($fd) && $server->isEstablished($fd)) {
        $server->push($fd, json_encode($output), SWOOLE_WEBSOCKET_OPCODE_TEXT, SWOOLE_WEBSOCKET_FLAG_FIN | SWOOLE_WEBSOCKET_FLAG_COMPRESS);
    } else { 
        $server->close($fd); 
    }
}

foreach ($subscribeFds['group2'] as $fd) {          
    if ($server->exist($fd) && $server->isEstablished($fd)) {
        $server->push($fd, json_encode($output), SWOOLE_WEBSOCKET_OPCODE_TEXT, SWOOLE_WEBSOCKET_FLAG_FIN | SWOOLE_WEBSOCKET_FLAG_COMPRESS);
    } else { 
        $server->close($fd); 
    }
}

Based on your example, where are these [1, 2, 3] and [4, 5, 6] come from? Are these a user's primary key value from the database? Sorry for the noob question.

$subscribeFds['group1'] = [1, 2, 3];
$subscribeFds['group2'] = [4, 5, 6];

Based on your example, where are these [1, 2, 3] and [4, 5, 6] come from? Are these a user's primary key value from the database? Sorry for the noob question.

$subscribeFds['group1'] = [1, 2, 3];
$subscribeFds['group2'] = [4, 5, 6];

Your websocket client 1,2,3 sent a subscribe request to topic1; websocket client 4, 5, 6 sent a subscribe request to topic1. For example:

public function onMessage(WebSocketServer $server, Frame $frame): void
{
    if (in_array($frame->data, ['topic1', 'topic2'])) {
        $subscribeFds[$frame->data] = $frame->fd;
    }
}

You should store the fd group information in redis.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rovico picture rovico  ·  4Comments

sshymko picture sshymko  ·  3Comments

Gemorroj picture Gemorroj  ·  3Comments

qifengzhang007 picture qifengzhang007  ·  3Comments

lotarbo picture lotarbo  ·  4Comments