Modern blockchain development demands real-time data streaming, and the Websocket API plays a pivotal role in enabling live interactions between decentralized applications (dApps) and the underlying network. For developers building on OKTC—a high-performance blockchain compatible with Ethereum’s ecosystem—leveraging the Websocket API is essential for monitoring chain activity, tracking transactions, and responding instantly to on-chain events.
Built using the Cosmos SDK and powered by Tendermint Core as its consensus engine, OKTC inherits robust event-handling capabilities. However, to ensure full compatibility with Ethereum-based tooling and developer workflows, OKTC transforms native Tendermint events into Ethereum-standard formats through its Web3-compatible Websocket interface. This allows developers to use familiar Ethereum JSON-RPC PubSub methods while interacting seamlessly with the OKTC network.
👉 Discover how real-time blockchain data can power your dApp development.
Establishing a Websocket Connection
To begin receiving real-time updates from the OKTC network, you must first establish a Websocket connection. This is done by initializing the REST server with the --wsport flag. By default, the Websocket port is set to 8546, aligning with Ethereum's standard configuration for Web3 communication.
Once the node is running with Websocket support enabled, clients can connect using any standard Websocket client such as ws, or integrate directly via web3.js or ethers.js libraries in a frontend or backend environment.
This bridge between Tendermint’s event system and Ethereum’s PubSub API ensures that developers don’t need to learn new patterns—instead, they can apply existing knowledge of Ethereum’s real-time event model to build on OKTC.
Creating Subscriptions
Subscriptions are created using a standard RPC call with the method eth_subscribe. This initiates a persistent connection that pushes updates whenever relevant events occur on the blockchain.
Parameters
- Subscription name (required): Specifies the type of event to listen for.
- Optional arguments: Depending on the subscription type, filters such as contract addresses or topics can be applied.
Upon successful registration, the server returns a unique subscription ID, which is used later to manage or cancel the subscription.
Example Usage
{
"id": 1,
"method": "eth_subscribe",
"params": ["newHeads"]
}The response will include the assigned subscription ID:
{
"id": 1,
"result": "0xabc123..."
}This simple yet powerful mechanism enables applications to stay synchronized with the latest block headers, transaction logs, and pending activities without polling the network repeatedly.
Canceling Subscriptions
When a client no longer needs to receive updates, it should clean up resources by canceling the subscription using eth_unsubscribe.
Parameters
- Subscription ID: The identifier returned during the
eth_subscribecall.
The method returns a boolean indicating whether the cancellation was successful (true) or if the subscription didn’t exist (false).
Example Request
{
"id": 2,
"method": "eth_unsubscribe",
"params": ["0xabc123..."]
}Proper lifecycle management of subscriptions improves efficiency and reduces unnecessary load on both client and server sides.
Supported Subscription Types
OKTC supports several key subscription types that map directly to common Ethereum PubSub functionality. These allow developers to monitor different aspects of chain behavior in real time.
newHeads
Triggers a notification every time a new block header is added to the chain. This includes scenarios involving chain reorganizations, ensuring your application remains aware of canonical truth at all times.
Parameters
None required.
This subscription is ideal for syncing light clients, updating UIs with the latest block information, or triggering off-chain processing pipelines.
👉 Learn how real-time block tracking enhances dApp responsiveness.
logs
Delivers logs generated by smart contracts that match specified filter criteria. These logs are emitted when new blocks are imported and contain valuable insights such as token transfers, state changes, or user interactions.
In the event of a chain reorganization:
- Logs from reverted blocks are resent with the
removed: trueflag. - New logs from blocks in the reorganized chain are emitted normally.
This ensures consistency and prevents data loss during network instability.
Parameters
An optional JSON object containing:
- address: A single contract address or an array of addresses to filter by.
- topics: Up to four indexed event topics for precise log filtering (e.g., specific event signatures or parameters).
Use Case Example
Monitoring ERC-20 token transfers to a wallet:
{
"address": "0x123...",
"topics": [
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
]
}This level of granularity empowers analytics platforms, notification engines, and compliance tools.
newPendingTransactions
Streams transaction hashes of all transactions entering the node’s mempool—i.e., those pending confirmation. This is particularly useful for:
- Front-running detection systems
- Real-time transaction monitoring
- Gas optimization tools
Note: After a chain reorganization, transactions that were previously dropped may reappear if reintroduced into the pending state.
Parameters
None required.
Ideal for traders, arbitrage bots, and relayers needing immediate visibility into upcoming network activity.
syncing
Indicates whether the node is currently synchronizing with the network. Returns either:
falsewhen fully syncedtruewhen syncing begins- An object with sync progress details (e.g., current block, highest known block)
This helps monitor node health and readiness before relying on its data for critical operations.
Frequently Asked Questions (FAQ)
Q: Is OKTC’s Websocket API fully compatible with Ethereum tooling?
A: Yes. OKTC maps Tendermint events to Ethereum-standard JSON-RPC PubSub formats, allowing seamless integration with web3.js, ethers.js, and other Ethereum development tools.
Q: Can I filter logs by multiple contract addresses?
A: Absolutely. The logs subscription accepts an array of addresses in the filter object, enabling broad or targeted monitoring across several contracts.
Q: How does OKTC handle chain reorganizations in event delivery?
A: During reorgs, logs from removed blocks are resent with removed: true, while new logs from the canonical chain are pushed normally—ensuring accurate event tracking.
Q: Do I need special infrastructure to use Websocket subscriptions?
A: No. Any standard Websocket client can connect to port 8546. Most modern dApp frameworks support this natively.
Q: Are there rate limits on Websocket connections?
A: Public endpoints may impose connection limits. For production applications, consider running your own node or using dedicated access services.
Q: What happens if my client disconnects temporarily?
A: Upon reconnection, you’ll need to re-subscribe. The API does not buffer missed events, so transient disconnections may result in data gaps unless handled by your application logic.
Final Thoughts
Integrating with OKTC’s Websocket API unlocks real-time capabilities crucial for modern blockchain applications. Whether you're building a DeFi dashboard, NFT marketplace, or analytics engine, leveraging newHeads, logs, newPendingTransactions, and syncing subscriptions gives you granular control over on-chain data flow.
By maintaining Ethereum compatibility while harnessing the performance benefits of the Cosmos SDK and Tendermint consensus, OKTC offers developers the best of both worlds: speed, security, and familiarity.