xavierlesa / channels-asgi-mqtt

Interface between MQTT and ASGI and Channels 2.0 compatible
GNU General Public License v3.0
44 stars 30 forks source link

Some messages are not sent to django mqtt channel #3

Open ipasic opened 5 years ago

ipasic commented 5 years ago

Hi, thanks for nice interface between channels and mqtt, I've tested it and it works but what I noticed is that many messages (~30-40 %) are not sent to django mqtt channel. Do you know where this could come from?

xavierlesa commented 5 years ago

Hello Ivan, thanks you for your support! I'm not sure why you've experimented messages losses. My first thought is mqtt and some kind of unexpected close or something like timeout.

congcheung commented 5 years ago

Hi, thanks for bringing such a good api. I found an improvement: when starting MQTT broker, I need to determine the current operating system type, because the function loop. add_signal_handler in server.py does not support in Windows system, for example: ` def run(self): self.stop = False loop = asyncio.get_event_loop() self.loop = loop

    for signame in ('SIGINT', 'SIGTERM'):
        if(platform.system()=='Windows'):
            signal.signal(signal.SIGTERM, self.stop_server)
            signal.signal(signal.SIGINT, self.stop_server)
        else:
            loop.add_signal_handler(
                    getattr(signal, signame),
                    functools.partial(self.stop_server, signame)
                )

` Happy life, thank you.

Ali-aqrabawi commented 5 years ago

i had the same issue, the reason why this is happening is because the chasgimqtt Server is using the same channel for pub and sub, so the server is pushing the message to the channel and poll it again,

this is not always happening because it depend on who will pick it up first, the django worker or chasgimqtt.Server.client_pool_message , if you add a break like follow, the issue disappears :

    async def client_pool_message(self):
        logger.info("Loop de recepción de messages")

        while True:
            break # break pulling messages from channel
            print(self.mqtt_channel_name)
            logger.info("Espera recibir un message desde el channel %s", self.mqtt_channel_name)
            result = await self.channel.receive(self.mqtt_channel_name)
            self._mqtt_receive(result)
            await asyncio.sleep(0.1)

my suggested solution is simply to use two different channels, one for pub and another for sub,

i will submit a pr for this,

xavierlesa commented 5 years ago

Hi @zcboys sorry for the very long delay, I was super busy! Im not familiar with Python in Windows but I can see what you mean, in fact in the doc this method is related for UNIX signals https://docs.python.org/3/library/asyncio-eventloop.html?highlight=add_signal_handler#unix-signals ... not sure how to get SIGNALS in Windows or even if is possible.

Your solution would work (maybe changing to win32 systems):

import sys
...
...
    for signame in ('SIGINT', 'SIGTERM'):
        if  sys.platform == 'win32':
            signal.signal(signal.SIGTERM, self.stop_server)
            signal.signal(signal.SIGINT, self.stop_server)
        else:
            ...

Does it works for you?

xavierlesa commented 5 years ago

Hi @Ali-aqrabawi, breaking that coroutine will not work because you are skipping all the staff bellow of the break and never is computed, i'm surprised if it works, i will try your pr :+1: . Yes you can use two channels and will work too.

xavierlesa commented 5 years ago

@Ali-aqrabawi I was wrong, breaking the loop is correct and coroutine is working normaly.

uhuntu commented 4 years ago

please see my fix #9

nhatnamnguyengtvthcm commented 4 years ago

Hi, thanks for nice interface between channels and mqtt, I've tested it and it works but what I noticed is that many messages (~30-40 %) are not sent to django mqtt channel. Do you know where this could come from?

I have the same issue

Minivolk02 commented 3 years ago

Hi everybody, so how to solve this problem?