Skip to content

Commit 9be7f24

Browse files
committed
Try sending a message on every websocket every 30s
Maybe this will stop the *.recurse.com websocket closure every 55 seconds?
1 parent 38efd38 commit 9be7f24

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

index.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -710,17 +710,32 @@ app.ws("/websocket", async function (ws, req) {
710710
),
711711
);
712712
};
713+
const keepWsAliveListener = () => {
714+
// TODO: How to send an empty message that doesn't conflict with HTMX?
715+
ws.send("<div data-reason='empty-message-to-keepalive-websocket'></div>");
716+
};
713717

714718
emitter.on("room-change", roomListener);
715719
emitter.on("in-the-hub-change", inTheHubListener);
720+
emitter.on("keep-ws-alive", keepWsAliveListener);
716721

717722
// If client closes connection, stop sending events
718723
ws.on("close", () => {
719724
emitter.off("room-change", roomListener);
720725
emitter.off("in-the-hub-change", inTheHubListener);
726+
emitter.off("keep-ws-alive", keepWsAliveListener);
721727
});
722728
});
723729

730+
// After seeing reference to another service with a 60 second idle timeout for
731+
// websocket connections (https://stackoverflow.com/a/48764819), I thought I
732+
// might try the same solution and see if that fixed the issue despite this
733+
// being on a different stack.
734+
// TODO: Test and see if the websocket still closes after 55 seconds
735+
setInterval(() => {
736+
emitter.emit("keep-ws-alive");
737+
}, 1000 * 30);
738+
724739
const roomNameToNote = {};
725740
app.post(
726741
"/note",
@@ -799,7 +814,7 @@ app.post(
799814
});
800815

801816
const botCreated = await apiResult.json();
802-
console.log("Bot created with ID #" + botCreated.id);
817+
// console.log("Bot created with ID #" + botCreated.id);
803818
if (!botCreated.id) {
804819
console.log("Bot creation failed somehow");
805820
console.log(apiResult.status);

0 commit comments

Comments
 (0)