simps / mqtt

🕹 MQTT Protocol Analysis and Coroutine Client for PHP. Support for 3.1, 3.1.1 and 5.0 versions of the MQTT protocol.

Home Page:https://mqtt.simps.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[QUESTION] Send message to specific subscribers

RazaGR opened this issue · comments

How can we send message to only connections which are subscribed to specific topic exmple/topic?
Below code is sending message to all connections, which are not subscribed to that topic, I would like to send message to only connections which are subscribed to exmple/topic :

                            // Send to subscribers
                            foreach ($server->connections as $sub_fd) {
                                if ($sub_fd != $fd) {
                                    $server->send(
                                        $sub_fd,
                                        V3::pack(
                                            [
                                                'type' => $data['type'],
                                                'topic' => $data['topic'],
                                                'message' => $data['message'],
                                                'dup' => $data['dup'],
                                                'qos' => $data['qos'],
                                                'retain' => $data['retain'],
                                                'message_id' => $data['message_id'] ?? '',
                                            ]
                                        )
                                    );
                                }
                            }

Is there something available in Swoole or, Do I have to manage the state myself? like for example I can store in DB each new connection and then compare before sending.

commented

You need to store the subscription connection information yourself