Websocket / event-source / ... implementation to expose two-way RPC for python / django application

for the django application I'm working on, I need to implement two RPC methods:

  • clients can call RPC methods from the platform and
  • the platform can call RPC methods from each client.

Since clients will be primarily behind NAT (which means no public IP addresses and unpredictable weird firewall policies), client-client must initiate a path to the client.

I have a pretty good idea on how I can write this from scratch, I also think I can change something from the publisher / subscriber model, but I found out that there is always a better way to do this in python.

So I'm wondering what would be the best way to do this, which would also integrate the best into django. In the short term, the code will be able to reach hundreds of customers and (hopefully) with thousands of customers in the medium / long term.

So which library / implementation would you recommend me to use? I mainly look at the starting points for RTFM!

+3
source share
4 answers

websocket is a moving target with new specifications from time to time. Brave developers implement the library on the server side, but few implement the client side. The client for the web socket is a web browser.

websocket - , - . . Twitter . http- . , - ( , - ). - .

websocket eventource . Websocket . , , .

eventource -. . , , , http-, django.

, , , . Django , , nginx. Django cron like action , . django, - , .

Gevent http-, . . , .

+7

Tornado + Tornadio + Socket.io. , , , , .

from tornadio2 import SocketConnection, TornadioRouter, SocketServer
class RouterConnection(SocketConnection):
__endpoints__ = {'/chat': ChatConnection,
                 '/ping': PingConnection,
                 '/notification' : NotificationConnection
                 }

def on_open(self, info):
    print 'Router', repr(info)

MyRouter = TornadioRouter(RouterConnection)

# Create socket application
application = web.Application(
MyRouter.apply_routes([(r"/", IndexHandler),
    (r"/socket.io.js", SocketIOHandler)]),
        flash_policy_port = 843,
        flash_policy_file = op.join(ROOT, 'flashpolicy.xml'),
        socket_io_port = 3001,
        template_path=os.path.join(os.path.dirname(__file__), "templates/notification")
)

class PingConnection(SocketConnection):
     def on_open(self, info):
       print 'Ping', repr(info)

     def on_message(self, message):
       now = dt.utcnow()
       message['server'] = [now.hour, now.minute, now.second, now.microsecond / 1000]
       self.send(message)
class ChatConnection(SocketConnection):
   participants = set()
   unique_id = 0

   @classmethod
   def get_username(cls):
       cls.unique_id += 1
       return 'User%d' % cls.unique_id

   def on_open(self, info):
       print 'Chat', repr(info)

       # Give user unique ID
       self.user_name = self.get_username()
       self.participants.add(self)

   def on_message(self, message):
       pass

   def on_close(self):
       self.participants.remove(self)

   def broadcast(self, msg):
       for p in self.participants:
            p.send(msg)
+2

, :

import tornado.ioloop
import tornado.web
import time

class MainHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous 
    def get(self):
        self.set_header("Content-Type", "text/event-stream")
        self.set_header("Cache-Control", "no-cache")
        self.write("Hello, world")
        self.flush()
        for i in range(0, 5):
            msg = "%d<br>" % i
            self.write("%s\r\n" % msg) # content
            self.flush()
            time.sleep(5)


application = tornado.web.Application([
    (r"/", MainHandler),
])

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

curl http://localhost:8888

, !

, . URL- lib, , .

+1

I recently played with Django, Server-Sent Events and WebSocket, and I wrote an article about it at http://curella.org/blog/2012/jul/17/django-push-using-server-sent-events-and- websocket /

Of course, this is due to the usual warnings that Django is probably not suitable for events, and both protocols are still drafts.

+1
source

All Articles