Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Django and Real-time Web

1,679 views

Published on

My talk from DjangoCon Europe 2015.

How to make Django play well with real-time web.
I'm giving short overview on how to use Pusher or your own WebSocket-based server, to easily add real-time functionality into your application.

Published in: Technology
  • Be the first to comment

Django and Real-time Web

  1. 1. Django and Real-time Web Rivo Laks
  2. 2. www.thorgate.euTHORGATE A Piece of History...
  3. 3. www.thorgate.euTHORGATE GMail and Firefox, ca 2005
  4. 4. www.thorgate.euTHORGATE
  5. 5. www.thorgate.euTHORGATE Nope…
  6. 6. Getting Started
  7. 7. www.thorgate.euTHORGATE PubSub Publish & Subscribe pattern Can use external services!
  8. 8. www.thorgate.euTHORGATE
  9. 9. www.thorgate.euTHORGATE # Create Pusher client pusher = Pusher( app_id='121933', key='f1ebb516981a3315e492', secret='SECRET' ) # ... and send the message! pusher.trigger('test_channel', 'showMessage', { 'message': message, }) Pusher server
  10. 10. www.thorgate.euTHORGATE // Create Pusher client var pusher = new Pusher('f1ebb516981a3315e492'); // Subscribe to events on our channel var channel = pusher.subscribe('test_channel'); // And react to showMessage events channel.bind('showMessage', function(data) { alert(data.message); }); Pusher client
  11. 11. www.thorgate.euTHORGATE $.post(SUBMIT_URL, { message: message, }); Messages from Clients Just use AJAX requests – the easiest way
  12. 12. Going Further
  13. 13. www.thorgate.euTHORGATE WebSockets • Standardized in 2011 • Fast, two-way connection • Can use custom protocols
  14. 14. www.thorgate.euTHORGATE WebSockets & Django • asyncio and websockets library • custom server process
  15. 15. www.thorgate.euTHORGATE Server Code @asyncio.coroutine def client_handler(websocket, uri): # Send a message yield from websocket.send("Hello world") # Wait for a reply msg = yield from websocket.recv() @asyncio.coroutine def client_handler(websocket, uri): # Send a message yield from websocket.send("Hello world") # Wait for a reply msg = yield from websocket.recv()
  16. 16. www.thorgate.euTHORGATE @asyncio.coroutine def client_handler(websocket, uri): # Send a message yield from websocket.send("Hello world") # Read all messages while the socket is open while True: msg = yield from websocket.recv() if msg is None: # Oops, the socket has closed return # Fancy message processing print("got message:", msg) Server Code @asyncio.coroutine def client_handler(websocket, uri): # Send a message yield from websocket.send("Hello world") # Read all messages while the socket is open while True: msg = yield from websocket.recv() if msg is None: # Oops, the socket has closed return # Fancy message processing print("got message:", msg)
  17. 17. www.thorgate.euTHORGATE Client Code • Pure JS • https://github.com/ cScarlson/sawkit-client
  18. 18. www.thorgate.euTHORGATE Client Code // Connect to the server $ws('ws://localhost:8080').ready(function($ws, ws) { // When connection is made, send a message $ws.emit('showMessage', { msg: 'WebSockets are fun!', }); // And react to incoming messages $ws.on('showMessage', function(data) { alert(data.message); }); });
  19. 19. www.thorgate.euTHORGATE Conclusion • Django & real-time can play together! • External services make it very easy • Websockets, without external service, is still pretty simple
  20. 20. Rivo Laks @RivoLaks Demo: http://djangocon.thorgate.eu Code: https://github.com/rivol/djangocon- realtime Thanks!

×