From ff343ca5a8899aec6426957860ff08950c06cc92 Mon Sep 17 00:00:00 2001 From: Anthony LC Date: Tue, 24 Dec 2024 16:21:22 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F(frontend)=20increase=20polli?= =?UTF-8?q?ng=20interval=20depend=20connected=20users?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If we see that someone is connected to the document, we increase the polling interval. If no one is connected, we decrease the polling interval. For the moment, we can only see users connected with websockets. A good improvement would be to see users connected with long polling as well. --- CHANGELOG.md | 3 ++- .../docs/doc-management/api/syncDocPolling.tsx | 1 + .../doc-management/hooks/useCollaboration.tsx | 6 ++++-- .../handlers/collaborationConnectionsHandler.ts | 16 ++++++++++++++-- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e168ea54..6daceb1c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,8 @@ and this project adheres to ## Added -🔧(helm) add option to disable default tls setting by @dominikkaminski #519 +- 🔧(helm) add option to disable default tls setting by @dominikkaminski #519 +- ✨Collaboration long polling fallback #517 ## Changed diff --git a/src/frontend/apps/impress/src/features/docs/doc-management/api/syncDocPolling.tsx b/src/frontend/apps/impress/src/features/docs/doc-management/api/syncDocPolling.tsx index d8d12d287..bd2c24e3a 100644 --- a/src/frontend/apps/impress/src/features/docs/doc-management/api/syncDocPolling.tsx +++ b/src/frontend/apps/impress/src/features/docs/doc-management/api/syncDocPolling.tsx @@ -9,6 +9,7 @@ interface SyncDocPollingParams { interface SyncDocPollingResponse { yDoc64?: Base64; + connectionsCount: number; } export const syncDocPolling = async ({ diff --git a/src/frontend/apps/impress/src/features/docs/doc-management/hooks/useCollaboration.tsx b/src/frontend/apps/impress/src/features/docs/doc-management/hooks/useCollaboration.tsx index 3b8639fa8..245c833f4 100644 --- a/src/frontend/apps/impress/src/features/docs/doc-management/hooks/useCollaboration.tsx +++ b/src/frontend/apps/impress/src/features/docs/doc-management/hooks/useCollaboration.tsx @@ -14,7 +14,7 @@ export const useCollaboration = (room?: string, initialContent?: Base64) => { const { setBroadcastProvider } = useBroadcastStore(); const { provider, createProvider, destroyProvider, isProviderFailure } = useProviderStore(); - const [pollingInterval] = useState(1500); + const [pollingInterval, setPollingInterval] = useState(1500); const intervalRef = useRef(); useEffect(() => { @@ -68,7 +68,9 @@ export const useCollaboration = (room?: string, initialContent?: Base64) => { yDoc64: toBase64(Y.encodeStateAsUpdate(provider.document)), }) .then((response) => { - const { yDoc64 } = response; + const { yDoc64, connectionsCount } = response; + + setPollingInterval(connectionsCount ? 600 : 1500); if (!yDoc64) { return; diff --git a/src/frontend/servers/y-provider/src/handlers/collaborationConnectionsHandler.ts b/src/frontend/servers/y-provider/src/handlers/collaborationConnectionsHandler.ts index 8f1db5b8c..b5b718a88 100644 --- a/src/frontend/servers/y-provider/src/handlers/collaborationConnectionsHandler.ts +++ b/src/frontend/servers/y-provider/src/handlers/collaborationConnectionsHandler.ts @@ -49,10 +49,16 @@ export const collaborationHTTPHandler = async ( return; } - const syncYDoc64 = await syncDoc(room, yDoc64, canEdit, req); + const { syncYDoc64, connectionsCount } = await syncDoc( + room, + yDoc64, + canEdit, + req, + ); res.status(200).json({ yDoc64: syncYDoc64, + connectionsCount, }); }; @@ -69,6 +75,8 @@ const syncDoc = async ( ) => { let docExist = false; let syncYDoc = undefined; + let connectionsCount = 0; + hocusPocusServer.documents.forEach((hpYDoc) => { if (hpYDoc.name !== room) { return; @@ -77,6 +85,7 @@ const syncDoc = async ( docExist = true; const hpYDoc64 = toBase64(Y.encodeStateAsUpdate(hpYDoc)); + connectionsCount = hpYDoc.getConnectionsCount(); // If the document has not changed, return if (yDoc64 === hpYDoc64) { @@ -117,5 +126,8 @@ const syncDoc = async ( syncYDoc = hpYDoc.merge(ydoc); } - return syncYDoc && toBase64(Y.encodeStateAsUpdate(syncYDoc)); + return { + syncYDoc64: syncYDoc && toBase64(Y.encodeStateAsUpdate(syncYDoc)), + connectionsCount, + }; };