SDK (Socket.IO)

You can download the TronSocket SDK from NPM using the command below and follow the steps to listen to Tron Blockchain events and transactions. But first you need to create a token.

How to create a token

NOTE:

Please log the error with the key value “messageLimitPerSecond” in the error handle process because we don't disconnect you when you exceed your message per second limit. we just send a message. However, you may miss some events during this interval. So either listen to these events by filtering them or upgrade your plan.

You can also assume that every time you receive this message you miss an event.

Install SDK

npm i @tronsocket/sdk

Event Types

There are a total of 4 events in the TronSocket Event Listener API and you can listen to them. “transaction” and ‘block’ return only one type of data while ‘contract’ and ‘solidity’ return log or event data. There are helper methods in the SDK to detect them.

import { EventTypes } from '@tronsocket/sdk/node'

type Events = keyof EventTypes; // 'block', 'transaction', 'contract', 'solidity'

How to Use

Listening to events with the TronSocket SDK is very easy, all you need to do is enter your token as the 1st parameter and set “testnet” true or false in the 2nd parameter and you can easily listen to events on the Nile Testnet network or the Tron Mainnet network.

First of all, you must make sure that the connection is established using the “connect” method.

Also, if you are not getting errors with the Socket.IO polling feature in your current development process. you can enable polling by passing the true parameter to the connect method.

import TronSocket, { Transaction /** types */ } from '@tronsocket/sdk/node'

const socket = new TronSocket('your_token_here', /* testnet */ true)

const polling: bool = false // optional, default: false

// Connect to API
await socket.connect(polling).then((connected) => {
    console.log("Connected:", connected);
}).catch((error) => {
    console.error("Error:", error);
});

// For disconnect
await socket.disconnect()
import TronSocket, {
    Transaction,
    Block,
    Solidity,
    SolidityEvent,
    SolidityLog,
    ContractEvent,
    ContractLog,
} from "@tronsocket/sdk/node";

const socket = new TronSocket("your_token_here", true);

await socket.connect().then((connected) => {
    console.log("Connected:", connected);
}).catch((error) => {
    console.error("Error:", error);
});

socket.on("transaction", (tx: Transaction) => {
    const id = tx.transactionId;
});

socket.on("block", (block: Block) => {
    console.log(block);
});

socket.on("solidity", (solidity: Solidity | SolidityEvent | SolidityLog) => {
    if (socket.isSolidityEvent(solidity)) {
        console.log(solidity); // SolidityEvent
    } else if (socket.isSolidityLog(solidity)) {
        console.log(solidity); // SolidityLog
    } else {
        console.log(solidity); // Solidity
    }
});

socket.on("contract", (contract: ContractEvent | ContractLog) => {
    if (socket.isContractEvent(contract)) {
        console.log(contract); // ContractEvent
    } else if (socket.isContractLog(contract)) {
        console.log(contract); // ContractLog
    }
});

Yes, you can listen to events, blocks and transactions on the Tron Blockchain network with WebSocket with just the above lines.

Use with Filter

In the example above, we didn't add any filters, which means that you will listen to all events moment by moment on the blockchain network. But in such a case, you need a lot of usage limits, right? So you can only listen to certain transactions or events with filters. Take a look at the examples below.

In the example below you can only listen to transactions between two addresses.

import TronSocket, {
    Transaction,
    TransactionFilter,
} from "@tronsocket/sdk/node";

const socket = new TronSocket("your_token_here", true);

await socket.connect().then((connected) => {
    console.log("Connected:", connected);
}).catch((error) => {
    console.error("Error:", error);
});

const transaction = (data: Transaction) => {
    console.log(data);
}

const transactionFilter: TransactionFilter = {
    toAddress: "TJr3QjX...",
    fromAddress: "TJr3QjX..."
}

socket.subscribe("transaction", transaction, transactionFilter); // Subscribe to transaction events with the filter

On & Off

There are basically two methods you will use in the SDK. These are “on” and “off”. I assume that every developer already knows them. You can also use “subscribe” and “unsubscribe” methods as long version.

In the “off”, i.e. unscribe process, if you are listening to the event with filter, please do not forget to send the filter parameter.

import TronSocket, {
    Transaction,
    TransactionFilter,
} from "@tronsocket/sdk/node";

const socket = new TronSocket("your_token_here", true);

await socket.connect().then((connected) => {
    console.log("Connected:", connected);
}).catch((error) => {
    console.error("Error:", error);
});

const transaction = (data: Transaction) => {
    console.log(data);
    socket.off("transaction", transaction, transactionFilter);
    // socket.unsubscribe("transaction", transaction, transactionFilter);
}

const transactionFilter: TransactionFilter = {
    toAddress: "TJr3QjX...",
    fromAddress: "TJr3QjX..."
}

socket.on("transaction", transaction, transactionFilter);
// socket.subscribe("transaction", transaction, transactionFilter);

Handle Error

You can use the “error” function for all error processes and see below what each code means.

import TronSocket from "@tronsocket/sdk/node";

const socket = new TronSocket("your_token_here", true);

type EmitError = {
    message: string
    code: string
    key?: string | number // specific key for errors
}

await socket
    .connect()
    .then((connected) => {
        console.log("Connected:", connected);
    })
    .catch((error) => {
        console.error("Error:", error);
    });

socket.error((error: EmitError) => {
    console.error("Error:", error);
})

401: Unauthorized

403: Forbidden

409: Conflict

429: Too Many Requests

500: Internal Server Error

Last updated