Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Aws-lex-web-ui] add heartbeat for streaming responses to prevent idle timeout of 10 mins #771

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions lex-web-ui/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -710,11 +710,11 @@ export default {

wsClient.onmessage = (event) => {
if(event.data!=='/stop/' && context.getters.isStartingTypingWsMessages()){
console.info("streaming ", context.getters.isStartingTypingWsMessages());
console.info('Streaming: ', context.getters.isStartingTypingWsMessages());
context.commit('pushWebSocketMessage',event.data);
context.dispatch('typingWsMessages')
}else{
console.info('stopping streaming');
console.info('Currently not streaming');
}
}
}
Expand Down Expand Up @@ -1304,6 +1304,41 @@ export default {

const signedUrl = Signer.signUrl(context.state.config.lex.streamingWebSocketEndpoint+'?sessionId='+sessionId, accessInfo, serviceInfo);
wsClient = new WebSocket(signedUrl);

// Add heartbeat logic
const HEARTBEAT_INTERVAL = 540000; // 9 minutes
const MAX_DURATION = 7200000; // 2 hours
const startTime = Date.now();
let heartbeatTimer = null;

function startHeartbeat() {
if (wsClient.readyState === WebSocket.OPEN) {
const elapsedTime = Date.now() - startTime;
if (elapsedTime < MAX_DURATION) {
const pingMessage = JSON.stringify({ action: 'ping' });
wsClient.send(pingMessage);
console.log('Sending Ping:', new Date().toISOString());
heartbeatTimer = setTimeout(startHeartbeat, HEARTBEAT_INTERVAL);
} else {
console.log('Stopped sending pings after reaching 2-hour limit.');
clearTimeout(heartbeatTimer);
}
}
}
wsClient.onopen = () => {
console.log('WebSocket Connected');
startHeartbeat();
};

wsClient.onclose = () => {
console.log('WebSocket Closed');
clearTimeout(heartbeatTimer);
};

wsClient.onerror = (error) => {
console.log('WebSocket Error', error.message);
clearTimeout(heartbeatTimer);
};
},
typingWsMessages(context){
if (context.getters.wsMessagesCurrentIndex()<context.getters.wsMessagesLength()-1){
Expand Down
23 changes: 23 additions & 0 deletions templates/streaming-support.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,34 @@ Resources:
IntegrationUri:
Fn::Sub:
arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${StreamingLambda.Arn}/invocations

PingRoute:
Type: AWS::ApiGatewayV2::Route
Properties:
ApiId: !Ref StreamingWebSocket
RouteKey: ping
OperationName: PingRoute
Target: !Join
- '/'
- - 'integrations'
- !Ref PingIntegration

PingIntegration:
Type: AWS::ApiGatewayV2::Integration
Properties:
ApiId: !Ref StreamingWebSocket
Description: Ping Integration
IntegrationType: MOCK
PassthroughBehavior: WHEN_NO_MATCH
RequestTemplates:
application/json: '{"statusCode":200}'


Deployment:
Type: AWS::ApiGatewayV2::Deployment
DependsOn:
- ConnectRoute
- PingRoute
Properties:
ApiId: !Ref StreamingWebSocket

Expand Down