'AWS API Gateway Websocket UnknownError
We are experiencing an error when doing a SDK postToConnection()
call as a promise, full error details given below. Other calls in the same function with a different connection ID happen successfully. Expected 410 connection errors are happening correctly and in milliseconds and are being handled gracefully.
This error then takes anything between 40 seconds to well over a minute to return, which causes it to always cause 'endpoint request timeout' errors in the Web socket API as it has a 30 second maximum request timeout. Has anybody experienced this issue before and/or any solution implemented? Any ideas to fix the issue will be highly appreciated, thanks.
UnknownError: Network error communicating with endpoint at Object.extractError (/opt/nodejs/node_modules/aws-sdk/lib/protocol/json.js:51:27)
Solution 1:[1]
are you trying to use postToConnection inside the connect handler? The websocket connection is only created after the connect handler has returned statusCode 200. You should not use postToConnection inside connect handler.
Solution 2:[2]
To avoid a 410 headache whilst employing websockets on serverless, don't forget to catch your exceptions:
export const ApiGatewayConnector = (event) => {
const endpoint = process.env.IS_OFFLINE
? 'http://localhost:3001'
: `${event.requestContext.domainName}/${event.requestContext.stage}`
const apiVersion = '2018-11-29'
return new AWS.ApiGatewayManagementApi({ apiVersion, endpoint })
}
....
if (event.requestContext.stage == 'local') {
await ApiGatewayConnector(event)
.postToConnection({ ConnectionId, Data })
.promise()
.catch(_ => removeId(ConnectionId));<----- N.B. Remove disconnected IDs
} else {
await ws.send(Data, ConnectionId)
}
}
Solution 3:[3]
That error is thrown when calling .postToConnection in answer to a $connect event. You can call .postConnection without errors in answer to a $default event.
// index.js
// the handler is defined as: index.handler
const AWS = require("aws-sdk");
exports.handler = function (event, context, callback) {
console.log('event.requestContext.eventType', event && event.requestContext && event.requestContext.eventType)
if (event.requestContext.eventType === "CONNECT") {
console.log('$connect event')
// calling apigwManagementApi.postToConnection will throw an exception
callback(null, {
statusCode: 200,
body: "Connected"
});
} else if (event.requestContext.eventType === "DISCONNECT") {
console.log('$disconnect event')
// calling apigwManagementApi.postToConnection is pointless since the client has disconneted
callback(null, {
statusCode: 200,
body: "Disconnected"
});
} else {
console.log('$default event')
const ConnectionId = event.requestContext.connectionId
const bodyString = event.body
const Data = bodyString
const apigwManagementApi = new AWS.ApiGatewayManagementApi({
apiVersion: "2018-11-29",
endpoint: event.requestContext.domainName + "/" + event.requestContext.stage
});
apigwManagementApi
.postToConnection({ ConnectionId, Data })
.promise().then(() => {
callback(null, {
statusCode: 200,
body: "Disconnected"
});
})
}
};
Solution 4:[4]
Not really sure if that follows the best practice, but I was able to use postToConnection
inside the connect
handler, (by calling the callback
before initiating postToConnection
)
exports.handler = async (event, context, callback) => {
try {
const {
body = '{}',
requestContext: { apiId, connectionId, routeKey, stage },
} = event;
const apigwManagementApi = new ApiGatewayManagementApi({
endpoint: 'local' === stage ? `http://localhost:4002` : `https://${apiId}.execute-api.${process.env.AWS_REGION}.amazonaws.com/${stage}`,
});
switch(routeKey) {
case '$connect':
// call callback to create connection
callback(null, {
statusCode: 200,
body: "Connected"
});
await apigwManagementApi.postToConnection({
ConnectionId: connectionId,
Data: JSON.stringify({
action: 'test-message'
}),
}).promise();
break;
case '$disconnect':
console.log('DISCONNECT');
break;
default:
console.log('DEFAULT');
break;
}
return { statusCode: 200 };
} catch (error) {
console.log('Got error', error)
}
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ascendzor |
Solution 2 | |
Solution 3 | Giorgio |
Solution 4 | Lafif Astahdziq |