Skip to content

Commit ff343ca

Browse files
committed
⚡️(frontend) increase polling interval depend connected users
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.
1 parent d7b1c8c commit ff343ca

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ and this project adheres to
1111

1212
## Added
1313

14-
🔧(helm) add option to disable default tls setting by @dominikkaminski #519
14+
- 🔧(helm) add option to disable default tls setting by @dominikkaminski #519
15+
- ✨Collaboration long polling fallback #517
1516

1617
## Changed
1718

src/frontend/apps/impress/src/features/docs/doc-management/api/syncDocPolling.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ interface SyncDocPollingParams {
99

1010
interface SyncDocPollingResponse {
1111
yDoc64?: Base64;
12+
connectionsCount: number;
1213
}
1314

1415
export const syncDocPolling = async ({

src/frontend/apps/impress/src/features/docs/doc-management/hooks/useCollaboration.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const useCollaboration = (room?: string, initialContent?: Base64) => {
1414
const { setBroadcastProvider } = useBroadcastStore();
1515
const { provider, createProvider, destroyProvider, isProviderFailure } =
1616
useProviderStore();
17-
const [pollingInterval] = useState(1500);
17+
const [pollingInterval, setPollingInterval] = useState(1500);
1818
const intervalRef = useRef<NodeJS.Timeout>();
1919

2020
useEffect(() => {
@@ -68,7 +68,9 @@ export const useCollaboration = (room?: string, initialContent?: Base64) => {
6868
yDoc64: toBase64(Y.encodeStateAsUpdate(provider.document)),
6969
})
7070
.then((response) => {
71-
const { yDoc64 } = response;
71+
const { yDoc64, connectionsCount } = response;
72+
73+
setPollingInterval(connectionsCount ? 600 : 1500);
7274

7375
if (!yDoc64) {
7476
return;

src/frontend/servers/y-provider/src/handlers/collaborationConnectionsHandler.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,16 @@ export const collaborationHTTPHandler = async (
4949
return;
5050
}
5151

52-
const syncYDoc64 = await syncDoc(room, yDoc64, canEdit, req);
52+
const { syncYDoc64, connectionsCount } = await syncDoc(
53+
room,
54+
yDoc64,
55+
canEdit,
56+
req,
57+
);
5358

5459
res.status(200).json({
5560
yDoc64: syncYDoc64,
61+
connectionsCount,
5662
});
5763
};
5864

@@ -69,6 +75,8 @@ const syncDoc = async (
6975
) => {
7076
let docExist = false;
7177
let syncYDoc = undefined;
78+
let connectionsCount = 0;
79+
7280
hocusPocusServer.documents.forEach((hpYDoc) => {
7381
if (hpYDoc.name !== room) {
7482
return;
@@ -77,6 +85,7 @@ const syncDoc = async (
7785
docExist = true;
7886

7987
const hpYDoc64 = toBase64(Y.encodeStateAsUpdate(hpYDoc));
88+
connectionsCount = hpYDoc.getConnectionsCount();
8089

8190
// If the document has not changed, return
8291
if (yDoc64 === hpYDoc64) {
@@ -117,5 +126,8 @@ const syncDoc = async (
117126
syncYDoc = hpYDoc.merge(ydoc);
118127
}
119128

120-
return syncYDoc && toBase64(Y.encodeStateAsUpdate(syncYDoc));
129+
return {
130+
syncYDoc64: syncYDoc && toBase64(Y.encodeStateAsUpdate(syncYDoc)),
131+
connectionsCount,
132+
};
121133
};

0 commit comments

Comments
 (0)