This repository was archived by the owner on Aug 19, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 127
Adds MQTT backend #117
Closed
Closed
Adds MQTT backend #117
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7ecb641
Adds MQTT backend
912d9ce
Update setup.py
Kludex c2bad5c
Uses double quotes for strings on new MQTT code
Governa 89bb579
Fixes linting on new MQTT code
Governa b3b8598
Fixes blank lines in new MQTT code
Governa d7d1090
Removes unused import from new MQTT code
Governa 4ee12fa
Works around mypy problem
9e6fe71
Fixes import ordering
c648bb5
The returned message type is bytes
0aeec88
Configures MQTT to accept anonymous users for testing
bc34a32
Adds the MQTT service to GitHub workflow
96942df
Adds mqtt to the requirements file
fb905af
Add mqtt backend
alex-oleshkevich 7af169b
Merge remote-tracking branch 'upstream/master' into mqqt-backend
alex-oleshkevich 67ed04a
debug service
alex-oleshkevich bfbbc96
debug ci
alex-oleshkevich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import asyncio | ||
| import typing | ||
| from urllib.parse import urlparse | ||
|
|
||
| import aiomqtt | ||
|
|
||
| from .._base import Event | ||
| from .base import BroadcastBackend | ||
|
|
||
|
|
||
| class MqttBackend(BroadcastBackend): | ||
| def __init__(self, url: str): | ||
| parsed_url = urlparse(url) | ||
| self._host = parsed_url.hostname or "localhost" | ||
| self._port = 8883 if parsed_url.scheme == "mqtts" else 1883 | ||
| self._port = parsed_url.port or self._port | ||
| self._client = aiomqtt.Client(self._host, port=self._port) | ||
| self._queue: asyncio.Queue[aiomqtt.Message] = asyncio.Queue() | ||
| self._listener_task = asyncio.create_task(self._listener()) | ||
|
|
||
| async def connect(self) -> None: | ||
| await self._client.__aenter__() | ||
|
|
||
| async def disconnect(self) -> None: | ||
| self._listener_task.cancel() | ||
| try: | ||
| await self._listener_task | ||
| except asyncio.CancelledError: | ||
| pass | ||
|
|
||
| await self._client.__aexit__(None, None, None) | ||
|
|
||
| async def subscribe(self, channel: str) -> None: | ||
| await self._client.subscribe(channel) | ||
|
|
||
| async def unsubscribe(self, channel: str) -> None: | ||
| await self._client.unsubscribe(channel) | ||
|
|
||
| async def publish(self, channel: str, message: typing.Any) -> None: | ||
| await self._client.publish(channel, message, retain=False) | ||
|
|
||
| async def next_published(self) -> Event: | ||
| message = await self._queue.get() | ||
|
|
||
| # Event.message is string, not bytes | ||
| # this is a limiting factor and we need to make sure | ||
| # that the payload is bytes in order to properly decode it | ||
| assert isinstance(message.payload, bytes), "Payload must be bytes." | ||
|
|
||
| return Event(channel=message.topic.value, message=message.payload.decode()) | ||
|
|
||
| async def _listener(self) -> None: | ||
| async for message in self._client.messages: | ||
| await self._queue.put(message) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,4 +2,4 @@ uvicorn | |
| websockets | ||
| starlette | ||
| jinja2 | ||
| broadcaster[redis,postgres,kafka] | ||
| broadcaster[redis,postgres,kafka,mqtt] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| -e .[redis,postgres,kafka] | ||
| -e .[redis,postgres,kafka,mqtt] | ||
|
|
||
| # Documentation | ||
| mkdocs==1.5.3 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have tried to keep to a strict style within
stepsof:I'd prefer not to break that if possible.