-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathon_message_handler.py
31 lines (22 loc) · 1.01 KB
/
on_message_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import json
import boto3
import os
dynamodb = boto3.client('dynamodb')
def handle(event, context):
messageType = json.loads(event['body'])['type']
messageData = json.loads(event['body'])['data']
messageId = json.loads(event['body'])['id']
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=json.dumps({"type": messageType, "data": messageData, "id": messageId}),
ConnectionId=connectionId['connectionId']['S']
)
return {}