WebSocket API
WebSocket APIs provide real-time subscriptions to blockchain events.
Endpoints
CometBFT WebSocket
| Network | URL |
|---|---|
| Sprintnet | wss://rpc.sprintnet.monolythium.com:26657/websocket |
| Testnet | wss://rpc.testnet.monolythium.com:26657/websocket |
| Mainnet | wss://rpc.monolythium.com:26657/websocket |
EVM WebSocket
| Network | URL |
|---|---|
| Sprintnet | wss://rpc.sprintnet.monolythium.com:8546 |
| Testnet | wss://rpc.testnet.monolythium.com:8546 |
| Mainnet | wss://rpc.monolythium.com:8546 |
CometBFT Subscriptions
Subscribe to Events
{
"jsonrpc": "2.0",
"method": "subscribe",
"params": {
"query": "tm.event='NewBlock'"
},
"id": 1
}
Event Types
| Event | Query |
|---|---|
| New block | tm.event='NewBlock' |
| New block header | tm.event='NewBlockHeader' |
| Transaction | tm.event='Tx' |
| Validator set updates | tm.event='ValidatorSetUpdates' |
Filtered Subscriptions
{
"jsonrpc": "2.0",
"method": "subscribe",
"params": {
"query": "tm.event='Tx' AND transfer.sender='mono1abc...'"
},
"id": 1
}
Unsubscribe
{
"jsonrpc": "2.0",
"method": "unsubscribe",
"params": {
"query": "tm.event='NewBlock'"
},
"id": 1
}
EVM Subscriptions
Subscribe to New Heads
{
"jsonrpc": "2.0",
"method": "eth_subscribe",
"params": ["newHeads"],
"id": 1
}
Response on each new block:
{
"jsonrpc": "2.0",
"method": "eth_subscription",
"params": {
"subscription": "0x...",
"result": {
"number": "0x1b4",
"hash": "0x...",
"parentHash": "0x...",
"timestamp": "0x..."
}
}
}
Subscribe to Logs
{
"jsonrpc": "2.0",
"method": "eth_subscribe",
"params": [
"logs",
{
"address": "0x...",
"topics": ["0x..."]
}
],
"id": 1
}
Subscribe to Pending Transactions
{
"jsonrpc": "2.0",
"method": "eth_subscribe",
"params": ["newPendingTransactions"],
"id": 1
}
Unsubscribe
{
"jsonrpc": "2.0",
"method": "eth_unsubscribe",
"params": ["0x...subscription_id..."],
"id": 1
}
JavaScript Examples
CometBFT
const WebSocket = require('ws');
const ws = new WebSocket('wss://rpc.sprintnet.monolythium.com:26657/websocket');
ws.on('open', () => {
ws.send(JSON.stringify({
jsonrpc: '2.0',
method: 'subscribe',
params: { query: "tm.event='NewBlock'" },
id: 1
}));
});
ws.on('message', (data) => {
const msg = JSON.parse(data);
console.log('New block:', msg);
});
EVM (ethers.js)
const { ethers } = require('ethers');
const provider = new ethers.WebSocketProvider(
'wss://rpc.sprintnet.monolythium.com:8546'
);
// New blocks
provider.on('block', (blockNumber) => {
console.log('New block:', blockNumber);
});
// Contract events
const filter = {
address: '0x...',
topics: [ethers.id('Transfer(address,address,uint256)')]
};
provider.on(filter, (log) => {
console.log('Transfer event:', log);
});
Connection Management
Reconnection
function connect() {
const ws = new WebSocket('wss://...');
ws.on('close', () => {
console.log('Connection closed, reconnecting...');
setTimeout(connect, 5000);
});
ws.on('error', (err) => {
console.error('WebSocket error:', err);
ws.close();
});
}
Heartbeat
setInterval(() => {
if (ws.readyState === WebSocket.OPEN) {
ws.ping();
}
}, 30000);
Configuration
Enable WebSocket in your node:
# config.toml (CometBFT)
[rpc]
laddr = "tcp://0.0.0.0:26657"
# app.toml (EVM)
[json-rpc]
ws-address = "0.0.0.0:8546"
Rate Limits
Public endpoints may limit:
- Connections per IP
- Subscriptions per connection
- Messages per second
For heavy usage, run your own node.
Related
- EVM RPC - EVM HTTP RPC
- Cosmos REST - REST API