WebSocket API for OKTC Blockchain

Introduction to OKTC WebSocket Integration

OKTC leverages Tendermint Core as its consensus engine and utilizes the Cosmos SDK framework, inheriting its event format. To ensure native Web3 compatibility with Ethereum’s PubSub API, OKTC converts Tendermint responses into Ethereum-compatible types. This enables seamless WebSocket interactions for developers familiar with Ethereum’s ecosystem.

To establish a connection:
1. Initialize the REST server with the --wsport flag (default port: 8546)
2. Use WebSocket clients like ws or similar alternatives

👉 Explore blockchain development tools

Mainnet WebSocket Connection

The primary endpoint for production environments:
WebSocket URL: wss://mainnet.oktc.io/ws (secured connection recommended)

Creating Subscriptions

Initiate subscriptions using RPC calls with eth_subscribe method:

Subscription Parameters

  1. Subscription Name (required): Type of event to monitor
  2. Optional Arguments: Additional filters or parameters

json
// Example Request
{
"id": 1,
"method": "eth_subscribe",
"params": ["newHeads"]
}

Canceling Subscriptions

Terminate active subscriptions using eth_unsubscribe:

Cancellation Parameters

  1. Subscription ID: Identifier received during subscription creation

json
// Example Request
{
"id": 1,
"method": "eth_unsubscribe",
"params": ["0x9cef478923ff08bf67fde6c64013158d"]
}

Supported Subscription Types

1. New Block Headers (newHeads)

Triggers notifications for:
– New blocks added to the chain
– Chain reorganization events

Parameters: None required

2. Event Logs (logs)

Monitors contract events matching specified criteria:
– Returns logs from new blocks
– Resends logs during chain reorganizations (marked with removed: true)

Log Filters

json
{
"address": ["0x123..."], // Single or array
"topics": ["0x456..."] // Event signature hashes
}

👉 Master smart contract events

3. Pending Transactions (newPendingTransactions)

Notifies about:
– New transactions entering mempool
– Transactions affected by chain reorganizations

Parameters: None required

4. Synchronization Status (syncing)

Reports node synchronization state:
true: Sync started
false: Sync completed
– Object: Detailed progress metrics

Parameters: None required

FAQ Section

Q: What WebSocket ports does OKTC support?

A: The default WebSocket port is 8546, configurable via --wsport during server initialization.

Q: How does OKTC handle chain reorganizations?

A: For logs subscriptions, previously sent logs are re-sent with removed: true during reorgs.

Q: Can I filter logs by multiple contract addresses?

A: Yes, provide an array of addresses in the address parameter of logs subscription.

Q: What’s the difference between newHeads and newPendingTransactions?

A: newHeads tracks confirmed blocks, while newPendingTransactions monitors unconfirmed transactions in mempool.

Q: How often does the syncing status update?

A: Updates occur at each synchronization milestone (typically every 5-10% progress).

Q: Are WebSocket connections persistent?

A: Yes, maintain the connection to receive continuous updates until unsubscribed.

Advanced Implementation Notes

Feature Tendermint Native Web3 Compatibility Layer
Event Format ABCI Ethereum-style
Subscription Types 4 4 (mapped)
Reorg Handling Automatic Manual resending

For optimal performance:
1. Use compression if available
2. Implement reconnection logic
3. Batch requests when possible
4. Monitor connection health