Good day. Suppose there are two users. One presses the button on the site, the second immediately sees the message "user 1 pressed the button" without updating the page. Advise how to implement? Every second or two to request the state of the user button 1 Ajax?

  • Take as the basis of any of the chats that implement WebSocket and see how it works. - And

2 answers 2

There are several ways to make one user see the changes on the page made by another user. The overall model is called Comet . Implementation is possible in various ways, for example:

  1. Long polling .

The fact is that the browser establishes a connection to the server (using XMLHttpRequest), waiting for the moment when the client wants to send something to the server. After receiving data from the client, the connection is closed and a new connection is immediately created. The web server must support this method.

  1. Websocket

This is a boneless method of implementing the most real sockets on the web.

It should be noted that using only django , you cannot use any of these methods, since django processes each request synchronously and the very first polling connection will suspend your django application. To organize a comet-server, you can see a great asynchronous python web-framework tornado , which supports both long polling and WebSockets.

  • Can I use a socket on js? learn.javascript.ru/websockets#example- browser-code For example, make a simple site for django two-page. One page is user 1, the second is user 2. We open sockets on clients for receiving and sending. And ip users for the socket are simply sent with the server's response, simply retrieving from request - Yaktens Teed
  • django works on the principle of receiving a request -> processed -> sent a response . Any delay in this chain and all other customers will be waiting. Purely theoretically, you can organize asynchronous work with sockets even in django , but I think you can’t even imagine what kind of hell it will be in implementation and support. So in no case do not advise. For a comet server, you need to use something besides django , the same node.js - Nikmoon

See the django-channels. https://channels.readthedocs.io/en/stable/ Adds Websocket support to django.

Author: Andrew Godwin In the next versions, the jangi channels promise to add to the standard distribution.