Skip to content

Instantly share code, notes, and snippets.

@archishou
Last active June 27, 2019 17:10
Show Gist options
  • Save archishou/8dd768637ac1f4db40c521a605fa86c6 to your computer and use it in GitHub Desktop.
Save archishou/8dd768637ac1f4db40c521a605fa86c6 to your computer and use it in GitHub Desktop.
const AWS = require('aws-sdk');
const ddb = new AWS.DynamoDB.DocumentClient();
require('./patch.js');
let send = undefined;
function init(event) {
console.log(event);
const apigwManagementApi = new AWS.ApiGatewayManagementApi({
apiVersion: '2018-11-29',
endpoint: event.requestContext.domainName + '/' + event.requestContext.stage
});
send = async(connectionId, data) => {
await apigwManagementApi.postToConnection({ ConnectionId: connectionId, Data: `Echo: ${data}` }).promise();
};
}
exports.handler = (event, context, callback) => {
init(event);
let message = JSON.parse(event.body).message;
getConnections().then((data) => {
console.log(data.Items);
data.Items.forEach(function(connection) {
console.log("Connection " + connection.connectionid);
send(connection.connectionid, message);
});
});
return {};
};
function getConnections() {
return ddb.scan({ TableName: 'dl_telemetry', }).promise();
}
require('aws-sdk/lib/node_loader');
var AWS = require('aws-sdk/lib/core');
var Service = AWS.Service;
var apiLoader = AWS.apiLoader;
apiLoader.services['apigatewaymanagementapi'] = {};
AWS.ApiGatewayManagementApi = Service.defineService('apigatewaymanagementapi', ['2018-11-29']);
Object.defineProperty(apiLoader.services['apigatewaymanagementapi'], '2018-11-29', {
get: function get() {
var model = {
"metadata": {
"apiVersion": "2018-11-29",
"endpointPrefix": "execute-api",
"signingName": "execute-api",
"serviceFullName": "AmazonApiGatewayManagementApi",
"serviceId": "ApiGatewayManagementApi",
"protocol": "rest-json",
"jsonVersion": "1.1",
"uid": "apigatewaymanagementapi-2018-11-29",
"signatureVersion": "v4"
},
"operations": {
"PostToConnection": {
"http": {
"requestUri": "/@connections/{connectionId}",
"responseCode": 200
},
"input": {
"type": "structure",
"members": {
"Data": { "type": "blob" },
"ConnectionId": {
"location": "uri",
"locationName": "connectionId"
}
},
"required": ["ConnectionId", "Data"],
"payload": "Data"
}
}
},
"shapes": {}
}
model.paginators = { "pagination": {} }
return model;
},
enumerable: true,
configurable: true
});
module.exports = AWS.ApiGatewayManagementApi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment