Skip to content

Receive messages from TronLink

The message is sent using "window.postMessage", and the content received by the Dapp is a MessageEvent.You can refer to MDN documentation of MessageEvent.

Account Change Message

Message ID: accountsChanged

Overview

This message is generated when:

  1. Users log in

  2. Users switch accounts

  3. Users lock accounts

  4. The wallet is automatically locked after timeout

Specification

Example



    window.addEventListener('message', function (e) {
      if (e.data.message && e.data.message.action === "accountsChanged") {
        // handler logic
        console.log('got accountsChanged event', e.data)
      }
    })

Returns



    interface MessageEventAccountsChangedData {
      isTronLink: boolean;
      message: {
        action: string;
        data: {
          address: string | boolean;
        }
      }
    }

Return value example

  1. When users log in, the content of the message body is:


    {
      "data": {
        "address": "TZ5XixnRyraxJJy996Q1sip85PHWuj4793" // Last selected account address
      }
    }
  1. When users switch accounts, the content of the message body is:
    {
      "data": {
        "address": "TRKb2nAnCBfwxnLxgoKJro6VbyA6QmsuXq" // Newly selected account address
      }
    }
  1. When users lock accounts and the wallet is automatically locked due to timeout, the message body content is:
    {
      "data": {
        "address": false
      }
    }

Network Change Message

Message ID: setNode

Overview

Developers can monitor this message to know network changes

This message is generated when:

  1. When the user changes the network

Specification

Example



    window.addEventListener('message', function (e) {
      if (e.data.message && e.data.message.action == "setNode") {
        // handler logic
        console.log('got setNode event', e.data)
      }
    })

Returns



    {
      "node": {
        // Information about the current network
      },
      "connectNode": {
        // Node information of DApp chain
      }
    }

Successful connection message

Message ID: connect

Overview

Developers can monitor this message for connection changes.

This message is generated when:

  1. The DApp requests a connection, and the user confirms the connection in the pop-up window

  2. Users connect to the website

Specification

Example



    window.addEventListener('message', function (e) {
      if (e.data.message && e.data.message.action == "connect") {
        // handler logic
        console.log('got connect event', e.data)
      }
    })

Disconnect website message

Message ID: disconnect

Overview

Developers can monitor this message for connection changes.

This message is generated when:

  1. The DApp requests a connection, and the user rejects the connection in the pop-up window

  2. Users disconnect from the website

Specification

Example



    window.addEventListener('message', function (e) {
      if (e.data.message && e.data.message.action == "disconnect") {
        // handler logic
        console.log('got connect event', e.data)
      }
    })

Messages to Be Deprecated

  1. The user rejects connection: “rejectWeb”

  2. The user disconnects from the website: “disconnectWeb”

  3. The user accepts connection: “acceptWeb”

  4. The user requests to connect to the website: “connectWeb”

User rejects connection

DEPRECATED

Message ID: rejectWeb

This message is generated when:

  1. The DApp requests a connection and the user rejects the connection in the pop-up window.

image

Developers can get the connection rejection message by listening to it:


    window.addEventListener('message', function (e) {
      if (e.data.message && e.data.message.action == "rejectWeb") {
        // handler logic
        console.log('got rejectWeb event', e.data)
      }
    })

User disconnects from the website

DEPRECATED

Message ID: disconnectWeb

This message is generated when:

  1. User actively disconnect from the website.

image

Developers can get the disconnection message by listening to it:



    window.addEventListener('message', function (e) {
      if (e.data.message && e.data.message.action == "disconnectWeb") {
        // handler logic
        console.log('got disconnectWeb event', e.data)
      }
    })

User accepts connection

DEPRECATED

Message ID: acceptWeb

This message is generated when:

  1. The user accepts connection.

image

Developers can get the connection acceptance message by listening to it:

    window.addEventListener('message', function (e) {
      if (e.data.message && e.data.message.action == "acceptWeb") {
        // handler logic
        console.log('got acceptWeb event', e.data)
      }
    })

User requests to connect to the website

DEPRECATED

Message ID: connectWeb

This message is generated when:

  1. The user requests to connect to the website.

image

Developers can get the connection request message by listening to it:

    window.addEventListener('message', function (e) {
      if (e.data.message && e.data.message.action == "connectWeb") {
        // handler logic
        console.log('got connectWeb event', e.data)
      }
    })