0
votes

We have implemented API Gateway web socket (with serverless AWS Lambda) and it works well. However, we have encountered a critical issue explained below and would highly appreciate if anybody can provide a solution / workaround:

We want to sync server time with client/javascript time and wanted to use this AWS API gateway web sockets to send timestamp to all connected clients at regular intervals. However, we found that without an input action from a client, we cannot push data to the clients automatically. Is there any workaround / fix for this issue?

Thanks in advance,

1
It's unclear what you mean by "push data to the clients automatically". You can push data to connected clients spontaneously (not in reaction to a client event) of course... but what does "automatically" mean?Michael - sqlbot
AWS team confirmed that this is a limitation In a pure serverless environment, but suggested to use CloudWatch to trigger lambda function periodically which sorted out the issue, thanks.krishna

1 Answers

0
votes

You can do that using the connection URL of your WebSocket API. The connections URL looks like https://{api-id}.execute-api.{region}.amazonaws.com/{stage}.

To push data to the connected clients you can use the Amazon API Gateway Management API postToConnection() operation. Here is the reference: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/ApiGatewayManagementApi.html#getConnection-property

var params = {
  ConnectionId: 'STRING_VALUE', /* required */
  Data: Buffer.from('...') || 'STRING_VALUE' /* Strings will be Base-64 encoded on     your behalf */ /* required */
};
apigatewaymanagementapi.postToConnection(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});