Handling multiple protocols in Channels 2

2019-05-02 Thread Andrea Conti
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 django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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/fe5328d6-9808-4623-9f49-950955e74704%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Handling multiple protocols in Channels 2

2019-05-02 Thread Andrea Conti

First of all, thanks for responding.

While the example does route the incoming MQTT messages to a consumer, the 
consumer is still in the same process as the MQTT client, i.e. the one 
started with "runmqttworker" management command.

What I am trying to do is tohandle those events within another process.

Andrea

On Thursday, May 2, 2019 at 2:33:01 PM UTC+2, Fly Style wrote:
>
>
> 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 django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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/90504a49-5d89-452b-8528-1705d54a8d2c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Handling multiple protocols in Channels 2

2019-05-06 Thread Andrea Conti

Ok, but that doesnt'solve the problem with handling the events in the right 
process.

Anyway, I think I figured it out. It seems there is nothing magical to a 
'protocol handler' after all, so I can just create a thread with its event 
loop and run the MQTT listener in there. No other processes required, and I 
don't even need to use a channel layer as long as I can live without the 
group facilities.

Runs fine in development mode, let's hope it does not blow up with an 
external ASGI server.

andrea

On Monday, May 6, 2019 at 6:26:52 AM UTC+2, Fly Style wrote:
>
> for example:
>
> from asgiref.sync import async_to_sync as a2s
> from channels.layers import get_channel_layer
> from channels_mqtt import settings
>
>
> event = {"type": settings.MQTT_PUBLISH, "text": {"topic": topic, "payload"
> : payload, "qos": qos, "retain": retain}}
> a2s(channel_layer.send)(channel, event)
>
>

-- 
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 django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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/ba3fe782-9efe-4bc9-a5af-d9c10fc9f2f2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.