How can i send real time data to a client on Django?

Viewed 387

On a server, i'm hosting a Python script that streams some JSON data received by a lot of IoT sensors. I'm trying to find a way to have that data on a Django application: if the user opens the page for sensor 30, the user must get real time data from sensor number 30 and so on. I know that Django Channels is the standard to hadle real time data on Django, but in this case the data is not generated by the client and its not generated by Django Channels, the user only needs to receive the data. Using a database is out of question due to performances and because i don't need to store any data at all.

Is there a way to do this? I was thinking of creating a websocket on my data generator script (using SocketIO maybe?), for every sensor i would create a room, while on my Django frontend i would add some Javascript to connect to the websocket server and to that sensor's room. So user opens the page for sensor 35, the frontend will connect it to my external websocket server and then to the room of that sensor.

I know this question is not specific enough, i'm willing to give more details if necessary; any kind of advice is appreciated!

For reference, here is the piece of code that starts the script and streams the data; in this case it prints the data, what i need it to do is to send it do the Django frontend. This operation cannot be done inside a Django channels consumer because it would block it:

def process_message(sensor_data):
    JSON1 = json.dumps(sensor_data)
    JSON2 = json.loads(JSON1)
    quantity = JSON2['Quantity']
    status = JSON2['Status']
    print(quantity, status)


bm = ('USER', 'PASSWORD')
bm.start_sensor_data(sensors, process_message)
bm.start()
0 Answers
Related