From 94eebeff177d5bedd26bdc6e72aa10d286e72588 Mon Sep 17 00:00:00 2001 From: ozer550 Date: Mon, 31 Oct 2022 00:26:15 +0530 Subject: [PATCH 1/4] limit ping messages only when neccesarry --- .../frontend/shared/data/serverSync.js | 23 +++++++++++++++++++ .../viewsets/websockets/consumers.py | 8 +++++++ 2 files changed, 31 insertions(+) diff --git a/contentcuration/contentcuration/frontend/shared/data/serverSync.js b/contentcuration/contentcuration/frontend/shared/data/serverSync.js index e8816ae375..687e63768e 100644 --- a/contentcuration/contentcuration/frontend/shared/data/serverSync.js +++ b/contentcuration/contentcuration/frontend/shared/data/serverSync.js @@ -28,7 +28,11 @@ import urls from 'shared/urls'; // change being registered, sync changes! const SYNC_IF_NO_CHANGES_FOR = 0.5; +// 25 seconds +const WEBSOCKET_PING_INTERVAL = 25000; + let socket; +let shouldWePing = true; // Flag to check if a sync is currently active. let syncActive = false; @@ -36,6 +40,7 @@ const commonFields = ['type', 'key', 'table', 'rev', 'channel_id', 'user_id']; const objectFields = ['objs', 'mods']; const ignoredSubFields = [COPYING_FLAG, LAST_FETCHED, TASK_ID]; + const ChangeTypeMapFields = { [CHANGE_TYPES.CREATED]: commonFields.concat(['obj']), [CHANGE_TYPES.UPDATED]: commonFields.concat(['mods']), @@ -260,6 +265,8 @@ async function WebsocketSendChanges() { // in order to still call our change cleanup code. if (changes.length) { requestPayload.changes = changes; + //set ping to false to reset ping timmer + shouldWePing = false; socket.send( JSON.stringify({ payload: requestPayload, @@ -415,6 +422,18 @@ async function handleChanges(changes) { let intervalTimer; +// Skip the Pinging if we recently interacted with backend +var pingTimer = function () { + if(shouldWePing == false){ + shouldWePing = true; + return; + } + socket.send(JSON.stringify({ + ping: "PING!", + })) +} + + export function startSyncing() { // Initiate a sync immediately in case any data // is left over in the database. @@ -431,6 +450,10 @@ export function startSyncing() { socket.addEventListener('open', () => { console.log('Websocket connected'); }); + + //Keep Pinging the websocket connection to keep connection alive + setInterval(pingTimer, WEBSOCKET_PING_INTERVAL); + // Listen for any errors due to which connection may be closed. socket.addEventListener('error', event => { diff --git a/contentcuration/contentcuration/viewsets/websockets/consumers.py b/contentcuration/contentcuration/viewsets/websockets/consumers.py index 722281be12..8b5a1d86b7 100644 --- a/contentcuration/contentcuration/viewsets/websockets/consumers.py +++ b/contentcuration/contentcuration/viewsets/websockets/consumers.py @@ -84,6 +84,14 @@ def receive(self, text_data): from contentcuration.models import Channel from contentcuration.tasks import apply_channel_changes_task from contentcuration.tasks import apply_user_changes_task + + #If the message is PING message then just reply to keep connection alive + data = json.loads(text_data) + if "ping" in data: + self.send(json.dumps({ + 'response': "PONG!" + })) + return response_payload = { "disallowed": [], From dec515dfc19b9f3ef7641b97b0e363f4e7da7c78 Mon Sep 17 00:00:00 2001 From: ozer550 Date: Mon, 31 Oct 2022 01:19:30 +0530 Subject: [PATCH 2/4] fix linting --- .../frontend/shared/data/serverSync.js | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/contentcuration/contentcuration/frontend/shared/data/serverSync.js b/contentcuration/contentcuration/frontend/shared/data/serverSync.js index 687e63768e..0f13c522a0 100644 --- a/contentcuration/contentcuration/frontend/shared/data/serverSync.js +++ b/contentcuration/contentcuration/frontend/shared/data/serverSync.js @@ -28,7 +28,7 @@ import urls from 'shared/urls'; // change being registered, sync changes! const SYNC_IF_NO_CHANGES_FOR = 0.5; -// 25 seconds +// Set ping interval to 25 seconds const WEBSOCKET_PING_INTERVAL = 25000; let socket; @@ -40,7 +40,6 @@ const commonFields = ['type', 'key', 'table', 'rev', 'channel_id', 'user_id']; const objectFields = ['objs', 'mods']; const ignoredSubFields = [COPYING_FLAG, LAST_FETCHED, TASK_ID]; - const ChangeTypeMapFields = { [CHANGE_TYPES.CREATED]: commonFields.concat(['obj']), [CHANGE_TYPES.UPDATED]: commonFields.concat(['mods']), @@ -422,17 +421,18 @@ async function handleChanges(changes) { let intervalTimer; -// Skip the Pinging if we recently interacted with backend -var pingTimer = function () { - if(shouldWePing == false){ +// Skip the Pinging if we recently interacted with backend +var pingTimer = function() { + if (shouldWePing == false) { shouldWePing = true; return; } - socket.send(JSON.stringify({ - ping: "PING!", - })) -} - + socket.send( + JSON.stringify({ + ping: 'PING!', + }) + ); +}; export function startSyncing() { // Initiate a sync immediately in case any data @@ -450,11 +450,10 @@ export function startSyncing() { socket.addEventListener('open', () => { console.log('Websocket connected'); }); - + //Keep Pinging the websocket connection to keep connection alive setInterval(pingTimer, WEBSOCKET_PING_INTERVAL); - // Listen for any errors due to which connection may be closed. socket.addEventListener('error', event => { console.log('WebSocket error: ', event); From ad1c790f7d54c5c1f730872916d021f973630f4d Mon Sep 17 00:00:00 2001 From: ozer550 Date: Wed, 2 Nov 2022 23:30:23 +0530 Subject: [PATCH 3/4] use debouncing --- .../frontend/shared/data/serverSync.js | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/contentcuration/contentcuration/frontend/shared/data/serverSync.js b/contentcuration/contentcuration/frontend/shared/data/serverSync.js index 0f13c522a0..6887cc7c3a 100644 --- a/contentcuration/contentcuration/frontend/shared/data/serverSync.js +++ b/contentcuration/contentcuration/frontend/shared/data/serverSync.js @@ -32,7 +32,6 @@ const SYNC_IF_NO_CHANGES_FOR = 0.5; const WEBSOCKET_PING_INTERVAL = 25000; let socket; -let shouldWePing = true; // Flag to check if a sync is currently active. let syncActive = false; @@ -265,12 +264,12 @@ async function WebsocketSendChanges() { if (changes.length) { requestPayload.changes = changes; //set ping to false to reset ping timmer - shouldWePing = false; socket.send( JSON.stringify({ payload: requestPayload, }) ); + debouncePingMessage(); } } syncActive = false; @@ -376,6 +375,15 @@ const debouncedSyncChanges = debounce(() => { } }, SYNC_IF_NO_CHANGES_FOR * 1000); +const debouncePingMessage = debounce(() => { + socket.send( + JSON.stringify({ + ping: 'PING!', + }) + ); + debouncePingMessage(); +}, WEBSOCKET_PING_INTERVAL); + if (process.env.NODE_ENV !== 'production' && typeof window !== 'undefined') { window.forceServerSync = forceServerSync; } @@ -421,19 +429,6 @@ async function handleChanges(changes) { let intervalTimer; -// Skip the Pinging if we recently interacted with backend -var pingTimer = function() { - if (shouldWePing == false) { - shouldWePing = true; - return; - } - socket.send( - JSON.stringify({ - ping: 'PING!', - }) - ); -}; - export function startSyncing() { // Initiate a sync immediately in case any data // is left over in the database. @@ -452,7 +447,7 @@ export function startSyncing() { }); //Keep Pinging the websocket connection to keep connection alive - setInterval(pingTimer, WEBSOCKET_PING_INTERVAL); + debouncePingMessage(); // Listen for any errors due to which connection may be closed. socket.addEventListener('error', event => { From 8c1efd0ce4c72210c69e693c8f3af0d067db8b6a Mon Sep 17 00:00:00 2001 From: Blaine Jester Date: Wed, 21 Jun 2023 08:14:10 -0700 Subject: [PATCH 4/4] Correct formatting of websocket pong response --- .../contentcuration/viewsets/websockets/consumers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contentcuration/contentcuration/viewsets/websockets/consumers.py b/contentcuration/contentcuration/viewsets/websockets/consumers.py index 8b5a1d86b7..b3579cdd70 100644 --- a/contentcuration/contentcuration/viewsets/websockets/consumers.py +++ b/contentcuration/contentcuration/viewsets/websockets/consumers.py @@ -89,8 +89,8 @@ def receive(self, text_data): data = json.loads(text_data) if "ping" in data: self.send(json.dumps({ - 'response': "PONG!" - })) + 'response': "PONG!" + })) return response_payload = {