Skip to content

Instantly share code, notes, and snippets.

@SumindaD
Created September 25, 2019 07:06
Show Gist options
  • Save SumindaD/7df5932d53240af580894a4daa013840 to your computer and use it in GitHub Desktop.
Save SumindaD/7df5932d53240af580894a4daa013840 to your computer and use it in GitHub Desktop.
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