Skip to content

Commit

Permalink
⚡️(frontend) increase polling interval depend connected users
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AntoLC committed Dec 24, 2024
1 parent d7b1c8c commit ff343ca
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface SyncDocPollingParams {

interface SyncDocPollingResponse {
yDoc64?: Base64;
connectionsCount: number;
}

export const syncDocPolling = async ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<NodeJS.Timeout>();

useEffect(() => {
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
};

Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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,
};
};

0 comments on commit ff343ca

Please sign in to comment.