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
The worker has a Consumer which listens to 'mqtt' channels_layer messages such as "mqtt.subscribe" which takes a topic and channels_layer group.
It contains an async MQTT client which connects to the server and subscribes to the topic.
Incoming MQTT messages are matched to the subscriptions and are then sent to the channels_layer group requested.
However it needs a client to connect to some django Consumer (the tutorial uses the chat WS) and that consumer to then send an mqtt.subscribe message before the Consumer and hence client is instantiated.
This works well but has some drawbacks:
Every websocket consumer needs to call mqtt.subscribe
Each ws consumer needs to wait for the MQTT client to connect and get ready
Connectivity/initialisation issues don't appear in the worker until the first connection
The WS consumers need to know about the topics etc - ideally this would be handled in MqttConsumer subclass logic
This would allow a Consumer to inherit from MqttConsumer and subscribe to MQTT events, potentially constructing state from retained messages etc that could be used before any client connection is established.
Looking at the code this doesn't seem possible and backtracking into the Worker class it's not quite clear how this should be done properly.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
TL;DR;
I want my ChannelNameRouter worker Consumer to start as soon as the worker is run. Not after the first incoming channels_layer message.
I've written https://github.com/lbt/channels-mqtt-proxy
The worker has a Consumer which listens to 'mqtt' channels_layer messages such as "mqtt.subscribe" which takes a topic and channels_layer group.
It contains an async MQTT client which connects to the server and subscribes to the topic.
Incoming MQTT messages are matched to the subscriptions and are then sent to the channels_layer group requested.
However it needs a client to connect to some django Consumer (the tutorial uses the chat WS) and that consumer to then send an mqtt.subscribe message before the Consumer and hence client is instantiated.
This works well but has some drawbacks:
I would like the consumer to be created in ChannelNameRouter.init().
https://github.com/django/channels/blob/main/channels/routing.py#L171
This would allow a Consumer to inherit from MqttConsumer and subscribe to MQTT events, potentially constructing state from retained messages etc that could be used before any client connection is established.
Looking at the code this doesn't seem possible and backtracking into the Worker class it's not quite clear how this should be done properly.
Beta Was this translation helpful? Give feedback.
All reactions