Enable real-time chat message streaming in Amazon Connect
Amazon Connect Chat provides APIs that enable you to subscribe to a real-time stream of chat messages. Using these APIs, you can:
-
Stream chat messages in real time when a new chat contact is created.
-
Extend the current Amazon Connect Chat functionality to support use cases like building integrations with SMS solutions and third-party messaging applications, enabling mobile push notifications, and creating analytics dashboards to monitor and track chat message activity.
How the message streaming APIs work
The Amazon Connect message streaming APIs are triggered when certain events occur within an Amazon Connect Chat contact. For example, when a customer sends a new chat message, the event sends a payload to a specified endpoint containing data about the message that was just sent. Messages are published using Amazon Simple Notification Service (Amazon SNS) to a specific endpoint.
This topic describes how to set up real-time message streaming using Amazon Connect and Amazon SNS. The steps are:
-
Use the Amazon SNS console to create a new standard SNS topic and set up the messages.
-
Call the StartChatContact API to initiate the chat contact.
-
Call the StartContactStreaming API to initiate message streaming.
-
Call the CreateParticipantConnection API to create the participant's connection.
Step 1: Create a standard SNS topic
-
Go to the Amazon SNS console.
-
Create a SNS topic in your AWS account. In the Details section, for Type, choose Standard, enter a name for the topic, and then choose Create topic.
Note
Currently, the message streaming APIs only support standard SNS for real-time streaming of messages. They don't support Amazon SNS FIFO (first in, first out) topics.
-
After you create the topic, its Amazon Resource Name (ARN) is displayed in the Details section. Copy the topic ARN to the clipboard. You'll use the topic ARN in the next step, and in Step 3: Enable message streaming on the contact.
The topic ARN looks similar to the following example:
arn:aws:sns:
us-east-1
:123456789012
:MyTopic
-
Choose the Access policy tab, choose Edit, and then add a resource-based policy on the SNS topic so that Amazon Connect has permission to publish to it. Following is a sample SNS policy that you can copy and paste into the JSON editor, and then customize with your values:
{ "Version":"2012-10-17", "Statement":[ { "Effect":"Allow", "Principal":{ "Service":"connect.amazonaws.com" }, "Action":"sns:Publish", "Resource":"
YOUR_SNS_TOPIC_ARN
", "Condition":{ "StringEquals":{ "aws:SourceAccount":"YOUR_AWS_ACCOUNT_ID
" }, "ArnEquals":{ "aws:SourceArn":"YOUR_CONNECT_INSTANCE_ARN
" } } } ] }Note
The default Access policy comes with conditions applied to
sourceOwner
such as:"Condition": { "StringEquals": { "AWS:SourceOwner": "921772911154" } }
Make sure you remove it and replace with
SourceAccount
, for example:"Condition":{ "StringEquals":{ "aws:SourceAccount":"YOUR_AWS_ACCOUNT_ID" }, "ArnEquals":{ "aws:SourceArn":"YOUR_CONNECT_INSTANCE_ARN" } }
This prevents a cross-service confused deputy issue.
-
If you're using server-side encryption on SNS, verify you have
connect.amazonaws.com
permission enabled on the KMS key. Following is a sample policy:{ "Version": "2012-10-17", "Id": "key-consolepolicy-3", "Statement": [ { "Sid": "Enable IAM User Permissions", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::
your_accountId
:root", "Service": "connect.amazonaws.com" }, "Action": "kms:*", "Resource": "*" }, { "Sid": "Allow access for Key Administrators", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::your_accountId
:root", "Service": "connect.amazonaws.com" }, "Action": [ "kms:Create*", "kms:Describe*", "kms:Enable*", "kms:List*", "kms:Put*", "kms:Update*", "kms:Revoke*", "kms:Disable*", "kms:Get*", "kms:Delete*", "kms:TagResource", "kms:UntagResource", "kms:ScheduleKeyDeletion", "kms:CancelKeyDeletion" ], "Resource": "*" } ] }
Step 2: Initiate the chat contact
-
Call the Amazon Connect StartChatContact API to initiate the chat contact.
For information about how to create the SDK client for calling Amazon Connect APIs, see the following topics:
-
Keep track of
ContactId
andParticipantToken
from the StartChatContact response since these response attributes are used for calling other chat APIs required to enable streaming. This is described in the next steps.
Step 3: Enable message streaming on the contact
-
Call StartContactStreaming to enable real-time message streaming to your SNS topic.
-
Limits: You can subscribe to up to two SNS topics per contact.
-
When you call StartContactStreaming, you'll need to provide the Amazon Resource Name (ARN) of the SNS topic (see Step 1: Create a standard SNS topic).
A single SNS topic ARN may be used across multiple AWS accounts, but it must be in the same Region as your Amazon Connect instance. For example, if your topic ARN is in us-east-1, your Amazon Connect instance must be in us-east-1.
-
For initial chat messages that aren't received on the streaming endpoint, you can call the GetTranscript API to receive the initial messages.
-
Step 4: Create the participant connection
-
Call CreateParticipantConnection with the
ConnectParticipant
attribute passed as true.-
You must call CreateParticipantConnection within five minutes of creating the chat.
-
Calling CreateParticipantConnection with
ConnectParticipant
set to true only works if you enabled streaming in Step 2: Initiate the chat contact and caller participant isCustomer
. -
This step (creating the participant connection) is optional if you have already successfully connected to the chat contact using
WEBSOCKET
.
-
Next steps
You are all set for working with the message streaming APIs.
-
To verify it is working, check that messages are published to the SNS topic you created. You can do this using Amazon CloudWatch metrics. For instructions, see Monitoring Amazon SNS topics using CloudWatch.
-
Because SNS has limited retention
, we recommend that you set up Amazon Simple Queue Service (Amazon SQS) Amazon Kinesis , or another service to retain messages. -
Using StopContactStreaming is optional and not required if the chats are being disconnected through a contact flow, or if the customer disconnects the chat. However,
StopContactStreaming
provides the option to stop the message streaming on the SNS topic, even if the chat is active and ongoing.