简体   繁体   中英

Real Time data in Django

I want to observe data changes in real time in python to create a program that show list of online friends (ie every time a friend become online or offline I want update a list). I have a function that make an infinite loop to receive the presence a from am XMPP server. I can't seem to figure out how I could receive data in real-time and update. I use Django for backend and i want ti serve this data to a web page to show online Friends. I tried with Channels, but it's unuseful for this case.

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock = ssl.wrap_socket(s, ssl_version=ssl. ssl.PROTOCOL_TLSv1_2)
sock.connect((hostname, port))

sock.sendall(“<presence/>”.encode())

while True:
  response = sock.recv(1024)

How can i pass this results to my page view every time i get new update insidie loop?

How are Channels 'not useful'? From where I stand it's the best approach you could utilize. Sockets are too low level to use in this case, I mean how will you utilize them in your browser. Django doesn't support keep alive connections; that leaves channels (websockets) which are supported out-of-the-box by JS and can actually persist entire time a user in online. You can implement a looping event in JS (user's end) which sends messages to your server via the websocket to show the user is still online, the user's friends will all share/subscribe to his/her channel and can all be notified of her online status. Just my thoughts yo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM