Reduce, Reuse, Recycle - Persisting WebSocket connections with SharedWorkers

A presentation at DjangoCon Europe 2019 in April 2019 in Copenhagen, Denmark by Aaron Bassett

Slide 1

Slide 1

Slide 2

Slide 2

Slide 3

Slide 3

Slide 4

Slide 4

Slide 5

Slide 5

Slide 6

Slide 6

Slide 7

Slide 7

Slide 8

Slide 8

Slide 9

Slide 9

Slide 10

Slide 10

Slide 11

Slide 11

Slide 12

Slide 12

Slide 13

Slide 13

Slide 14

Slide 14

Slide 15

Slide 15

Slide 16

Slide 16

Slide 17

Slide 17

HTTP/1.1 200 OK Content-Type: text/html; charset=iso-8859-1 Transfer-Encoding: chunked

Slide 18

Slide 18

HTTP/1.1 200 OK Content-Type: text/html; charset=iso-8859-1 Transfer-Encoding: chunked

Slide 19

Slide 19

33 <script> doSomething(); !</script> 33 <script> doSomething(); !</script> 33 <script> doSomething(); !</script>

Slide 20

Slide 20

33 <script> doSomething(); !</script> 33 <script> doSomething(); !</script> 33 <script> doSomething(); !</script>

Slide 21

Slide 21

Slide 22

Slide 22

(function poll() { new Ajax.Request(‘/api/’, { method: ‘get’, timeout: 60000, onSuccess: function() { !// Do something poll(); }, onFailure: function() { !// Do something else poll(); } }); }());

Slide 23

Slide 23

GET / HTTP/1.1 Host: 2019.djangocon.eu Connection: keep-alive Upgrade-insecure-requests: 1 Dnt: 1 User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/ 537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/ webp,image/apng,/;q=0.8,application/signed-exchange;v=b3 Referer: https:!//!!www.google.com/ Accept-encoding: gzip, deflate, br Accept-language: en-GB,en;q=0.9,en-US;q=0.8

Slide 24

Slide 24

HTTP/1.1 200 OK Server: nginx/1.14.0 (Ubuntu) Date: Wed, 10 Apr 2019 17:18:50 GMT Content-Type: text/html Last-Modified: Wed, 10 Apr 2019 12:14:03 GMT Connection: keep-alive ETag: W/”5cadde0b-4c01” Strict-Transport-Security: max-age=31536000; X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN Content-Encoding: gzip

Slide 25

Slide 25

GET / HTTP/1.1 Upgrade: WebSocket Connection: Upgrade Host: !!www.websocket.org

Slide 26

Slide 26

HTTP/1.1 101 WebSocket Protocol Handshake Upgrade: WebSocket Connection: Upgrade

Slide 27

Slide 27

Slide 28

Slide 28

Slide 29

Slide 29

Slide 30

Slide 30

Slide 31

Slide 31

Slide 32

Slide 32

Slide 33

Slide 33

Slide 34

Slide 34

Slide 35

Slide 35

Slide 36

Slide 36

class CountConsumer(AsyncWebsocketConsumer): async def connect(self): self.room_name = ??? self.room_group_name = ??? self.redis_client = redis.Redis(???) self.redis_client.incr(‘connections’) self.redis_client.set(‘updated’, time.time()) await self.channel_layer.group_add( self.room_group_name, self.channel_name ) await self.channel_layer.group_send( self.room_group_name, { ‘type’: ‘connection_message’, ‘updated’: ???, ‘connections’: ??? } ) await self.accept()

Slide 37

Slide 37

class CountConsumer(AsyncWebsocketConsumer): async def connect(self): self.room_name = ??? self.room_group_name = ??? self.redis_client = redis.Redis(???) self.redis_client.incr(‘connections’) self.redis_client.set(‘updated’, time.time()) await self.channel_layer.group_add( self.room_group_name, self.channel_name ) await self.channel_layer.group_send( self.room_group_name, { ‘type’: ‘connection_message’, ‘updated’: ???, ‘connections’: ??? } ) await self.accept() class CountConsumer(AsyncWebsocketConsumer): async def connect(self):

Slide 38

Slide 38

class CountConsumer(AsyncWebsocketConsumer): async def connect(self): self.room_name = ??? self.room_group_name = ??? self.redis_client = redis.Redis(???) self.redis_client.incr(‘connections’) self.redis_client.set(‘updated’, time.time()) await self.channel_layer.group_add( self.room_group_name, self.channel_name ) await self.channel_layer.group_send( self.room_group_name, { ‘type’: ‘connection_message’, ‘updated’: ???, ‘connections’: ??? } ) await self.accept() self.redis_client.incr(‘connections’) self.redis_client.set(‘updated’, time.time())

Slide 39

Slide 39

class CountConsumer(AsyncWebsocketConsumer): async def connect(self): self.room_name = ??? self.room_group_name = ??? self.redis_client = redis.Redis(???) self.redis_client.incr(‘connections’) self.redis_client.set(‘updated’, time.time()) await self.channel_layer.group_add( self.room_group_name, self.channel_name ) await self.channel_layer.group_send( self.room_group_name, { ‘type’: ‘connection_message’, ‘updated’: ???, ‘connections’: ??? } ) await self.accept() await self.channel_layer.group_send( self.room_group_name, { ‘type’: ‘connection_message’, ‘updated’: ???, ‘connections’: ??? } )

Slide 40

Slide 40

async def disconnect(self, close_code): self.redis_client = redis.Redis(???) self.redis_client.decr(‘connections’) await self.channel_layer.group_discard( self.room_group_name, self.channel_name ) await self.channel_layer.group_send( self.room_group_name, { ‘type’: ‘connection_message’, ‘updated’: ???, ‘connections’: ??? } )

Slide 41

Slide 41

async def disconnect(self, close_code): self.redis_client = redis.Redis(???) self.redis_client.decr(‘connections’) await self.channel_layer.group_discard( self.room_group_name, self.channel_name ) await self.channel_layer.group_send( self.room_group_name, { ‘type’: ‘connection_message’, ‘updated’: ???, ‘connections’: ??? } ) async def disconnect(self, close_code): self.redis_client = redis.Redis(???) self.redis_client.decr(‘connections’)

Slide 42

Slide 42

async def disconnect(self, close_code): self.redis_client = redis.Redis(???) self.redis_client.decr(‘connections’) await self.channel_layer.group_discard( self.room_group_name, self.channel_name ) await self.channel_layer.group_send( self.room_group_name, { ‘type’: ‘connection_message’, ‘updated’: ???, ‘connections’: ??? } ) await self.channel_layer.group_send( self.room_group_name, { ‘type’: ‘connection_message’, ‘updated’: ???, ‘connections’: ??? } )

Slide 43

Slide 43

Slide 44

Slide 44

<script> var chatSocket = new WebSocket(URL) chatSocket.onmessage = function(e) { var data = JSON.parse(e.data) var totalConnections = data[‘connections’] var lastUpdated = data[‘updated’] document.querySelector(‘#count’).textContent = totalConnections document.querySelector(‘#updated’).textContent = lastUpdated } chatSocket.onclose = function(e) { console.error(‘Chat socket closed unexpectedly’); } !</script>

Slide 45

Slide 45

<script> var chatSocket = new WebSocket(URL) chatSocket.onmessage = function(e) { var data = JSON.parse(e.data) var totalConnections = data[‘connections’] var lastUpdated = data[‘updated’] document.querySelector(‘#count’).textContent = totalConnections document.querySelector(‘#updated’).textContent = lastUpdated } chatSocket.onclose = function(e) { console.error(‘Chat socket closed unexpectedly’); } !</script>

var chatSocket = new WebSocket(URL)

Slide 46

Slide 46

<script> var chatSocket = new WebSocket(URL) chatSocket.onmessage = function(e) { var data = JSON.parse(e.data) var totalConnections = data[‘connections’] var lastUpdated = data[‘updated’] document.querySelector(‘#count’).textContent = totalConnections document.querySelector(‘#updated’).textContent = lastUpdated } chatSocket.onclose = function(e) { console.error(‘Chat socket closed unexpectedly’); } !</script>

chatSocket.onmessage = function(e) { var data = JSON.parse(e.data) var totalConnections = data[‘connections’] var lastUpdated = data[‘updated’] document.querySelector(‘#count’).textContent = totalConnections document.querySelector(‘#updated’).textContent = lastUpdated }

Slide 47

Slide 47

<script> var chatSocket = new WebSocket(URL) chatSocket.onmessage = function(e) { var data = JSON.parse(e.data) var totalConnections = data[‘connections’] var lastUpdated = data[‘updated’] document.querySelector(‘#count’).textContent = totalConnections document.querySelector(‘#updated’).textContent = lastUpdated } chatSocket.onclose = function(e) { console.error(‘Chat socket closed unexpectedly’); } !</script>

chatSocket.onclose = function(e) { console.error(‘Chat socket closed unexpectedly’); }

Slide 48

Slide 48

Slide 49

Slide 49

Slide 50

Slide 50

Slide 51

Slide 51

const connections = []; var sharedWebSocket; onconnect = function(e) { const port = e.ports[0]; connections.push(port); port.start(); if (!sharedWebSocket) { sharedWebSocket = new WebSocket(URL); sharedWebSocket.onmessage = function(e) { connections.forEach(function(connection) { let data = JSON.parse(e.data); connection.postMessage(data); }); }; } sharedWebSocket.send(“PING”); };

Slide 52

Slide 52

const connections = []; var sharedWebSocket; onconnect = function(e) { const port = e.ports[0]; connections.push(port); port.start(); if (!sharedWebSocket) { sharedWebSocket = new WebSocket(URL); sharedWebSocket.onmessage = function(e) { connections.forEach(function(connection) { let data = JSON.parse(e.data); connection.postMessage(data); }); }; } sharedWebSocket.send(“PING”); }; const connections = []; var sharedWebSocket; onconnect = function(e) { const port = e.ports[0]; connections.push(port); port.start();

Slide 53

Slide 53

const connections = []; var sharedWebSocket; onconnect = function(e) { const port = e.ports[0]; connections.push(port); port.start(); if (!sharedWebSocket) { sharedWebSocket = new WebSocket(URL); sharedWebSocket.onmessage = function(e) { connections.forEach(function(connection) { let data = JSON.parse(e.data); connection.postMessage(data); }); }; } sharedWebSocket.send(“PING”); }; if (!sharedWebSocket) { sharedWebSocket = new WebSocket(URL);

Slide 54

Slide 54

const connections = []; var sharedWebSocket; onconnect = function(e) { const port = e.ports[0]; connections.push(port); port.start(); if (!sharedWebSocket) { sharedWebSocket = new WebSocket(URL); sharedWebSocket.onmessage = function(e) { connections.forEach(function(connection) { let data = JSON.parse(e.data); connection.postMessage(data); }); }; } sharedWebSocket.send(“PING”); }; sharedWebSocket.onmessage = function(e) { connections.forEach(function(connection) { let data = JSON.parse(e.data); connection.postMessage(data); }); };

Slide 55

Slide 55

const connections = []; var sharedWebSocket; onconnect = function(e) { const port = e.ports[0]; connections.push(port); port.start(); if (!sharedWebSocket) { sharedWebSocket = new WebSocket(URL); sharedWebSocket.onmessage = function(e) { connections.forEach(function(connection) { let data = JSON.parse(e.data); connection.postMessage(data); }); }; } sharedWebSocket.send(“PING”); }; sharedWebSocket.onmessage = function(e) { connections.forEach(function(connection) { let data = JSON.parse(e.data); connection.postMessage(data); }); };

Slide 56

Slide 56

const connections = []; var sharedWebSocket; onconnect = function(e) { const port = e.ports[0]; connections.push(port); port.start(); if (!sharedWebSocket) { sharedWebSocket = new WebSocket(URL); sharedWebSocket.onmessage = function(e) { connections.forEach(function(connection) { let data = JSON.parse(e.data); connection.postMessage(data); }); }; } sharedWebSocket.send(“PING”); }; sharedWebSocket.onmessage = function(e) { connections.forEach(function(connection) { let data = JSON.parse(e.data); connection.postMessage(data); }); };

Slide 57

Slide 57

const connections = []; var sharedWebSocket; onconnect = function(e) { const port = e.ports[0]; connections.push(port); port.start(); if (!sharedWebSocket) { sharedWebSocket = new WebSocket(URL); sharedWebSocket.onmessage = function(e) { connections.forEach(function(connection) { let data = JSON.parse(e.data); connection.postMessage(data); }); }; } sharedWebSocket.send(“PING”); }; sharedWebSocket.onmessage = function(e) { connections.forEach(function(connection) { let data = JSON.parse(e.data); connection.postMessage(data); }); };

Slide 58

Slide 58

const connections = []; var sharedWebSocket; onconnect = function(e) { const port = e.ports[0]; connections.push(port); port.start(); if (!sharedWebSocket) { sharedWebSocket = new WebSocket(URL); sharedWebSocket.onmessage = function(e) { connections.forEach(function(connection) { let data = JSON.parse(e.data); connection.postMessage(data); }); }; } sharedWebSocket.send(“PING”); }; sharedWebSocket.onmessage = function(e) { connections.forEach(function(connection) { let data = JSON.parse(e.data); connection.postMessage(data); }); };

Slide 59

Slide 59

const worker = new SharedWorker(‘js/shared-worker.js’) worker.port.onmessage = function(e) { var data = e.data var totalConnections = data[‘connections’] var lastUpdated = data[‘updated’] document.querySelector(‘#count’).textContent = totalConnections document.querySelector(‘#updated’).textContent = lastUpdated } worker.port.start()

Slide 60

Slide 60

const worker = new SharedWorker(‘js/shared-worker.js’) worker.port.onmessage = function(e) { var data = e.data var totalConnections = data[‘connections’] var lastUpdated = data[‘updated’] document.querySelector(‘#count’).textContent = totalConnections document.querySelector(‘#updated’).textContent = lastUpdated } worker.port.start() const worker = new SharedWorker(‘js/shared-worker.js’)

Slide 61

Slide 61

const worker = new SharedWorker(‘js/shared-worker.js’) worker.port.onmessage = function(e) { var data = e.data var totalConnections = data[‘connections’] var lastUpdated = data[‘updated’] document.querySelector(‘#count’).textContent = totalConnections document.querySelector(‘#updated’).textContent = lastUpdated } worker.port.start() worker.port.onmessage = function(e) { var data = e.data var totalConnections = data[‘connections’] var lastUpdated = data[‘updated’] document.querySelector(‘#count’).textContent = totalConnections document.querySelector(‘#updated’).textContent = lastUpdated }

Slide 62

Slide 62

Slide 63

Slide 63

Slide 64

Slide 64

Slide 65

Slide 65

Slide 66

Slide 66