0

I am trying to use chat application on Amazon AWS using IVS resources. I am using CreateChatToken.php and index.html pages that I wrote below. However,I saw these two errors in console: Uncaught DOMException: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state. and WebSocket connection to 'ws://edge.ivschat.eu-west-1.amazonaws.com/' failed I'm trying to open my index.html file on the browser on chrome. I tried changing the region on the code and replacing wss with ws but the errors I got did not change.

(This page is CreateChatToken.php)

<?php

include "./vendor/autoload.php";
$accessKey = "<ACCESS KEY EXAMPLE>";
$secretKey="<SECRET KEY EXAMPLE>";
$region = "eu-west-1";
$awsclient = new Aws\ivschat\ivschatClient([
'version' => 'latest',
 'region'  => $region,
'credentials' => array(
  'key'    => $accessKey,
 'secret' => $secretKey,
    )
]);
 'roomIdentifier' => '<ROOM IDENTIFIER EXAMPLE>',
 'userId' => '11111',
]);
var_dump($result);

(This page is index.html)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    const chatClientToken = "<CHAT TOKEN EXAMPLE>";
    const socket = "wss://edge.ivschat.eu-west-1.amazonaws.com";
const connection = new WebSocket(socket, chatClientToken);
    const payload = {
        "Action": "SEND_MESSAGE",
        "RequestId": "OPTIONAL_ID_YOU_CAN_SPECIFY_TO_TRACK_THE_REQUEST",
        "Content": "text message",
        "Attributes": {
            "CustomMetadata": "test metadata"
}
}

    connection.send(JSON.stringify(payload));
    connection.onmessage = (event) => {
        const data = JSON.parse(event.data);
        displayMessages({
            display_name: data.Sender.Attributes.DisplayName,
            message: data.Content,
            timestamp: data.SendTime
        });


}


    function displayMessages(message) {
        console.log(message);
    }
</script>
</html>
Kuti7
  • 1
  • 1
  • That PHP code doesn't look correct. Did you remove some of the code, copy/paste mishap or is that your actual code? If so, it should throw syntax errors because of the `'roomIdentifier' => ...` lines which doesn't belong to an array. Also, where is that `$result` variable defined? You also have `JSON.parse(event.data)`, but the PHP you're showing doesn't return any JSON data. – M. Eriksson Sep 12 '22 at 10:55
  • As the error message already told you "`Still in CONNECTING state`" - i.e. first make a successful connection then use the `->send(...)` method. – Top-Master Sep 12 '22 at 11:45
  • Does this answer your question? [PHP request to PHP Websocket](https://stackoverflow.com/questions/27960064/php-request-to-php-websocket) – Top-Master Sep 12 '22 at 11:46
  • Unfortunately It does not.I did all steps without failure but ı still have same errors. – Kuti7 Sep 14 '22 at 06:17

0 Answers0