javascript - Push live updates to client through Django Channels & Websockets - Stack Overflow

I'm trying to make a page which shows live-updating data to the client. The rest of the site is bu

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" + "/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.

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." + "/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.

Share Improve this question asked Sep 18, 2017 at 20:38 RoxergRoxerg 1161 silver badge13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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'})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742281690a4414606.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信