Dear Django-Team,

I trying to create an IoT application with Django Channels, this is my plan

   - I have is a python program that can fetch data from several hardware 
   devices(PLC, Smart Sensors, etc..) and publish data into the Redis channel 
   (this program is running 24/7 and 365 days).
   - Now I want to forward this data into UI framework (Vue js) using  
   Django (latest) + Channels (latest). Once the UI framework received this 
   data I will render using charts and other animations. (this program should 
   running 24/7 and 365 days )
   - So far I have understood that Django can't directly listen to Redis 
   PubSub Channel and send data through WebSockets to all connected clients. ( 
   Reference link 
   
<https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/django-users/Ny7otqAgqrQ/ey4AOC3VCAAJ>
    ) 
   - So I want to run one more program which can run along with Django 
   Server and listens to Redis channels and sends data to Django-Channels 
   Group (As you know once channels group receives data it will broadcast to 
   all connected client on the same group)
   - But I am facing a few issues,
      - I have read that the channels group will expire after 24 hours. How 
      to handle this situation?
      - I am not sure how stable is my methodology. Is it a better way to 
      do for an application that should run for a lifelong time?
      - Somehow if I managed above two points, how to pack this entire 
      application and distribute it?
   
So far I have done, once the Django server is running, I am starting an 
application and posting data into WebSocket endpoint then it's broadcasting 
the same data to all connected clients.

But it's not sable and throwing an exception after 2-3 hours. I have 
attached Publisher and Subscriber code below for your ref.

*If I stabilize this application I want to make this application 
open-source so Django can be used for IoT application as well*

[image: Base.png]
 
import redis
import time
import traceback
from datetime import datetime
import random
import json

def Publish():
   try:
       r = redis.StrictRedis(host='127.0.0.1', port=6379)                # 
Connect to local Redis instance
       p = r.pubsub()
       while True:
           for i in range(10):
               topicname = 'ch' + str(i)
               randnum = random.randint(1,100) 
                mydict = {'Temperature': randnum}
               data = json.dumps(mydict)
               r.publish(topicname, data)                                  # 
PUBLISH Data on topic
               print(f"{datetime.now()} Published Data {data} on Topic {
topicname}")
           time.sleep(1)

    except Exception as e:
       print("!!!!!!!!!! EXCEPTION !!!!!!!!!")
       print(str(e))
       print(traceback.format_exc())
if __name__ == "__main__":
   Publish()


import time
import traceback
from datetime import datetime
import redis
import json
import websocket
ws = websocket.WebSocket()
ws.connect("ws://127.0.0.1:8000/ws/dashboard/")

def Subscribe():
   try:
       r = redis.StrictRedis(host='127.0.0.1', port=6379,decode_responses=
True)                          # Connect to local Redis instance
       topicname = ['ch0','ch1','ch2','ch3','ch4']
       p = r.pubsub()                                                       
       
       p.subscribe(topicname)  
       p.get_message()                                               
        print(f"Subscriber Started For Topic {topicname}...")
       for message in p.listen():                                           
       # Checks for message
           data = message['data']
           #print(data)
           if type(data) == str:
               j_data = json.loads(data)
               #print(data)
               ws.send(data)
               print(f"{datetime.now()} Data is : {j_data}")
               
   except Exception as e:
       print("!!!!!!!!!! EXCEPTION !!!!!!!!!")
       print(str(e))
       print(traceback.format_exc())

if __name__ == "__main__":
   Subscribe()

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/286c8240-1271-4382-b78d-67e510b1fa94%40googlegroups.com.

Reply via email to