WoC Sockets V2 (Beta)

Example using WebSocket API client
<!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

Receive new block header events in real time.

Mainnet URLs

Block Headers History

Stream block headers between the provided block heights from and to.

Mainnet URLs

URL Parameters

Parameter
Description

from

Block heigh to stream from.

to

Block height to stream to.

Block Transactions

Stream transactions from a block height and transaction index.

  • hex, vin and vout values are not published for message sizes greater than 10MB.

  • Recommended to fetch details for such transactions using the REST endpoints using the published txid value.

Mainnet URLs

URL Parameters

Parameter
Description

from

Block height to stream from.

to

Block height to stream to.

txIndex (optional)

Transaction index as a starting point in the first block to deliver. Default value 0.

format (optional)

hex (default) or json. If hex, the response will include transaction hex and metadata. If json, hex is excluded.

Mempool Transactions

Receive mempool transactions in real time.

  • Mempool transactions are collected from multiple nodes.

  • Client side should be idempotent because duplicate transaction events are not guaranteed to have been removed.

  • hex, vin and vout values are not published for message sizes greater than 10MB. Recommended to fetch details for such transactions using the REST endpoints using the published txid value.

Mainnet URLs

URL Parameters

Parameter
Description

format (optional)

hex (default) or json. If hex, the response will include transaction hex and metadata. If json, hex is excluded.

Confirmed Transactions

Receive confirmed transactions in real time.

  • Confirmed transactions are collected from multiple nodes.

Mainnet URLs

Chain Stats

Receive common chain stats every 10 seconds.

Mainnet URLs

Customized Events

If your application needs customized/filtered events via WebSockets, please let us know in the WoC devs Telegram channel.

Last updated

Was this helpful?