Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push live updates to client through Django Channels & Websockets

I'm trying to make a page which shows live-updating data to the client. The rest of the site is built with Django, so I'm trying to use Channels for this.

The data I am displaying is saved in both a JSON file and a MySQL database for further calculations in other parts of the site. Ideally, I would like to display the latest data received (that is, when the file updates) to the client as it is received.

And even though as I understand Channels are built exactly for this purpose, I am having trouble doing it.

I have tried sending multiple requests from the client-side with delay and loops in the consumer, but it either (ironically) only updates on refresh or updates instantly. However, neither of these approaches are triggered by a change in the file or database.

This is the code that "works", but doesn't really do what's required. (also, admittedly, there's basically nothing there...)

# consumers.py
def ws_connect(message):
  message.reply_channel.send({"accept": True})

def ws_receive(message):

  with open("data.json") as jsonfile:
    jsondata = json.load(jsonfile)

  res = json.dumps(jsondata)
  message.reply_channel.send({ "text": res, })

#routing.py
from channels.routing import route
from .consumers import ws_receive, ws_connect


channel_routing = [
  route("websocket.receive", ws_receive, path=r"^/websockets/$"),
  route("websocket.connect", ws_connect, path=r"^/websockets/$"),
]

JS used:

<script>
var wsurl = "ws://" + "mywebsite.com" + "/websockets/";
socket = new WebSocket(wsurl);

socket.onopen = function() {
     socket.send("this is a request");
     console.log('sent');
  }

socket.onmessage = function(message) {
    console.log(message.data);
    document.getElementById("livedata").innerHTML = message.data;
}
</script>

I'd be perfectly happy with a link to the docs that would help me achieve something like this, as I've managed to not find the solution for a whole week.

like image 691
Roxerg Avatar asked Mar 09 '23 00:03

Roxerg


1 Answers

Add user to django channels Group on ws_connect

from channels.auth import channel_session_user_from_http
from channels import Group

@channel_session_user_from_http
def ws_connect(message, **kwargs):
    http_user = message.user
    if not http_user.is_anonymous:
        message.reply_channel.send({'accept': True})
        Group('user-'+str(http_user.id)).add(message.reply_channel)`

send any new updates data to the user by

Group('user-1').send({'text': 'hello user 1'})
Group('user-2').send({'text': 'hello user 2'})
like image 76
Amulya Acharya Avatar answered Mar 23 '23 01:03

Amulya Acharya