have a try of https://github.com/ruralscenery/channels_mqtt
Andrea Conti於 2019年5月2日星期四 UTC+8下午7時40分20秒寫道: > > 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? > https://channels.readthedocs.io/en/latest/topics/channel_layers.html says > that "Messages across channel layers also go to consumers/ASGI application > instances, just like events from the client", and > https://channels.readthedocs.io/en/latest/topics/worker.html seems to > imply that such events are simply available to the main ASGI application -- > but that relies on a having a dummy protocol handler > (channels.worker.Worker) subscribe to a set of channels and wrap each event > in a scope which is then passed to the ASGI application. So, once again > that would seem to require a second protocol handler besides the > http/websocket one. > > Am I missing something? > > Any Ideas, corrections and pointers to relevant documentation and examples > are welcome. > > Thanks in advance, > Andrea > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d15f1a36-0857-4bd2-b5e9-490b46c54a94%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.

