> For the complete documentation index, see [llms.txt](https://docs.videsk.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.videsk.io/es-developers/apps/ejemplos/eventos-en-vivo.md).

# Eventos en vivo

Este ejemplo cubre el [Patrón B](/es-developers/apps/broker.md#patron-b-broker-como-canal-continuo): la app conversa con el host durante toda la atención.

Casos típicos:

* La altura de tu UI cambia (un acordeón se abre, llega un resultado de búsqueda) y quieres que el iframe se ajuste.
* Tu app realiza una acción que el agente o un sistema externo quiere registrar como parte de la llamada (por ejemplo, "se creó un ticket"; "se actualizó el contacto").

## Auto-resize

Reporta la nueva altura cada vez que el contenido cambia, **no** en un setInterval.

```js
const sendHeight = () => {
  window.parent.postMessage({
    type: 'videsk:resize',
    height: document.documentElement.scrollHeight,
  }, '*');
};

new ResizeObserver(sendHeight).observe(document.documentElement);
window.addEventListener('load', sendHeight);
```

## Emitir un evento de negocio

```js
function onTicketCreated(ticket) {
  window.parent.postMessage({
    type: 'videsk:emit',
    event: 'ticket.created',
    payload: {
      id: ticket.id,
      url: ticket.url,
      priority: ticket.priority,
    },
  }, '*');
}
```

Reglas para `videsk:emit`:

* `event` es un string libre que tú defines. Usa nombres con namespace (`ticket.created`, `lead.updated`) para no chocar con futuros eventos del propio Videsk.
* `payload` debe ser serializable a JSON.
* No esperes respuesta del host: `videsk:emit` es *fire-and-forget*.

## Ejemplo completo

```html
<!doctype html>
<html>
<body>
  <button id="create">Crear ticket</button>
  <ul id="log"></ul>

  <script>
    const TRUSTED = ['https://console.videsk.io', 'https://app.videsk.io'];
    let ctx = null;

    const post = (msg) => window.parent.postMessage(msg, '*');

    const sendHeight = () => post({
      type: 'videsk:resize',
      height: document.documentElement.scrollHeight,
    });

    new ResizeObserver(sendHeight).observe(document.documentElement);

    document.getElementById('create').addEventListener('click', async () => {
      const ticket = await fakeCreateTicket(ctx);
      const li = document.createElement('li');
      li.textContent = `Ticket ${ticket.id}`;
      document.getElementById('log').appendChild(li);

      post({
        type: 'videsk:emit',
        event: 'ticket.created',
        payload: { id: ticket.id, callId: ctx.call?.id },
      });
    });

    window.addEventListener('message', (event) => {
      if (!TRUSTED.includes(event.origin)) return;
      if (event.data?.type !== 'videsk:init') return;
      ctx = event.data.context;
    });

    post({ type: 'videsk:ready' });

    async function fakeCreateTicket(ctx) {
      return { id: 'TCK-' + Math.random().toString(36).slice(2, 8) };
    }
  </script>
</body>
</html>
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.videsk.io/es-developers/apps/ejemplos/eventos-en-vivo.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
