> 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). Agents can also search and read this documentation through its [MCP server](https://docs.videsk.io/~gitbook/mcp).

# Eventos en vivo

Auto-resize del iframe y emisión de eventos de negocio al host usando el broker como canal continuo.

Este ejemplo cubre el [Patrón B](https://docs.videsk.io/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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
