# Event-based Communication Between dApp and the Elsa Widget.

{% stepper %}
{% step %}

### Architecture

* Widget (npm package)
  * React component integrated by the DApp.
  * Does not directly access wallets.
  * Sends generic wallet requests to the DApp.
* DApp (Host)
  * Integrates the widget package.
  * Has wallet provider logic (EVM, Solana, Bitcoin, Aptos, etc).
  * Listens for widget events and responds.
* MessageChannel
  * Dedicated communication channel between widget and DApp code.
    {% endstep %}

{% step %}

### Communication Flow

* DApp initializes MessageChannel
* DApp passes port to widget during initialization
* Widget sends request via MessagePort
* DApp handles request → performs wallet action
* DApp sends response back via MessagePort
  {% endstep %}

{% step %}

### Security Benefits

* Same-Origin Isolation: MessageChannel creates isolated communication ports that only work within the same origin/context.
* No Global Namespace Pollution: Unlike window\.postMessage, MessageChannel doesn't expose communication to the global scope.
* Direct Port-to-Port Communication: Creates a private channel between two specific endpoints.
* Structured Cloning: Messages are safely cloned, preventing reference sharing vulnerabilities.
* Type-Safe Communication: Your implementation uses typed interfaces (WalletRequest, WalletResponse) for additional safety.
  {% endstep %}
  {% endstepper %}


---

# Agent Instructions: 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:

```
GET https://docs.heyelsa.ai/elsa-widget/event-based-communication-between-dapp-and-the-elsa-widget..md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
