WoC Sockets V2 (Beta)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>WOC Socket V2 - blockheaders (JSON format)</title>
<script type="text/javascript">
window.addEventListener('load', function() {
const wsUrl = "wss://socket-v2.whatsonchain.com/websocket/blockheaders?format=json";
let ws;
let pingInterval;
function doConnect() {
console.log("[SocketV2] Connecting ->", wsUrl);
ws = new WebSocket(wsUrl);
ws.onopen = function(e) {
console.log("[SocketV2] OPEN:", wsUrl, e);
pingInterval = setInterval(() => {
if (ws && ws.readyState === WebSocket.OPEN) {
console.log("[SocketV2] Sending ping to server");
ws.send('ping'); // Send ping as text message
}
}, 25000);
};
ws.onerror = function(e) {
console.warn("[SocketV2] ERROR event – possibly normal if server closes. URL:", wsUrl);
};
ws.onclose = function(e) {
console.log("[SocketV2] CLOSE:", wsUrl, e);
// Clear ping interval when connection closes
if (pingInterval) {
clearInterval(pingInterval);
pingInterval = null;
}
};
ws.onmessage = async function(e) {
if (!e.data) {
console.log("[SocketV2] PING or empty message ->", wsUrl);
return;
}
const blob = e.data;
if (blob && blob.size > 0) {
try {
const text = await blob.text();
// Handle ping/pong messages
if (text === 'ping') {
console.log("[SocketV2] Received ping from server, sending pong");
ws.send('pong');
return;
}
if (text === 'pong') {
console.log("[SocketV2] Received pong from server");
return;
}
console.log("[SocketV2] MESSAGE (JSON):", text);
} catch (err) {
console.log("[SocketV2] MESSAGE (error reading blob):", err);
}
} else {
// Handle direct string messages
const text = e.data;
// Handle ping/pong messages
if (text === 'ping') {
console.log("[SocketV2] Received ping from server, sending pong");
ws.send('pong');
return;
}
if (text === 'pong') {
console.log("[SocketV2] Received pong from server");
return;
}
console.log("[SocketV2] MESSAGE (direct string):", text);
}
};
}
doConnect();
});
</script>
</head>
<body>
<h1>WOC Socket V2 - blockheaders (JSON format)</h1>
<p>Open the browser console to see messages from the WebSocket.</p>
</body>
</html>New Block Header Event
Mainnet URLs
Block Headers History
Mainnet URLs
Parameter
Description
Block Transactions
Mainnet URLs
Parameter
Description
Mempool Transactions
Mainnet URLs
Parameter
Description
Confirmed Transactions
Mainnet URLs
Chain Stats
Mainnet URLs
Customized Events
Last updated
Was this helpful?