You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CONTRIBUTING.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -82,7 +82,7 @@ The documentation uses [Sphinx](https://www.sphinx-doc.org/en/master/). The sour
82
82
83
83
After running `git commit`, `pre-commit` will check the committed code. This check can be passed, skipped or failed.
84
84
85
-
If the check failed, it is possible that `pre-commit` auto-fixed the code, so you only need to re-stage and re-commit. If it did not auto-fie the code, you will need to do so manually.
85
+
If the check failed, it is possible that `pre-commit` auto-fixed the code, so you only need to re-stage and re-commit. If it did not auto-fix the code, you will need to do so manually.
86
86
87
87
`pre-commit` will only check the code that is staged, the unstaged code will be stashed during the checks.
_asyncio-mqtt_ combines the stability of the time-proven [paho-mqtt](https://github.com/eclipse/paho.mqtt.python) library with a modern, asyncio-based interface.
35
+
asyncio-mqtt combines the stability of the time-proven [paho-mqtt](https://github.com/eclipse/paho.mqtt.python) library with a modern, asyncio-based interface.
36
36
37
37
- No more callbacks! 👍
38
38
- No more return codes (welcome to the `MqttError`)
@@ -41,6 +41,8 @@ _asyncio-mqtt_ combines the stability of the time-proven [paho-mqtt](https://git
41
41
- Fully type-hinted
42
42
- Did we mention no more callbacks?
43
43
44
+
The whole thing is less than [900 lines of code](https://github.com/sbtinstruments/asyncio-mqtt/blob/main/asyncio_mqtt/client.py).
45
+
44
46
<!-- pitch end -->
45
47
46
48
---
@@ -53,15 +55,15 @@ _asyncio-mqtt_ combines the stability of the time-proven [paho-mqtt](https://git
53
55
54
56
## Installation
55
57
56
-
_asyncio-mqtt_ can be installed via `pip install asyncio-mqtt`. It requires Python 3.7+ to run. The only dependency is [paho-mqtt](https://github.com/eclipse/paho.mqtt.python).
58
+
asyncio-mqtt can be installed via `pip install asyncio-mqtt`. It requires Python 3.7+ to run. The only dependency is [paho-mqtt](https://github.com/eclipse/paho.mqtt.python).
57
59
58
60
If you can't wait for the latest version and want to install directly from GitHub, use:
Since Python 3.8, the default asyncio event loop is the `ProactorEventLoop`. Said loop [doesn't support the `add_reader` method](https://docs.python.org/3/library/asyncio-platforms.html#windows) that is required by _asyncio-mqtt_. Please switch to an event loop that supports the `add_reader` method such as the built-in `SelectorEventLoop`:
66
+
Since Python 3.8, the default asyncio event loop is the `ProactorEventLoop`. Said loop [doesn't support the `add_reader` method](https://docs.python.org/3/library/asyncio-platforms.html#windows) that is required by asyncio-mqtt. Please switch to an event loop that supports the `add_reader` method such as the built-in `SelectorEventLoop`:
65
67
66
68
```python
67
69
# Change to the "Selector" event loop
@@ -97,11 +99,11 @@ The changelog lives in the [CHANGELOG.md](https://github.com/sbtinstruments/asyn
97
99
98
100
## Related projects
99
101
100
-
Is _asyncio-mqtt_ not what you're looking for? There are a few other clients you can try:
102
+
Is asyncio-mqtt not what you're looking for? There are a few other clients you can try:
101
103
102
104
-[paho-mqtt](https://github.com/eclipse/paho.mqtt.python) — Own protocol implementation. Synchronous.<br>
103
105
-[gmqtt](https://github.com/wialon/gmqtt) — Own protocol implementation. Asynchronous.<br>
104
106
-[fastapi-mqtt](https://github.com/sabuhish/fastapi-mqtt) — Asynchronous wrapper around gmqtt. Simplifies integration in your FastAPI application.<br>
105
107
-[amqtt](https://github.com/Yakifo/amqtt) — Own protocol implementation. Asynchronous. Includes a broker.<br>
106
108
-[mqttools](https://github.com/eerimoq/mqttools) — Own protocol implementation. Asynchronous.<br>
107
-
-[trio-paho-mqtt](https://github.com/bkanuka/trio-paho-mqtt) — Asynchronous wrapper around paho-mqtt (similar to _asyncio-mqtt_). Based on trio instead of asyncio.<br>
109
+
-[trio-paho-mqtt](https://github.com/bkanuka/trio-paho-mqtt) — Asynchronous wrapper around paho-mqtt (similar to asyncio-mqtt). Based on trio instead of asyncio.<br>
For subscribing and listening to messages, a minimal working example looks like this:
18
+
19
+
```python
20
+
import asyncio
21
+
import asyncio_mqtt as aiomqtt
22
+
23
+
24
+
asyncdefmain():
25
+
asyncwith aiomqtt.Client("test.mosquitto.org") as client:
26
+
asyncwith client.messages() as messages:
27
+
await client.subscribe("humidity/#")
28
+
asyncfor message in messages:
29
+
print(message.payload)
30
+
31
+
32
+
asyncio.run(main())
33
+
```
34
+
35
+
## Payload encoding
36
+
37
+
Message payloads can be of types `int`, `float`, `str`, `bytes`, `bytearray`, and `None`.
38
+
39
+
asyncio-mqtt (or more precisely, paho-mqtt) automatically converts `int` and `float` payloads to `str`. If you want to send a true `int` or `float`, you can use [`struct.pack()`](https://docs.python.org/3/library/struct.html) to encode it as a `bytes` object.
40
+
41
+
If no payload is given or if it's set to `None`, a zero-length payload will be sent.
42
+
43
+
All other types you'll have to encode yourself. For example, if you want to send a `dict` as JSON, you can use `json.dumps()` (which returns a `str`):
44
+
45
+
```python
46
+
import asyncio
47
+
import asyncio_mqtt as aiomqtt
48
+
import json
49
+
50
+
51
+
asyncdefmain():
52
+
asyncwith aiomqtt.Client("test.mosquitto.org") as client:
On the receiving end, you can then use `json.loads()` to decode the JSON string back into a `dict`.
60
+
61
+
## Filtering messages
62
+
63
+
Imagine you're measuring temperature and humidity on the outside and inside, and our topics look like this: `temperature/outside`. You want to receive all types of measurements but handle them differently.
64
+
65
+
asyncio-mqtt provides `Topic.matches()` to make this easy:
66
+
67
+
```python
68
+
import asyncio
69
+
import asyncio_mqtt as aiomqtt
70
+
71
+
72
+
asyncdefmain():
73
+
asyncwith aiomqtt.Client("test.mosquitto.org") as client:
74
+
asyncwith client.messages() as messages:
75
+
await client.subscribe("#")
76
+
asyncfor message in messages:
77
+
if message.topic.matches("humidity/outside"):
78
+
print(f"[humidity/outside] {message.payload}")
79
+
if message.topic.matches("+/inside"):
80
+
print(f"[+/inside] {message.payload}")
81
+
if message.topic.matches("temperature/#"):
82
+
print(f"[temperature/#] {message.payload}")
83
+
84
+
85
+
asyncio.run(main())
86
+
```
87
+
88
+
Note that in our example, messages to `temperature/inside` are handled twice!
Copy file name to clipboardExpand all lines: docs/sharing-the-connection.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -67,4 +67,4 @@ Managing a connection by calling `connect` and `disconnect` directly is a bit tr
67
67
68
68
Context managers take care of all connection and disconnection logic for you, in a way that makes it very difficult to shoot yourself in the foot. They are a lot easier and less error-prone to use than `connect`/`disconnect`.
69
69
70
-
Supporting both context managers and manual `connect`/`disconnect` would add a lot of complexity to _asyncio-mqtt_. To keep maintainer burden manageable, we focus only on the preferred option: context managers.
70
+
Supporting both context managers and manual `connect`/`disconnect` would add a lot of complexity to asyncio-mqtt. To keep maintainer burden manageable, we focus only on the preferred option: context managers.
0 commit comments