I am trying to use AWS Lambda, along with API Gateway to send some JSON strings between clients.
Currently, I can only get it to send a single value, for example:
{"action":"sendMessage", "message": "hello"}
But i would really like to be able to send something like:
{"action":"sendMessage", "message": {"status1": "hello"}}
And for the sent message to be: {“status1”: “hello”}
Unfortunately I’m stuck on the first example, of only being able to send “hello”, nothing else passes.
This is code I’m using for my websocket send:
import json
import boto3
import os
dynamodb = boto3.client('dynamodb')
def lambda_handler(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"]
)
for page in paginator.paginate(TableName=os.environ['WEBSOCKET_TABLE']):
connectionIds.extend(page['Items'])
for connectionId in connectionIds:
apigatewaymanagementapi.post_to_connection(
Data=message,
ConnectionId=connectionId['connectionId']['S']
)
return {}
Dan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.