Created
September 25, 2019 07:06
-
-
Save SumindaD/7df5932d53240af580894a4daa013840 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| import boto3 | |
| import os | |
| dynamodb = boto3.client('dynamodb') | |
| def handle(event, context): | |
| message = json.loads(event['body'])['message'] | |
| paginator = dynamodb.get_paginator('scan') | |
| connectionIds = [] | |
| apigatewaymanagementapi = boto3.client('apigatewaymanagementapi', | |
| endpoint_url = "https://" + event["requestContext"]["domainName"] + "/" + event["requestContext"]["stage"]) | |
| # Retrieve all connectionIds from the database | |
| for page in paginator.paginate(TableName=os.environ['SOCKET_CONNECTIONS_TABLE_NAME']): | |
| connectionIds.extend(page['Items']) | |
| # Emit the recieved message to all the connected devices | |
| for connectionId in connectionIds: | |
| apigatewaymanagementapi.post_to_connection( | |
| Data=message, | |
| ConnectionId=connectionId['connectionId']['S'] | |
| ) | |
| return {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment