A work-in-progress chat bot for Streamplace with chat overlay functionality
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

Jetstream heartbeat to prevent timeouts

+11 -5
+11 -5
utils/websocket.ts
··· 36 36 37 37 private connectToJetstream() { 38 38 const jetstreamUrl = JETSTREAM_URL; 39 - 40 39 this.jetstreamWs = new WebSocket(jetstreamUrl); 41 40 41 + let pingInterval: number | null = null; 42 + 42 43 this.jetstreamWs.onopen = () => { 43 44 console.log("Connected to Jetstream"); 45 + 46 + // Send a ping every 15 seconds to keep the connection alive 47 + pingInterval = setInterval(() => { 48 + if (this.jetstreamWs?.readyState === WebSocket.OPEN) { 49 + this.jetstreamWs.send(JSON.stringify({ type: "ping" })); 50 + } 51 + }, 15_000); 44 52 }; 45 53 46 54 this.jetstreamWs.onmessage = (event) => { 47 55 try { 48 - const jetstreamEvent: JetstreamEvent = JSON.parse( 49 - event.data, 50 - ); 51 - 56 + const jetstreamEvent: JetstreamEvent = JSON.parse(event.data); 52 57 this.eventHandler.handleEvent(jetstreamEvent); 53 58 } catch (error) { 54 59 console.error("Error processing jetstream message:", error); ··· 59 64 console.log( 60 65 "Jetstream connection closed, attempting to reconnect...", 61 66 ); 67 + if (pingInterval !== null) clearInterval(pingInterval); 62 68 setTimeout(() => this.connectToJetstream(), 5000); 63 69 }; 64 70