Effortless real time apps in Django

A presentation at DjangoCon Europe 2015 in May 2015 in Cardiff, UK by Aaron Bassett

Slide 1

Slide 1

Effortless real time apps in Django @aaronbassett – rawtech.io

Slide 2

Slide 2

1.0

Slide 3

Slide 3

rawtech.io

Slide 4

Slide 4

I talk weird.

Slide 5

Slide 5

@aaronbassett

Slide 6

Slide 6

Slide 7

Slide 7

1 (function poll() { 2 new Ajax.Request('/api/', { 3 method:'get', 4 onSuccess: function() { ... }, 5 onFailure: function() { ... } 6 }); 7 8 setTimeout(poll, 1000); 9 }());

Slide 8

Slide 8

1 (function poll() { 2 new Ajax.Request('/api/', { 3 method: 'get', 4 timeout: 60000, 5 onSuccess: function() { 6 // Do something 7 poll(); 8 }, 9 onFailure: function() { 10 // Do something else 11 poll(); 12 } 13 }); 14 }());

Slide 9

Slide 9

HACKS UPON HACKS UPON HACKS, UPON HACKS UPON HACKS, UPON HACKS, UPON HACKS UPON HACKS, UPON HACKS, UPON HACKS, UPON HACKS UPON HACKS, UPON HACKS, UPON HACKS, UPON HACKS, UPON HACKS, UPON HACKS UPON HACKS, UPON HACKS, UPON HACKS, UPON HACKS, UPON HACKS, UPON HACKS, UPON HACKS

Slide 10

Slide 10

Slide 11

Slide 11

“typical header sizes of 700-800 bytes is common” –Google “SPDY: An experimental protocol for a faster web"

Slide 12

Slide 12

2 bytes.

Slide 13

Slide 13

0x00 UTF8 DATA 0xFF

Slide 14

Slide 14

100,000

Slide 15

Slide 15

871 bytes

Slide 16

Slide 16

696,800,000

Slide 17

Slide 17

665 Mbps

Slide 18

Slide 18

83 MB

Slide 19

Slide 19

100,000

Slide 20

Slide 20

2 bytes

Slide 21

Slide 21

1,600,000

Slide 22

Slide 22

1.526 Mbps

Slide 23

Slide 23

0.2 MB

Slide 24

Slide 24

POLLING

Slide 25

Slide 25

WEB SOCKETS

Slide 26

Slide 26

SWAMP DRAGON swampdragon.net

Slide 27

Slide 27

=

Slide 28

Slide 28

Slide 29

Slide 29

Slide 30

Slide 30

Slide 31

Slide 31

Slide 32

Slide 32

create YET another todo app ✓

Slide 33

Slide 33

create YET another todo app pip install swampdragon ✓ ✓

Slide 34

Slide 34

create YET another todo app pip install swampdragon (apt-get | brew) install redis ✓ ✓ ✓

Slide 35

Slide 35

dragon admin

Slide 36

Slide 36

1 2 3 4 5 6 7 8 9 10 11 12 #!/usr/bin/env python import os import sys from swampdragon.swampdragon_server import run_server os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<project>.settings") host_port = sys.argv[1] if len(sys.argv) > 1 else None run_server(host_port=host_port)

Slide 37

Slide 37

1 2 3 4 5 6 7 8 9 10 11 12 #!/usr/bin/env python import os import sys from swampdragon.swampdragon_server import run_server os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<project>.settings") host_port = sys.argv[1] if len(sys.argv) > 1 else None run_server(host_port=host_port)

Slide 38

Slide 38

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

-- coding: utf-8 -# Django from django.db import models # Swamp Dragon from swampdragon.models import SelfPublishModel from .serializers import TodoListSerializer, TodoItemSerializer class TodoItem(SelfPublishModel, models.Model): serializer_class = TodoItemSerializer todo_list = models.ForeignKey(TodoList) done = models.BooleanField(default=False) text = models.CharField(max_length=100) def unicode(self): return u"{text} ({status})".format( text=self.text, status=(u"✓" if self.done else u"×") )

Slide 39

Slide 39

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

-- coding: utf-8 -# Django from django.db import models # Swamp Dragon from swampdragon.models import SelfPublishModel from .serializers import TodoListSerializer, TodoItemSerializer class TodoItem(SelfPublishModel, models.Model): serializer_class = TodoItemSerializer todo_list = models.ForeignKey(TodoList) done = models.BooleanField(default=False) text = models.CharField(max_length=100) def unicode(self): return u"{text} ({status})".format( text=self.text, status=(u"✓" if self.done else u"×") )

Slide 40

Slide 40

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

-- coding: utf-8 -# Django from django.db import models # Swamp Dragon from swampdragon.models import SelfPublishModel from .serializers import TodoListSerializer, TodoItemSerializer class TodoItem(SelfPublishModel, models.Model): serializer_class = TodoItemSerializer todo_list = models.ForeignKey(TodoList) done = models.BooleanField(default=False) text = models.CharField(max_length=100) def unicode(self): return u"{text} ({status})".format( text=self.text, status=(u"✓" if self.done else u"×") )

Slide 41

Slide 41

Slide 42

Slide 42

1 2 3 4 5 6 7 8 9 10 11

-- coding: utf-8 -# Swamp Dragon from swampdragon.serializers.model_serializer import ModelSerializer class TodoItemSerializer(ModelSerializer): class Meta: model = 'todo.TodoItem' publish_fields = ('done', 'text') update_fields = ('done', )

Slide 43

Slide 43

1 2 3 4 5 6 7 8 9 10 11

-- coding: utf-8 -# Swamp Dragon from swampdragon.serializers.model_serializer import ModelSerializer class TodoItemSerializer(ModelSerializer): class Meta: model = 'todo.TodoItem' publish_fields = ('done', 'text') update_fields = ('done', )

Slide 44

Slide 44

1 2 3 4 5 6 7 8 9 10 11

-- coding: utf-8 -# Swamp Dragon from swampdragon.serializers.model_serializer import ModelSerializer class TodoItemSerializer(ModelSerializer): class Meta: model = 'todo.TodoItem' publish_fields = ('done', 'text') update_fields = ('done', )

Slide 45

Slide 45

1 2 3 4 5 6 7 8 9 10 11

-- coding: utf-8 -# Swamp Dragon from swampdragon.serializers.model_serializer import ModelSerializer class TodoItemSerializer(ModelSerializer): class Meta: model = 'todo.TodoItem' publish_fields = ('done', 'text') update_fields = ('done', )

Slide 46

Slide 46

1 2 3 4 5 6 7 8 9 10 11

-- coding: utf-8 -# Swamp Dragon from swampdragon.serializers.model_serializer import ModelSerializer class TodoItemSerializer(ModelSerializer): class Meta: model = 'todo.TodoItem' publish_fields = ('done', 'text') update_fields = ('done', )

Slide 47

Slide 47

1 2 3 4 5 6 7 8 9 10 11

-- coding: utf-8 -# Swamp Dragon from swampdragon.serializers.model_serializer import ModelSerializer class TodoItemSerializer(ModelSerializer): class Meta: model = 'todo.TodoItem' publish_fields = ('done', 'text') update_fields = ('done', )

Slide 48

Slide 48

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

-- coding: utf-8 -# Swamp Dragon from swampdragon import route_handler from swampdragon.route_handler import ModelRouter from .models import TodoList, TodoItem from .serializers import TodoListSerializer, TodoItemSerializer class TodoItemRouter(ModelRouter): route_name = 'todo-item' serializer_class = TodoItemSerializer model = TodoItem def get_object(self, **kwargs): return self.model.objects.get(pk=kwargs['id']) def get_query_set(self, **kwargs): return self.model.objects.filter(todo_list__id=kwargs['list_id']) route_handler.register(TodoItemRouter)

Slide 49

Slide 49

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

-- coding: utf-8 -# Swamp Dragon from swampdragon import route_handler from swampdragon.route_handler import ModelRouter from .models import TodoList, TodoItem from .serializers import TodoListSerializer, TodoItemSerializer class TodoItemRouter(ModelRouter): route_name = 'todo-item' serializer_class = TodoItemSerializer model = TodoItem def get_object(self, **kwargs): return self.model.objects.get(pk=kwargs['id']) def get_query_set(self, **kwargs): return self.model.objects.filter(todo_list__id=kwargs['list_id']) route_handler.register(TodoItemRouter)

Slide 50

Slide 50

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

-- coding: utf-8 -# Swamp Dragon from swampdragon import route_handler from swampdragon.route_handler import ModelRouter from .models import TodoList, TodoItem from .serializers import TodoListSerializer, TodoItemSerializer class TodoItemRouter(ModelRouter): route_name = 'todo-item' serializer_class = TodoItemSerializer model = TodoItem def get_object(self, **kwargs): return self.model.objects.get(pk=kwargs['id']) def get_query_set(self, **kwargs): return self.model.objects.filter(todo_list__id=kwargs['list_id']) route_handler.register(TodoItemRouter)

Slide 51

Slide 51

get_list delete create get_single update subscribe unsubscribe

Slide 52

Slide 52

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

-- coding: utf-8 -# Swamp Dragon from swampdragon import route_handler from swampdragon.route_handler import ModelRouter from .models import TodoList, TodoItem from .serializers import TodoListSerializer, TodoItemSerializer class TodoItemRouter(ModelRouter): route_name = 'todo-item' serializer_class = TodoItemSerializer model = TodoItem def get_object(self, **kwargs): return self.model.objects.get(pk=kwargs['id']) def get_query_set(self, **kwargs): return self.model.objects.filter(todo_list__id=kwargs['list_id']) route_handler.register(TodoItemRouter)

Slide 53

Slide 53

Slide 54

Slide 54

Slide 55

Slide 55

1 <div class="row" ng-controller="TodoListCtrl"> 2 {% verbatim %} 3 <h1>{{ todoList.name }}</h1> 4 <p>{{ todoList.description }}</p> 5 6 <div ng-repeat="item in todoItems" class="todos"> 7 . . . 8 </div> 9 {% endverbatim %} 10 </div>

Slide 56

Slide 56

1 <div class="row" ng-controller="TodoListCtrl"> 2 {% verbatim %} 3 <h1>{{ todoList.name }}</h1> 4 <p>{{ todoList.description }}</p> 5 6 <div ng-repeat="item in todoItems" class="todos"> 7 . . . 8 </div> 9 {% endverbatim %} 10 </div>

Slide 57

Slide 57

1 <div class="row" ng-controller="TodoListCtrl"> 2 {% verbatim %} 3 <h1>{{ todoList.name }}</h1> 4 <p>{{ todoList.description }}</p> 5 6 <div ng-repeat="item in todoItems" class="todos"> 7 . . . 8 </div> 9 {% endverbatim %} 10 </div>

Slide 58

Slide 58

1 $dragon.onReady(function() { 2 $dragon.subscribe('todo-item', $scope.channel, {todo_list__id: 1}).then(function(response) { 3 $scope.dataMapper = new DataMapper(response.data); 4 }); 5 6 $dragon.getSingle('todo-list', {id:1}).then(function(response) { 7 $scope.todoList = response.data; 8 }); 9 10 $dragon.getList('todo-item', {list_id:1}).then(function(response) { 11 $scope.todoItems = response.data; 12 }); 13 });

Slide 59

Slide 59

$dragon.subscribe('todo-item', $scope.channel, {todo_list__id: 1})

Slide 60

Slide 60

$dragon.subscribe('todo-item', $scope.channel, {todo_list__id: 1})

Slide 61

Slide 61

6 7 8 $dragon.getSingle('todo-list', {id:1}).then(function(response) { $scope.todoList = response.data; });

Slide 62

Slide 62

10 11 12 $dragon.getList('todo-item', {list_id:1}).then(function(response) { $scope.todoItems = response.data; });

Slide 63

Slide 63

1 $dragon.onReady(function() { 2 $dragon.subscribe('todo-item', ...); 3 $dragon.getSingle('todo-list', ...); 4 $dragon.getList('todo-item', ...); 5 });

Slide 64

Slide 64

1 $dragon.onReady(function() { 2 $dragon.subscribe('todo-item', ...); 3 $dragon.getSingle('todo-list', ...); 4 $dragon.getList('todo-item', ...); 5 });

Slide 65

Slide 65

1 $dragon.onReady(function() { 2 $dragon.subscribe('todo-item', ...); 3 $dragon.getSingle('todo-list', ...); 4 $dragon.getList('todo-item', ...); 5 });

Slide 66

Slide 66

1 $dragon.onReady(function() { 2 $dragon.subscribe('todo-item', ...); 3 $dragon.getSingle('todo-list', ...); 4 $dragon.getList('todo-item', ...); 5 });

Slide 67

Slide 67

1 $dragon.onChannelMessage(function(channels, message) { 2 if (indexOf.call(channels, $scope.channel) > -1) { 3 $scope.$apply(function() { 4 $scope.dataMapper.mapData($scope.todoItems, message); 5 }); 6 } 7 });

Slide 68

Slide 68

1 $dragon.onChannelMessage(function(channels, message) { 2 if (indexOf.call(channels, $scope.channel) > -1) { 3 $scope.$apply(function() { 4 $scope.dataMapper.mapData($scope.todoItems, message); 5 }); 6 } 7 });

Slide 69

Slide 69

1 $dragon.onChannelMessage(function(channels, message) { 2 if (indexOf.call(channels, $scope.channel) > -1) { 3 $scope.$apply(function() { 4 $scope.dataMapper.mapData($scope.todoItems, message); 5 }); 6 } 7 });

Slide 70

Slide 70

1 $dragon.onChannelMessage(function(channels, message) { 2 if (indexOf.call(channels, $scope.channel) > -1) { 3 $scope.$apply(function() { 4 $scope.dataMapper.mapData($scope.todoItems, message); 5 }); 6 } 7 });

Slide 71

Slide 71

DEMO TIME!

Slide 72

Slide 72

Slide 73

Slide 73

git.io/vUWyn

Slide 74

Slide 74

PaaS Platform as a Service

Slide 75

Slide 75

Slide 76

Slide 76

Slide 77

Slide 77

Slide 78

Slide 78

DaaS Database as a Service

Slide 79

Slide 79

www.leggetter.co.uk/2013/12/09/choosingrealtime-web-app-tech-stack.html @leggetter

Slide 80

Slide 80

Slide 81

Slide 81

Slide 82

Slide 82

Slide 83

Slide 83

Slide 84

Slide 84

Slide 85

Slide 85

1 2 3 4 5 6 7 8 9 from pusher import Pusher pusher = Pusher( app_id=u'111545', key=u'895dec4d68d4e286a48d', secret=u'3cd18f6810c758057767' ) pusher.trigger(u'channel_name', u'event_name', {u'message': u'hello world'})

Slide 86

Slide 86

1 2 3 4 5 6 7 8 9 from pusher import Pusher pusher = Pusher( app_id=u'111545', key=u'895dec4d68d4e286a48d', secret=u'3cd18f6810c758057767' ) pusher.trigger(u'channel_name', u'event_name', {u'message': u'hello world'})

Slide 87

Slide 87

1 2 3 4 5 6 7 8 9 from pusher import Pusher pusher = Pusher( app_id=u'111545', key=u'895dec4d68d4e286a48d', secret=u'3cd18f6810c758057767' ) pusher.trigger(u'channel_name', u'event_name', {u'message': u'hello world'})

Slide 88

Slide 88

1 2 3 4 5 6 7 8 9 from pusher import Pusher pusher = Pusher( app_id=u'111545', key=u'895dec4d68d4e286a48d', secret=u'3cd18f6810c758057767' ) pusher.trigger(u'channel_name', u'event_name', {u'message': u'hello world'})

Slide 89

Slide 89

1 2 3 4 5 6 7 8 9 from pusher import Pusher pusher = Pusher( app_id=u'111545', key=u'895dec4d68d4e286a48d', secret=u'3cd18f6810c758057767' ) pusher.trigger(u'channel_name', u'event_name', {u'message': u'hello world'})

Slide 90

Slide 90

1 2 3 4 5 6 7 8 9 from pusher import Pusher pusher = Pusher( app_id=u'111545', key=u'895dec4d68d4e286a48d', secret=u'3cd18f6810c758057767' ) pusher.trigger(u'channel_name', u'event_name', {u'message': u'hello world'})

Slide 91

Slide 91

1 2 3 4 5 6 7 8 9 from pusher import Pusher pusher = Pusher( app_id=u'111545', key=u'895dec4d68d4e286a48d', secret=u'3cd18f6810c758057767' ) pusher.trigger(u'channel_name', u'event_name', {u'message': u'hello world'})

Slide 92

Slide 92

Slide 93

Slide 93

1 class SelfPublishModel(object): 2 3 def _publish(self, action): 4 serializer = self.serializer_class(instance=self) 5 data = serializer.serialize() 6 7 pusher = Pusher( 8 app_id=settings.PUSHER_APP_ID, 9 key=settings.PUSHER_KEY, 10 secret=settings.PUSHER_SECRET 11 ) 12 13 pusher.trigger(self.channel_name, action, data)

Slide 94

Slide 94

1 class SelfPublishModel(object): 2 3 def _publish(self, action): 4 serializer = self.serializer_class(instance=self) 5 data = serializer.serialize() 6 7 pusher = Pusher( 8 app_id=settings.PUSHER_APP_ID, 9 key=settings.PUSHER_KEY, 10 secret=settings.PUSHER_SECRET 11 ) 12 13 pusher.trigger(self.channel_name, action, data)

Slide 95

Slide 95

1 class SelfPublishModel(object): 2 3 def _publish(self, action): 4 serializer = self.serializer_class(instance=self) 5 data = serializer.serialize() 6 7 pusher = Pusher( 8 app_id=settings.PUSHER_APP_ID, 9 key=settings.PUSHER_KEY, 10 secret=settings.PUSHER_SECRET 11 ) 12 13 pusher.trigger(self.channel_name, action, data)

Slide 96

Slide 96

Slide 97

Slide 97

Slide 98

Slide 98

1 var TodoControllers = angular.module('TodoControllers', []); 2 3 TodoControllers.controller('TodoListCtrl', ['$scope', '$pusher', function ($scope, $pusher) { 4 $scope.todoItems = []; 5 6 var client = new Pusher("895dec4d68d4e286a48d"); 7 var pusher = $pusher(client); 8 var todo_items_channel = pusher.subscribe('todo-item'); 9 todo_items_channel.bind('updated', 10 function(data) { 11 for(var i=0; i < $scope.todoItems.length; i++) { 12 if($scope.todoItems[i].id == data.id) { 13 $scope.todoItems[i] = data; 14 return; 15 } 16 } 17 } 18 ); 19 }]);

Slide 99

Slide 99

1 var TodoControllers = angular.module('TodoControllers', []); 2 3 TodoControllers.controller('TodoListCtrl', ['$scope', '$pusher', function ($scope, $pusher) { 4 $scope.todoItems = []; 5 6 var client = new Pusher("895dec4d68d4e286a48d"); 7 var pusher = $pusher(client); 8 var todo_items_channel = pusher.subscribe('todo-item'); 9 todo_items_channel.bind('updated', 10 function(data) { 11 for(var i=0; i < $scope.todoItems.length; i++) { 12 if($scope.todoItems[i].id == data.id) { 13 $scope.todoItems[i] = data; 14 return; 15 } 16 } 17 } 18 ); 19 }]);

Slide 100

Slide 100

1 var TodoControllers = angular.module('TodoControllers', []); 2 3 TodoControllers.controller('TodoListCtrl', ['$scope', '$pusher', function ($scope, $pusher) { 4 $scope.todoItems = []; 5 6 var client = new Pusher("895dec4d68d4e286a48d"); 7 var pusher = $pusher(client); 8 var todo_items_channel = pusher.subscribe('todo-item'); 9 todo_items_channel.bind('updated', 10 function(data) { 11 for(var i=0; i < $scope.todoItems.length; i++) { 12 if($scope.todoItems[i].id == data.id) { 13 $scope.todoItems[i] = data; 14 return; 15 } 16 } 17 } 18 ); 19 }]);

Slide 101

Slide 101

1 var TodoControllers = angular.module('TodoControllers', []); 2 3 TodoControllers.controller('TodoListCtrl', ['$scope', '$pusher', function ($scope, $pusher) { 4 $scope.todoItems = []; 5 6 var client = new Pusher("895dec4d68d4e286a48d"); 7 var pusher = $pusher(client); 8 var todo_items_channel = pusher.subscribe('todo-item'); 9 todo_items_channel.bind('updated', 10 function(data) { 11 for(var i=0; i < $scope.todoItems.length; i++) { 12 if($scope.todoItems[i].id == data.id) { 13 $scope.todoItems[i] = data; 14 return; 15 } 16 } 17 } 18 ); 19 }]);

Slide 102

Slide 102

Slide 103

Slide 103

DEMO TIME!

Slide 104

Slide 104

Slide 105

Slide 105

git.io/vker1

Slide 106

Slide 106

Slide 107

Slide 107

Slide 108

Slide 108

1 2 3 4 5 6 7 8 9 from pusher import Pusher pusher = Pusher( app_id=u'111545', key=u'895dec4d68d4e286a48d', secret=u'3cd18f6810c758057767' ) pusher.trigger(u'channel_name', u'event_name', {u'message': u'hello world'})

Slide 109

Slide 109

1 class PusherMixin(object): 2 3 def render_to_response(self, context, **response_kwargs): 4 5 channel = u"{model}_{pk}".format( 6 model=self.object._meta.model_name, 7 pk=self.object.pk 8 ) 9 event_data = {'user': self.request.user.username} 10 11 pusher = Pusher(app_id=settings.PUSHER_APP_ID, 12 key=settings.PUSHER_KEY, 13 secret=settings.PUSHER_SECRET) 14 pusher.trigger( 15 [channel, ], 16 self.pusher_event_name, 17 event_data 18 ) 19 20 return super(PusherMixin, self).render_to_response(context, **response_kwargs)

Slide 110

Slide 110

1 class PusherMixin(object): 2 3 def render_to_response(self, context, **response_kwargs): 4 5 channel = u"{model}_{pk}".format( 6 model=self.object._meta.model_name, 7 pk=self.object.pk 8 ) 9 event_data = {'user': self.request.user.username} 10 11 pusher = Pusher(app_id=settings.PUSHER_APP_ID, 12 key=settings.PUSHER_KEY, 13 secret=settings.PUSHER_SECRET) 14 pusher.trigger( 15 [channel, ], 16 self.pusher_event_name, 17 event_data 18 ) 19 20 return super(PusherMixin, self).render_to_response(context, **response_kwargs)

Slide 111

Slide 111

1 class PusherMixin(object): 2 3 def render_to_response(self, context, **response_kwargs): 4 5 channel = u"{model}_{pk}".format( 6 model=self.object._meta.model_name, 7 pk=self.object.pk 8 ) 9 event_data = {'user': self.request.user.username} 10 11 pusher = Pusher(app_id=settings.PUSHER_APP_ID, 12 key=settings.PUSHER_KEY, 13 secret=settings.PUSHER_SECRET) 14 pusher.trigger( 15 [channel, ], 16 self.pusher_event_name, 17 event_data 18 ) 19 20 return super(PusherMixin, self).render_to_response(context, **response_kwargs)

Slide 112

Slide 112

1 class PusherMixin(object): 2 3 def render_to_response(self, context, **response_kwargs): 4 5 channel = u"{model}_{pk}".format( 6 model=self.object._meta.model_name, 7 pk=self.object.pk 8 ) 9 event_data = {'user': self.request.user.username} 10 11 pusher = Pusher(app_id=settings.PUSHER_APP_ID, 12 key=settings.PUSHER_KEY, 13 secret=settings.PUSHER_SECRET) 14 pusher.trigger( 15 [channel, ], 16 self.pusher_event_name, 17 event_data 18 ) 19 20 return super(PusherMixin, self).render_to_response(context, **response_kwargs)

Slide 113

Slide 113

1 class PusherMixin(object): 2 3 def render_to_response(self, context, **response_kwargs): 4 5 channel = u"{model}_{pk}".format( 6 model=self.object._meta.model_name, 7 pk=self.object.pk 8 ) 9 event_data = {'user': self.request.user.username} 10 11 pusher = Pusher(app_id=settings.PUSHER_APP_ID, 12 key=settings.PUSHER_KEY, 13 secret=settings.PUSHER_SECRET) 14 pusher.trigger( 15 [channel, ], 16 self.pusher_event_name, 17 event_data 18 ) 19 20 return super(PusherMixin, self).render_to_response(context, **response_kwargs)

Slide 114

Slide 114

1 2 3 4 5 var pusher = new Pusher('{{ settings.PUSHER_KEY }}'); var channel = pusher.subscribe('model_{{ object.pk }}'); channel.bind('update', function(data) { alert(data.user + " has begun updating this object"); });

Slide 115

Slide 115

1 2 3 4 5 var pusher = new Pusher('{{ settings.PUSHER_KEY }}'); var channel = pusher.subscribe('model_{{ object.pk }}'); channel.bind('update', function(data) { alert(data.user + " has begun updating this object"); });

Slide 116

Slide 116

1 2 3 4 5 var pusher = new Pusher('{{ settings.PUSHER_KEY }}'); var channel = pusher.subscribe('model_{{ object.pk }}'); channel.bind('update', function(data) { alert(data.user + " has begun updating this object"); });

Slide 117

Slide 117

1 2 3 4 5 var pusher = new Pusher('{{ settings.PUSHER_KEY }}'); var channel = pusher.subscribe('model_{{ object.pk }}'); channel.bind('update', function(data) { alert(data.user + " has begun updating this object"); });

Slide 118

Slide 118

Slide 119

Slide 119

Slide 120

Slide 120

git.io/vk9uu

Slide 121

Slide 121

@aaronbassett