Why does Amazon Connect websocket text include backslashes in the content? Is this a bug?

0

I am working with the Amazon Connect Participant Service API - https://docs.aws.amazon.com/connect-participant/latest/APIReference/working-with-acps-api.html https://docs.aws.amazon.com/connect-participant/latest/APIReference/API_CreateParticipantConnection.html

I was able to use it to get a websocket connection URL - https://docs.aws.amazon.com/connect-participant/latest/APIReference/API_CreateParticipantConnection.html#API_CreateParticipantConnection_ResponseSyntax

However, upon connecting to that websocket URL, I found that the messages that arrive on that websocket connection have backslashes in the content. I checked this with both Java and JavaScript websocket implementations to rule out the possibility that I am doing something wrong in receiving these messages.

Normally, in Java code, I can easily remove these extra backslashes using something like -

String backslashRemovedContent = content.replace("\\", "");

However, this approach is naive and if the agent in Amazon Connect himself types a message with a double quote in it, then this logic breaks!.

The following example shows the sample payloads received.

Here is a sample script I used to connect to an Amazon Connect websocket -

<html>
   <head><title>Welcome to Websocket Test</title>

<body>

<script>
  let websocketUrl = "wss://tufsuyburufn.transport.connect.us-east-1.amazonaws.com/?AuthToken=QVFJREFIaElkeDhub3M0czFVWnlwd2NrUExuaFc2VFplaEtlVlVBSDBhcXpjTTVycUFFd0VraG1yN2Frbmd0NTZkYjdYQ1U4QUFBQWJqQnNCZ2txaGtpRzl3MEJCd2FnWHpCZEFnRUFNRmdHQ1NxR1NJYjNEUUVIQVRBZUJnbGdoa2dCWlFNRUFTNHdFUVFNRTdncVZJalZycmhFdVdTM0FnRVFnQ3R0RWl0RXp2Y2hOYTRLUmNtZWxhdTh5cWF3WFJzMWdKYWcwU3BFbHFIS084Wm80NlNScFFVSjBmRVU6OldGeEdGcllxYlZoVnZMTDFpWVdGNzJuT2RrRkxkcmtaNThMaEV5Mnk0SnA3Y1I4WFN4ZDFWV055dHVQb2orbU1BRUxxZGM3L1lldUswWWNLd1dQc1czMVkrUHhkZmFSMFFrSzN6RE8yOXp1b1ptYTUvUk45ZDByWElLR2o2c1BGN0tEenJZV3NaQ1ZuMWIxQzBXOVRSdzB6UlJTSmVHY3NtV3JTN1liZmlmTDkzNmtuU1BlcVVycHpSUGt2THFvc0ZGUlJTZHc9";
  const ws = new WebSocket(websocketUrl);
  ws.onopen = () => {
     console.log('Websocket opened! Subscribing...');
     ws.send('{"topic":"aws/subscribe","content":{"topics":["aws/chat"]}}');
  }

  ws.onmessage = (message) => {
    console.log("Got message.");
    console.log(message);
  }
</script>

</body>

</html>

In this example, the agent first types 'hi' and then types 'hi "'

You can see the backslashes in the message received on the websocket.

The output is as follows - WebSocketTest.html:10 Websocket opened! Subscribing... WebSocketTest.html:15 Got message. WebSocketTest.html:16 MessageEvent WebSocketTest.html:15 Got message. WebSocketTest.html:16 MessageEvent WebSocketTest.html:15 Got message. WebSocketTest.html:16 MessageEvent {isTrusted: true, data: '{"content":"{\\"AbsoluteTime\\":\\"2023-12-06T20:45:2…ntentType":"application/json","topic":"aws/chat"}', origin: 'wss://tufsuyburufn.transport.connect.us-east-1.amazonaws.com', lastEventId: '', source: null, …}isTrusted: truebubbles: falsecancelBubble: falsecancelable: falsecomposed: falsecurrentTarget: WebSocket {url: 'wss://tufsuyburufn.transport.connect.us-east-1.ama…TN1liZmlmTDkzNmtuU1BlcVVycHpSUGt2THFvc0ZGUlJTZHc9', readyState: 1, bufferedAmount: 0, onerror: null, onopen: ƒ, …}data: "{\"content\":\"{\\\"AbsoluteTime\\\":\\\"2023-12-06T20:45:26.546Z\\\",\\\"ContentType\\\":\\\"application/vnd.amazonaws.connect.event.participant.joined\\\",\\\"Id\\\":\\\"43f3c6db-6117-4737-9892-662964ee6625\\\",\\\"Type\\\":\\\"EVENT\\\",\\\"ParticipantId\\\":\\\"89fd1e06-831c-4c3e-84d1-62ae0cc7e6e5\\\",\\\"DisplayName\\\":\\\"Wesley\\\",\\\"ParticipantRole\\\":\\\"AGENT\\\",\\\"InitialContactId\\\":\\\"6b44a62b-eb5e-48b8-8edc-7b2c2fc00a47\\\",\\\"ContactId\\\":\\\"6b44a62b-eb5e-48b8-8edc-7b2c2fc00a47\\\"}\",\"contentType\":\"application/json\",\"topic\":\"aws/chat\"}"defaultPrevented: falseeventPhase: 0lastEventId: ""origin: "wss://tufsuyburufn.transport.connect.us-east-1.amazonaws.com"ports: []returnValue: truesource: nullsrcElement: WebSocket {url: 'wss://tufsuyburufn.transport.connect.us-east-1.ama…TN1liZmlmTDkzNmtuU1BlcVVycHpSUGt2THFvc0ZGUlJTZHc9', readyState: 1, bufferedAmount: 0, onerror: null, onopen: ƒ, …}target: WebSocket {url: 'wss://tufsuyburufn.transport.connect.us-east-1.ama…TN1liZmlmTDkzNmtuU1BlcVVycHpSUGt2THFvc0ZGUlJTZHc9', readyState: 1, bufferedAmount: 0, onerror: null, onopen: ƒ, …}timeStamp: 9761.699999988079type: "message"userActivation: null[[Prototype]]: MessageEvent WebSocketTest.html:15 Got message. WebSocketTest.html:16 MessageEvent {isTrusted: true, data: '{"content":"{\\"AbsoluteTime\\":\\"2023-12-06T20:47:4…ntentType":"application/json","topic":"aws/chat"}', origin: 'wss://tufsuyburufn.transport.connect.us-east-1.amazonaws.com', lastEventId: '', source: null, …} "agent Types hi" 'agent Types hi' WebSocketTest.html:15 Got message. WebSocketTest.html:16 MessageEvent {isTrusted: true, data: '{"content":"{\\"AbsoluteTime\\":\\"2023-12-06T20:47:5…ntentType":"application/json","topic":"aws/chat"}', origin: 'wss://tufsuyburufn.transport.connect.us-east-1.amazonaws.com', lastEventId: '', source: null, …}isTrusted: truebubbles: falsecancelBubble: falsecancelable: falsecomposed: falsecurrentTarget: WebSocket {url: 'wss://tufsuyburufn.transport.connect.us-east-1.ama…TN1liZmlmTDkzNmtuU1BlcVVycHpSUGt2THFvc0ZGUlJTZHc9', readyState: 1, bufferedAmount: 0, onerror: null, onopen: ƒ, …}data: "{\"content\":\"{\\\"AbsoluteTime\\\":\\\"2023-12-06T20:47:59.593Z\\\",\\\"Content\\\":\\\"hi\\\",\\\"ContentType\\\":\\\"text/plain\\\",\\\"Id\\\":\\\"6b5958e9-facc-4ebe-b2be-c81bb5e8379c\\\",\\\"Type\\\":\\\"MESSAGE\\\",\\\"ParticipantId\\\":\\\"89fd1e06-831c-4c3e-84d1-62ae0cc7e6e5\\\",\\\"DisplayName\\\":\\\"Wesley\\\",\\\"ParticipantRole\\\":\\\"AGENT\\\",\\\"InitialContactId\\\":\\\"6b44a62b-eb5e-48b8-8edc-7b2c2fc00a47\\\",\\\"ContactId\\\":\\\"6b44a62b-eb5e-48b8-8edc-7b2c2fc00a47\\\"}\",\"contentType\":\"application/json\",\"topic\":\"aws/chat\"}"defaultPrevented: falseeventPhase: 0lastEventId: ""origin: "wss://tufsuyburufn.transport.connect.us-east-1.amazonaws.com"ports: []returnValue: truesource: nullsrcElement: WebSocket {url: 'wss://tufsuyburufn.transport.connect.us-east-1.ama…TN1liZmlmTDkzNmtuU1BlcVVycHpSUGt2THFvc0ZGUlJTZHc9', readyState: 1, bufferedAmount: 0, onerror: null, onopen: ƒ, …}target: WebSocket {url: 'wss://tufsuyburufn.transport.connect.us-east-1.ama…TN1liZmlmTDkzNmtuU1BlcVVycHpSUGt2THFvc0ZGUlJTZHc9', readyState: 1, bufferedAmount: 0, onerror: null, onopen: ƒ, …}timeStamp: 162815.19999998808type: "message"userActivation: null[[Prototype]]: MessageEvent 'agent types Hi with a " in it' 'agent types Hi with a " in it' WebSocketTest.html:15 Got message. WebSocketTest.html:16 MessageEvent {isTrusted: true, data: '{"content":"{\\"AbsoluteTime\\":\\"2023-12-06T20:48:5…ntentType":"application/json","topic":"aws/chat"}', origin: 'wss://tufsuyburufn.transport.connect.us-east-1.amazonaws.com', lastEventId: '', source: null, …} WebSocketTest.html:15 Got message. WebSocketTest.html:16 MessageEvent {isTrusted: true, data: '{"content":"{\\"AbsoluteTime\\":\\"2023-12-06T20:48:5…ntentType":"application/json","topic":"aws/chat"}', origin: 'wss://tufsuyburufn.transport.connect.us-east-1.amazonaws.com', lastEventId: '', source: null, …}isTrusted: truebubbles: falsecancelBubble: falsecancelable: falsecomposed: falsecurrentTarget: WebSocket {url: 'wss://tufsuyburufn.transport.connect.us-east-1.ama…TN1liZmlmTDkzNmtuU1BlcVVycHpSUGt2THFvc0ZGUlJTZHc9', readyState: 1, bufferedAmount: 0, onerror: null, onopen: ƒ, …}data: "{\"content\":\"{\\\"AbsoluteTime\\\":\\\"2023-12-06T20:48:59.822Z\\\",\\\"Content\\\":\\\"hi \\\\\\\"\\\",\\\"ContentType\\\":\\\"text/plain\\\",\\\"Id\\\":\\\"7fae0e4d-f5c4-4c42-82e9-016490a19528\\\",\\\"Type\\\":\\\"MESSAGE\\\",\\\"ParticipantId\\\":\\\"89fd1e06-831c-4c3e-84d1-62ae0cc7e6e5\\\",\\\"DisplayName\\\":\\\"Wesley\\\",\\\"ParticipantRole\\\":\\\"AGENT\\\",\\\"InitialContactId\\\":\\\"6b44a62b-eb5e-48b8-8edc-7b2c2fc00a47\\\",\\\"ContactId\\\":\\\"6b44a62b-eb5e-48b8-8edc-7b2c2fc00a47\\\"}\",\"contentType\":\"application/json\",\"topic\":\"aws/chat\"}"defaultPrevented: falseeventPhase: 0lastEventId: ""origin: "wss://tufsuyburufn.transport.connect.us-east-1.amazonaws.com"ports: []returnValue: truesource: nullsrcElement: WebSocket {url: 'wss://tufsuyburufn.transport.connect.us-east-1.ama…TN1liZmlmTDkzNmtuU1BlcVVycHpSUGt2THFvc0ZGUlJTZHc9', readyState: 1, bufferedAmount: 0, onerror: null, onopen: ƒ, …}target: WebSocket {url: 'wss://tufsuyburufn.transport.connect.us-east-1.ama…TN1liZmlmTDkzNmtuU1BlcVVycHpSUGt2THFvc0ZGUlJTZHc9', readyState: 1, bufferedAmount: 0, onerror: null, onopen: ƒ, …}timeStamp: 223057.89999999106type: "message"userActivation: null[[Prototype]]: MessageEvent

So, is this a bug in the Amazon Connect implementation? Or is this the intended format and I have to find a way around it?

The documentation doesn't provide an example of the websocket messages so I cannot tell for sure.

Please help.

asked 5 months ago158 views
2 Answers
0

No, I don't think this is a bug. The backslashes in the received messages are due to JSON encoding and this is an expected behaviour of the message received on the websocket.

To handle this, you should parse the JSON string received from the WebSocket connection. In JavaScript, you can use JSON.parse() to convert the JSON string into a JavaScript object, which will automatically handle the escaping and remove the backslashes. You can refer the below example snippet.

ws.onmessage = (event) => {
  const message = JSON.parse(event.data);
  console.log("Got message", message);
};
AWS
Srinath
answered 5 months ago
0

OK. Thanks for confirming.

answered 5 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions