You need a WebSocket ! Django is not directly supported, but I would recommend taking a look at Websockets for Django applications using Redis . I have used this before and it is very easy to configure and use.
For example, create a django template (e.g. output.html ):
<html> ... <body> <textarea id="output" row=3 cols=25></textarea> <script> var ws = new WebSocket('{{ ws_url }}'); ws.onmessage = function(e) { $('#output').append(e.data); }; </script> </body> </html>
Then create a method that will call your script when it wants to display the message in the text area:
def output_message(session_key, user, message): conn = redis.StrictRedis() ws_url = '{0}:{1}'.format(session_key, user) conn.publish(ws_url, message)
And finally, you need to specify in your views.py method that displays your output.html template:
def output_console(request): template_values = {'ws_url':'ws://{SERVER_NAME}:{SERVER_PORT}/ws/{0}?subscribe-session'.format(request.user.id, **request.META)} return render(request, 'output.html', template_values)
Check out the chat server for git repo projects for a more in-depth sample code.
Hope this helps and good luck!
source share