

# Paso 4: enviar y recibir su primer mensaje
<a name="getting-started-chat-send-and-receive"></a>

Utilice el token de chat para conectarse a una sala de chat y enviar su primer mensaje. A continuación, se proporciona un código JavaScript de prueba. Los SDK de los clientes de IVS también están disponibles: consulte [SDK de chat: guía para Android](chat-sdk-android.md), [SDK de chat: guía para iOS](chat-sdk-ios.md) y [SDK de chat: guía para JavaScript](chat-sdk-js.md).

**Servicio regional:** el código de prueba que aparece a continuación se refiere a la “región de elección admitida”. El chat de Amazon IVS ofrece puntos de conexión regional que puede utilizar para realizar sus solicitudes. Para la API de mensajería de chat de Amazon IVS, la sintaxis general de un punto de conexión regional es:
+ wss://edge.ivschat.<region-code>.amazonaws.com

Por ejemplo, el punto de conexión de la región Oeste de EE. UU. (Oregón) es wss://edge.ivschat.us-west-2.amazonaws.com. Para obtener una lista de las regiones admitidas, consulte la información de chat de Amazon IVS en [Página de Amazon IVS](https://docs.aws.amazon.com/general/latest/gr/ivs.html) en la *Referencia general de AWS*.

```
/*
1. To connect to a chat room, you need to create a Secure-WebSocket connection
using the client token you created in the previous steps. Use one of the provided 
endpoints in the Chat Messaging API, depending on your AWS region.
*/
const chatClientToken = "GENERATED_CHAT_CLIENT_TOKEN_HERE";
const socket = "wss://edge.ivschat.us-west-2.amazonaws.com"; // Replace “us-west-2” with supported region of choice.
const connection = new WebSocket(socket, chatClientToken);

/*
2. You can send your first message by listening to user input 
in the UI and sending messages to the WebSocket connection.
*/
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));

/*
3. To listen to incoming chat messages from this WebSocket connection 
and display them in your UI, you must add some event listeners.
*/
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) {
  // Modify this function to display messages in your chat UI however you like.
  console.log(message);
}

/*
4. Delete a chat message by sending the DELETE_MESSAGE action to the WebSocket 
connection. The connected user must have the "DELETE_MESSAGE" permission to 
perform this action.
*/

function deleteMessage(messageId) {
  const deletePayload = {
    "Action": "DELETE_MESSAGE",
    "Reason": "Deleted by moderator",
    "Id": "${messageId}"
  }
  connection.send(deletePayload);
}
```

¡Enhorabuena, está listo\! Ahora tiene una aplicación de chat sencilla que puede enviar o recibir mensajes.