Django Server Migration Method

I developed a Django application that requires pushing a server (or comet) for further improvement. At first, I successfully implemented a push server using "orbital", which worked fine in all major browsers except IE (yes, I know ... this IE spins my life again). Although IE has been significantly reduced, I would still like to implement server push, which will work on all major platforms.

I have been working for several days and here are some options that I have found.

It seems that using the Django + Tornado platform is more ideal in the long run, but I could not find a good example from which I can learn how to implement server push that combines two structures. The only example I found is

django-tornado-demo ( https://github.com/bdarnell/django-tornado-demo/tree/master/testsite )

However, I'm not sure that this project even handles the push server mechanism or otherwise, has no explanation.

Can anyone direct me to a good example of a demo project that integrates Django and Tornado together and actually implements a push server mechanism? In addition, all information on this topic will be highly appreciated.

Thank you in advance.

+4
source share
2 answers

SockJS is the way to go: https://github.com/mrjoes/sockjs-tornado

First, make your environment ready:

pip install sockjs-tornado 

Secondly, you need to configure Tornado:

 application = django.core.handlers.wsgi.WSGIHandler() container = tornado.wsgi.WSGIContainer(application) tornado_app = tornado.web.Application( EchoSockjsRouter('/websocket')+ [ ('/source/([^/]+)', SourceHandler), ('.*', tornado.web.FallbackHandler, dict(fallback=container)), ] ) HTTPServer(tornado_app).listen(port) tornado.ioloop.IOLoop.instance().start() 

'/ source' is the request handler (I use it for async with @ gen.engine), '. * 'works for Django, and EchoSockjsRouter is what you want:

 from sockjs.tornado import SockJSRouter, SockJSConnection class EchoWebSocket(SockJSConnection): def on_open(self, request): print "sockjs: open" def on_message(self, data): print "data: %r" % (data,) self.send(data) def on_comment(self, data): print "data: %r" % (data,) self.send(data) def on_close(self): print "sockjs: close" def EchoSockjsRouter(prefix): return SockJSRouter(EchoWebSocket, prefix).urls 

After that, you will need to configure the user interface (I use the SockJS client, version 0.3.4, http://sockjs.org , MIT license):

 <script src="<SockJS Javascript>"></script> <script> socket = new SockJS('http://localhost:8000/websocket'); socket.onmessage = function(e) { console.log(e.data); }; socket.onclose = function() { console.log('closed :('); }; socket.onopen = function() { console.log('opened :>'); letTheMadnessBegin(); }; function letTheMadnessBegin() { // silly, but you get the idea socket.send(JSON.stringify({ name: $('#name').text(), message: $('input').val() })); } <script> 

Well, you can make the script pretty inside $ (function () {}) ... but also, you are ready to go: http://grab.by/mSv6 , http://grab.by/mSuU (sorry for 404 to make it realistic Oo). I took this template from: https://idea.popcount.org/2012-09-21-django-with-sockjs/

Please note that I tried with Socketio, Tornado WebSocket and some others. However, SocketJS was incredibly simple.

Sincerely.

+6
source

There is also Socket.IO , you can use it with gevent-socketio / django-socketio with Django. (The API is pretty similar to SockJS examples, perhaps fooobar.com/questions/174768 / ... )

+1
source

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


All Articles