php-mqtt / client

An MQTT client written in and for PHP.
MIT License
373 stars 72 forks source link

Separate protocol from client #171

Closed Dmcz closed 11 months ago

Dmcz commented 11 months ago

I suggest separating the protocol into its own standalone repository because the client can be implemented in various ways, but the protocol parsing remains consistent and exclusively relies on binary string interpretation.

Namoshek commented 11 months ago

That is already the case if you look at the

https://github.com/php-mqtt/client/blob/458afc0bf33075ed8a1ffad72af5e10f2b516220/src/MessageProcessors/Mqtt31MessageProcessor.php#L21

If you have concrete changes in mind, I'm open to discuss them though.

Dmcz commented 11 months ago

What I mean is creating a dedicated repository for the protocol. Because protocols can be used to implement different clients or server.

For example, you can use php sockets to implement an mqtt client like this repository, or I can use coroutine to implement another client, but these clients can share the same protocol package.

Dmcz commented 11 months ago

And I recommend defining packet classes such as Connect, Connack, Publish, Puback, aligning with the corresponding MQTT protocol packets. Additionally, consider naming the protocol classes using versions like V3, V4, V5. These protocol classes should handle packing packet class into binary strings and unpacking received strings into packet class. This can more enhance consistency with the MQTT protocol.

Namoshek commented 11 months ago

For example, you can use php sockets to implement an mqtt client like this repository, or I can use coroutine to implement another client, but these clients can share the same protocol package.

You can use the loopOnce() method to integrate the client with other event loops or coroutines.

However, implementing a WebSocket based client would be a good argument to move the protocol to its own package. At least if you don't want it be integrated in the current client.

And I recommend defining packet classes such as Connect, Connack, Publish, Puback, aligning with the corresponding MQTT protocol packets. Additionally, consider naming the protocol classes using versions like V3, V4, V5. These protocol classes should handle packing packet class into binary strings and unpacking received strings into packet class. This can more enhance consistency with the MQTT protocol.

I've thought about this before, but there are two things I'm not so happy about:

The hard part about all of this is not publishing messages (i.e. the serialization), but the deserialization of data and addressing the correct handlers. What you suggest is basically a rewrite of most parts of the client. And I don't think I have the time and motivation to do this right now. Which doesn't mean I won't consider contributions though. If you want to give it a shot, I'm also ok at looking at a draft with one or two implemented messages as proof of concept.

Dmcz commented 11 months ago

I'll try it, thanks for your reply.