How to display the update transferred from the server to the web page without updating?

I use Django 1.10 as the backend of the repository we created.

I am adding another new feature where the web page will be displayed on a giant monitor. On this web page nothing will be visible except 1 giant number.

I have an RFID device that, after detecting the RFID tag, will send an HTTP request to my Django server.

When this happens, I want the RFID tag number to appear on the web page mentioned earlier.

I read a little about socket.io, but I want to keep the Django universe as much as possible. I also read briefly about Django channels.

My questions:

  • Should I use Django feeds for this use case?
  • if so, how to do this with my use case above?
+4
source share
2 answers

It really depends on the use of the information you display, if you do not need this number to be in real time, then you can choose a regular AJAX poll once every X seconds, as indicated in the zwer comment.

Now, if you need this number to be in real time, you should look for django web ports and channels, it’s very simple to set up a code base that does what you want.

Assuming you installed django feeds and configured your settings .

consumers.py routing.py, - ( views.py urls.py, websocket).

consumers.py

from channels import Group
from channels.auth import channel_session_user_from_http, channel_session_user


@channel_session_user_from_http
def ws_add(message):
    # Authenticates the client
    # ASGI WebSocket packet-received and send-packet message types
    # both have a "text" key for their textual data.
    message.reply_channel.send({
        "accept": True,
    })
    Group("rfid-group").add(message.reply_channel)


@channel_session_user
def ws_message(message):
    # You can process messages you receive from the socket here
    # Apply w/e logic validation


@channel_session_user
def ws_disconnect(message):
    Group("rfid-group").discard(message.reply_channel)

routing.py

from channels.routing import route
from .consumers import ws_message, ws_add, ws_disconnect

routing_routing = [
    route("websocket.connect", ws_add),
    route("websocket.receive", ws_message),
    route("websocket.disconnect", ws_disconnect),
]

front-end websocket:

<script>
    socket = new WebSocket("ws://" + window.location.host);
    socket.onmessage = function(e) {
        console.log("Message received");
        // Process the received number here
        console.log(e.data);
    }
</script>

-, "rfid-group", , , , .

, rfid, , view, RFID HTTP-.

from django.http import HttpResponse
from channels import Group


def rfid_processor(request):
    '''
      Consider authenticating your rfid_num producer to prevent
      someone messing with your api
    '''
    rfid_num = request.GET.get("rfid", "")
    if rfid_num:
        Group("rfid-group").send({"text": rfid_num})
        return HttpResponse(status=200)

    return HttpResponse(status=500)

URL:

from app.views import rfid_processor
urlpatterns = [
    url(r'^rfid/$', rfid_processor),
]

, django, -, RFID, .

, .

+4

Source: https://habr.com/ru/post/1669768/


All Articles