From 766d00c4d980959c18bb75d63652adab0a05f0e9 Mon Sep 17 00:00:00 2001 From: Erik Michelson Date: Fri, 21 Aug 2026 13:07:30 +0200 Subject: [PATCH] fix(realtime): proper reconnect handling for socket.io Socket.IO seems to include an own reconnect mechanism for some time. HedgeDoc however built an own reconnect mechanism on top of that. Since the 'reconnect' event was dropped for socket.io 4.x, the reconnect-cleanup code didn't run anymore. This commit fixes this by: - removing the manual reconnect interval, - putting the reconnect handler into the correct place, - setting reconnection options on the server side Signed-off-by: Erik Michelson --- app.js | 4 ++++ public/docs/release-notes.md | 2 +- public/js/index.js | 23 +++++++++-------------- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/app.js b/app.js index 269b13fc..75b6d46a 100644 --- a/app.js +++ b/app.js @@ -89,6 +89,10 @@ const io = new Server(server, { origin: config.serverURL, methods: ['GET', 'POST'], credentials: true + }, + connectionStateRecovery: { + maxDisconnectionDuration: 2 * 60 * 1000, + skipMiddlewares: false } }) diff --git a/public/docs/release-notes.md b/public/docs/release-notes.md index bdc1e1a8..00ce81e8 100644 --- a/public/docs/release-notes.md +++ b/public/docs/release-notes.md @@ -9,7 +9,7 @@ ### Bugfixes - All links will be opened in new tabs instead of the same tab as the note. This was changed when the external link warning was implemented and now the old behavior is restored. -- Fixed a bug in the realtime disconnect handler that could lead to data-loss of users with a flaky connection +- Fixed multiple bugs in the realtime connection handling that could lead to data-loss of users with a flaky connection ## 1.11.1 2026-07-24 diff --git a/public/js/index.js b/public/js/index.js index a3f27032..1f571850 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -2233,7 +2233,6 @@ socket.on('delete', function () { }) } }) -let retryTimer = null socket.on('maintenance', function () { cmClient.revision = -1 }) @@ -2246,21 +2245,17 @@ socket.on('disconnect', function (data) { if (!editor.getOption('readOnly')) { editor.setOption('readOnly', true) } - if (!retryTimer) { - retryTimer = setInterval(function () { - if (!needRefresh) socket.connect() - }, 1000) - } -}) -socket.on('reconnect', function (data) { - // sync back any change in offline - emitUserStatus(true) - cursorActivity(editor) - socket.emit('online users') }) socket.on('connect', function (data) { - clearInterval(retryTimer) - retryTimer = null + if (socket.recovered) { + // sync back any change in offline + console.debug('Reconnected client') + emitUserStatus(true) + cursorActivity(editor) + socket.emit('online users') + return + } + console.debug('Client connected (no reconnect)') personalInfo.id = socket.id showStatus(statusType.connected) socket.emit('version')