From 5d16376d850fa9a2c767be668d3c6ff97f3664fd Mon Sep 17 00:00:00 2001 From: Benjamin Frueh Date: Tue, 23 Sep 2025 11:20:59 +0200 Subject: [PATCH 1/2] Fix WebSocket race condition by adding readyState check before send() Signed-off-by: Benjamin Frueh --- lib/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.ts b/lib/index.ts index 9e75c2a..bdedd1b 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -57,7 +57,7 @@ export function listen(name: string, handler: (string, any) => void, options: No } window._notify_push_listeners[name].push(handler); - if (window._notify_push_ws !== null && typeof window._notify_push_ws === "object") { + if (window._notify_push_ws !== null && typeof window._notify_push_ws === "object" && window._notify_push_ws.readyState === WebSocket.OPEN) { window._notify_push_ws.send('listen ' + name); } else { setupSocket(options); From fa363d219deb7197e3ef0b7d2abe2a3e14132876 Mon Sep 17 00:00:00 2001 From: Benjamin Frueh Date: Wed, 24 Sep 2025 17:33:58 +0200 Subject: [PATCH 2/2] Add a flag to track if notify_push is ready instead of relying on the Webservice readyState Signed-off-by: Benjamin Frueh --- lib/index.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/index.ts b/lib/index.ts index bdedd1b..b8df635 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -13,6 +13,7 @@ declare global { _notify_push_online: boolean, _notify_push_available: boolean, _notify_push_error_count: number, + _notify_push_ready: boolean, } } @@ -57,7 +58,7 @@ export function listen(name: string, handler: (string, any) => void, options: No } window._notify_push_listeners[name].push(handler); - if (window._notify_push_ws !== null && typeof window._notify_push_ws === "object" && window._notify_push_ws.readyState === WebSocket.OPEN) { + if (window._notify_push_ws !== null && typeof window._notify_push_ws === "object" && window._notify_push_ready) { window._notify_push_ws.send('listen ' + name); } else { setupSocket(options); @@ -73,10 +74,12 @@ function setupGlobals(options: NotifyPushOptions = {}) { window._notify_push_online = true; window._notify_push_available = false; window._notify_push_error_count = 0; + window._notify_push_ready = false; subscribe('networkOffline', () => { window._notify_push_online = false; window._notify_push_ws = null; + window._notify_push_ready = false; }); subscribe('networkOnline', () => { window._notify_push_error_count = 0; @@ -127,6 +130,8 @@ async function setupSocket(options: NotifyPushOptions = {}) { window._notify_push_ws.send(options.credentials.password) } + window._notify_push_ready = true; + for (let name in window._notify_push_listeners) { window._notify_push_ws.send('listen ' + name); } @@ -154,6 +159,7 @@ async function setupSocket(options: NotifyPushOptions = {}) { window._notify_push_ws.onerror = window._notify_push_ws.onclose = () => { window._notify_push_ws = null; window._notify_push_error_count += 1; + window._notify_push_ready = false; setTimeout(() => { if (window._notify_push_online) {