Blockchain technology continues to evolve, and with it, the demand for real-time data access grows. For developers building decentralized applications (dApps) on the OKT Chain (OKTC), leveraging the WebSocket API is essential for achieving low-latency, event-driven interactions. Built on the Cosmos SDK and powered by Tendermint Core, OKTC offers native Web3 compatibility through Ethereum-style PubSub APIs—making it easier than ever to integrate with existing tooling.
This guide dives deep into how developers can establish WebSocket connections, create and manage subscriptions, and respond to blockchain events in real time.
Understanding OKTC’s WebSocket Architecture
OKTC inherits its consensus mechanism and event structure from Tendermint Core, while its modular framework is built using the Cosmos SDK. However, to ensure seamless interoperability with Ethereum-based tools and dApps, OKTC translates Tendermint events into Ethereum-compatible formats via the WebSocket PubSub API.
This means developers can use standard Ethereum clients and libraries—such as eth_subscribe and eth_unsubscribe—to listen for live blockchain updates directly from an OKTC node.
👉 Unlock real-time blockchain data with powerful WebSocket tools
Setting Up a WebSocket Connection
To begin receiving real-time events, you must first enable the WebSocket server when launching the REST server. This is done using the --wsport flag.
By default, the WebSocket port is set to 8546. You can start your node with:
oktc start --rpc.unsafe-cors --wsOnce running, connect to the WebSocket endpoint using any compliant client. Popular tools include:
ws– a lightweight CLI WebSocket clientwscat– useful for testing and debugging- Browser-based WebSocket libraries or Web3 providers like ethers.js
The connection URL typically follows this format:
ws://<your-node-ip>:8546Ensure your firewall allows traffic on this port and CORS policies are configured appropriately for web applications.
Creating Subscriptions with eth_subscribe
Real-time event listening begins with creating a subscription using the eth_subscribe RPC method. This call returns a unique subscription ID, which you’ll use later to cancel or track the stream.
Parameters
- Subscription name – the type of event to listen for
- Optional filter parameters – depends on the subscription type
Example: Subscribe to New Block Headers
{
"id": 1,
"method": "eth_subscribe",
"params": ["newHeads"]
}If successful, the response will include a subscription ID:
{
"id": 1,
"result": "0xabc123..."
}From now on, every time a new block header is added to the chain—including during reorganizations—you’ll receive a notification.
Canceling Subscriptions with eth_unsubscribe
When you no longer need to receive updates, clean up resources by unsubscribing.
Use the eth_unsubscribe method with the subscription ID as the only parameter.
Parameters
- Subscription ID – returned from
eth_subscribe
Example: Cancel a Subscription
{
"id": 2,
"method": "eth_unsubscribe",
"params": ["0xabc123..."]
}Response:
{ "id": 2, "result": true }A true result confirms successful cancellation.
Supported Subscription Types
OKTC supports several key subscription types that align with Ethereum’s PubSub standards, enabling robust monitoring capabilities.
newHeads
Triggers whenever a new block header is appended to the blockchain. This includes cases of chain reorganization, ensuring your app stays synchronized with the canonical chain.
Parameters
None
Example Output
{
"jsonrpc": "2.0",
"method": "eth_subscription",
"params": {
"subscription": "0xabc123...",
"result": {
"number": "0x1b4",
"hash": "0x...",
"parentHash": "0x...",
"timestamp": "0x..."
}
}
}Use this for tracking block production, syncing status, or triggering actions upon new block finality.
logs
Receives logs from smart contracts that match specific filter criteria. Ideal for tracking token transfers, NFT mints, or contract state changes.
Parameters
An optional JSON object containing:
- address – single address or array of addresses to filter logs from
- topics – array of topic hashes (e.g., event signatures)
Example: Listen for Specific Contract Events
{
"id": 3,
"method": "eth_subscribe",
"params": [
"logs",
{
"address": "0x8320fe7702b96808f7bbc0d4a888ed1468216cfd",
"topics": ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"]
}
]
}🔔 During chain reorganizations, logs previously emitted from the old chain are resent with "removed": true. New logs from the updated chain are then pushed. This ensures consistency but requires handling duplicate events in your application logic.newPendingTransactions
Streams transaction hashes of all transactions entering the pending state—i.e., those not yet included in a block but broadcasted to the network.
Perfect for mempool monitoring, frontrunning detection, or real-time analytics.
Parameters
None
Example Output
{
"jsonrpc": "2.0",
"method": "eth_subscription",
"params": {
"subscription": "0xabc123...",
"result": "0x..."
}
}Note: Transactions that were dropped due to reorganization may reappear if they’re reintroduced into the pending pool.
syncing
Monitors synchronization status of the node. Useful for dashboards or backend health checks.
Returns either:
false– if the node is fully syncedtrue– if syncing is in progress- An object with sync progress details (e.g., current block, highest known block)
Parameters
None
Example Response
{
"startingBlock": "0x100",
"currentBlock": "0x1b4",
"highestBlock": "0x200"
}👉 Monitor blockchain sync status in real time with advanced WebSocket features
Frequently Asked Questions (FAQ)
What is the default WebSocket port for OKTC?
The default port is 8546. You can change it using the --wsport flag when starting the node.
Can I filter logs by multiple contract addresses?
Yes. Use the address field in the logs subscription and provide an array of contract addresses.
How does OKTC handle chain reorganizations in event streams?
During reorgs, old events (like logs or headers) are resent with a "removed": true flag. New events from the updated chain are then emitted. Your app should handle both cases to maintain accuracy.
Is authentication required to connect via WebSocket?
No. However, it’s recommended to secure your node behind authentication or a reverse proxy in production environments to prevent abuse.
Can I use ethers.js with OKTC’s WebSocket provider?
Absolutely. ethers.js supports custom WebSocket providers. Just initialize it with your OKTC node’s WebSocket URL:
const provider = new ethers.providers.WebSocketProvider("ws://localhost:8546");Are there rate limits on WebSocket connections?
While there are no built-in rate limits, excessive connections or subscriptions may impact node performance. Always implement proper connection management and error retries.
Final Thoughts
The OKTC WebSocket API bridges the gap between Cosmos-based architecture and Ethereum-compatible developer tooling. With support for real-time subscriptions like newHeads, logs, and newPendingTransactions, developers can build responsive, event-driven dApps that react instantly to blockchain activity.
Whether you're building a wallet, analytics dashboard, or DeFi platform, mastering WebSocket integration is a critical step toward delivering a seamless user experience.
👉 Start building real-time dApps on OKTC today
Core Keywords: WebSocket API, OKTC, real-time blockchain data, eth_subscribe, Tendermint Core, Cosmos SDK, Ethereum compatibility, blockchain event streaming