Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Django WebSockets & WebHooks

Jpadilla1
October 16, 2014

Django WebSockets & WebHooks

Jpadilla1

October 16, 2014
Tweet

More Decks by Jpadilla1

Other Decks in Programming

Transcript

  1. WebSocket Apps • Social Feeds! • Multiplayer games! • Collaborative

    editing/coding! • Sports updates! • Multimedia chats! • Location-based apps! • Online education
  2. The problem with Django WSGI! Web Server Gateway Interface! !

    WSGI does not support WebSockets out of the box
  3. Problem • Hard to maintain • Dependent on another infrastructure

    • Other solutions include using Twisted or Tornado
  4. uWSGI supports WebSockets out of the box. Use Redis as

    a Messaging Queue More info -> http://django-websocket- redis.readthedocs.org/en/latest/introduction.html
  5. Advantages • It is simpler to implement • The asynchronous

    I/O loop handling websockets can run • inside Django with ./manage.py run server • as a stand alone HTTP Server, using uWSGI • using NGiNX as proxy in two decoupled loops, one for WSGI and one for websocket HTTP in front of two separate uWSGI workers. • The whole Django API is available in this loop.
  6. def send_notification(sender, **kwargs): ! m = kwargs['instance'] ! data =

    { ! "sent_by": m.sent_by.email, ! "content": m.content, ! "created_at": m.created_at ! } ! response =\ ! requests.post( ! url=settings.WEBHOOK_URL, ! data=json.dumps(data, cls=MyEncoder), ! headers={'content-type': 'application/json'} ! ) ! ! ! ! post_save.connect(send_notification, sender=Message)
  7. +

  8. What it does • Receives a message through the API

    in Django! • Does a POST to the given WEBHOOK_URL with the data! • Node receives the data! • Node sends it off through a WebSocket to the app