aboutsummaryrefslogtreecommitdiffstats
path: root/lib/nwc.ts
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-03-29 23:40:15 -0700
committerAdam Soltys <[email protected]>2026-03-29 23:40:15 -0700
commit51428790ea558f8e34c3034e75d2e49a076261b8 (patch)
tree82a31a86b4e1cb3a185863e2976fab753b4350c7 /lib/nwc.ts
parentef06f2eb48581c201e62fc2cde27b106e898e54d (diff)
downloadcoinos-server-51428790ea558f8e34c3034e75d2e49a076261b8.tar.gz
coinos-server-51428790ea558f8e34c3034e75d2e49a076261b8.zip
nwc heartbeat
Diffstat (limited to 'lib/nwc.ts')
-rw-r--r--lib/nwc.ts29
1 files changed, 21 insertions, 8 deletions
diff --git a/lib/nwc.ts b/lib/nwc.ts
index 646ed4b0..d09e6e6c 100644
--- a/lib/nwc.ts
+++ b/lib/nwc.ts
@@ -49,12 +49,30 @@ const nwcRequestTimes: Map<string, number[]> = new Map();
export default () => {
let r: any;
+ let heartbeatInterval: any;
function connect() {
+ if (heartbeatInterval) clearInterval(heartbeatInterval);
r = new Relay("ws://sf:7777", { reconnect: false });
r.on("open", async (_) => {
l("nwc connected to strfry");
+
+ // Periodically check if connection is alive
+ heartbeatInterval = setInterval(() => {
+ try {
+ if (!r?.ws || r.ws.readyState !== 1) {
+ warn("nwc heartbeat: connection dead, reconnecting");
+ clearInterval(heartbeatInterval);
+ try { r.close(); } catch (_) {}
+ setTimeout(connect, 1000);
+ }
+ } catch (_) {
+ warn("nwc heartbeat: error, reconnecting");
+ clearInterval(heartbeatInterval);
+ setTimeout(connect, 1000);
+ }
+ }, 30000);
r.subscribe("nwc", { kinds: [23194], "#p": [serverPubkey, serverPubkey2], since: Math.floor(Date.now() / 1000) - nwcEventMaxAgeSeconds });
const info = await finalizeEvent(
{
@@ -87,6 +105,7 @@ export default () => {
r.on("close", () => {
warn("nwc strfry connection lost, reconnecting in 5s");
+ if (heartbeatInterval) clearInterval(heartbeatInterval);
setTimeout(connect, 5000);
});
@@ -147,16 +166,10 @@ export default () => {
response = await finalizeEvent(response, hexToBytes(sk));
r.send(["EVENT", response]);
} catch (e) {
- // err(
- // "problem with nwc",
- // pubkey,
- // method,
- // JSON.stringify(params),
- // e.message,
- // );
+ err("problem with nwc", pubkey, method, e.message);
}
} catch (e) {
- // err("problem with nwc", e.message);
+ err("problem with nwc event", e.message);
}
});
}