Skip to content

Commit

Permalink
[Filing] Websocket - ping server every 60s to keep connection alive
Browse files Browse the repository at this point in the history
  • Loading branch information
meissadia committed Apr 13, 2023
1 parent cdc6dae commit 4ae6283
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/filing/actions/listenForProgress.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
} from '../constants/statusCodes.js'
import * as AccessToken from '../../common/api/AccessToken.js'

let keepSocketAlive

// Extract completion percentage
export const parseProgress = string => {
if (!string.match(/^InProgress/)) return string
Expand Down Expand Up @@ -86,6 +88,14 @@ export default function listenForProgress() {
},
})
)

// Keep connection alive by pinging server every 60s
keepSocketAlive = setInterval(() => {
const timestamp = new Date().toLocaleString('en-US', {
timeZone: 'America/New_York',
})
socket.send(JSON.stringify({ keepAlive: `${timestamp} ET` }))
}, 60000)
}

// Listen for messages
Expand Down Expand Up @@ -152,6 +162,8 @@ export default function listenForProgress() {
// event.code is usually 1006 in this case
console.log('[socket onclose] Connection died', event)
}

clearInterval(keepSocketAlive)
}

// TODO: What to do on websocket error?
Expand Down

0 comments on commit 4ae6283

Please sign in to comment.