Hello,
I am trying to use Channels 2 to implement an http/websocket application which also handles asynchronous requests from a second source (right now it's messages from an MQTT subscription, but I think the problem is largely independent from the specific protocol). This doesn't seem to be a common use case, and I could not find any indication on how to do that, either in the official Channels documentation or elsewhere
Given that I must handle the contents of the MQTT messages in the same process as the websocket requests, the ideal approach would be to run both the http/websocket and the MQTT protocol handlers in the same process, but I am quite certain I can't do that.
My next thought would then be to have a second process for handling the incoming messages and sending them to the main application as events over a channel layer.
For the MQTT side, leaving the specific protocol aside, I have seen examples using asgiref.server.StatelessServer (e.g.
https://github.com/andrewgodwin/asgigram), but that ends up creating a scope from the event and passing it to an ASGI application instance -- i.e. it handles the events in-process, which is not what I want.
Then there's
https://github.com/xavierlesa/channels-asgi-mqtt. The code looks a bit messy, and I think it's been written for Channels 1, but the principle seems clear: retrieve the default channel layer and for every incoming message, send an event of a specific type to a channel with a specific name.
But then, how do I receive and handle events from the channel in the websocket process?
Am I missing something?
Any Ideas, corrections and pointers to relevant documentation and examples are welcome.
Thanks in advance,
Andrea