Sending serial port-data via websocket in Django framework

I need to visualize data on Django-based webserver; which data is captured on serial port, like a stream.

I use python **serial ** package to listen on the serial port, and I store these data in DB.

I also have my Django application using async websocket with the purpose to update immediately the client side when new data is captured.

Although I cannot connect these two application together.

I was believing that ChannelLayer might be the key, although there are no more different instances of the application; only one. So it might be a wrong approach.

I also thinking on Worker or Background Task, however I don't see how/where should I implement the serial-port listener, which is basically an infinite loop:

        while True:
            data = serial_connection.read_until()
            if data == b"EOF":
                break

            print(data)

Using https://channels.readthedocs.io/en/stable/topics/worker.html as reference, I think, it should be inside the consumer:

# Inside a consumer
self.channel_layer.send(
    "serialport-listener",
    {
        "type": "listener",
        "id": 123456789,
    },
)

What is the concept and the architecture that might resolve this problem?

Once criterion: as the performance matters, I would prefer to send captured data to client before saving that in the DB.

Back to Top