How to Subscribe to Stream

Stream subscription provides you with direct access to blockchain data as it propagates through the BDN network. This is very good for users e.g traders who want to get data earlier e.g new released token

With a stream subscription, you enjoy:

  • Faster blocks: Receive new blocks before standard RPC propagation
  • Mempool access: See pending transactions before they're mined
  • Lower latency: Edge infrastructure minimizes network hops

Code Sample

Here, you will be checking if you are connected to streams

bashwss://shared.eu-central-1.getblock.io/<ACCESS_TOKEN>

index.js

javascript//npm install ws dotenv
import WebSocket from "ws";
import "dotenv/config";

const ws = new WebSocket(
  `wss://shared.eu-central-1.getblock.io/${process.env.ACCESS_TOKEN}`,
);

ws.on("open", () => {
  console.log("Connected to BSC streams");
});

ws.on("error", (err) => {
  console.error("WebSocket error:", err.message);
});
bash> bsc-node@1.0.0 start
> node index.js

Connected to BSC streams

How to Subscribe to Streams

To subscribe to streams, there are four parameters you can make use of as seen below:

StreamDescriptionUse Case
bdnBlocksNew blocks with headers and transactionsBlock monitoring, analytics
newTxsNew transactions entering mempoolMEV detection, copy trading
pendingTxsPending transactionsFront-running protection
txReceiptsTransaction receipts after confirmationTrade confirmation

1. Block Streams (bdnBlocks)

Subscribe to new blocks as they're produced.

Include Options

FieldDescription
headerBlock header (number, hash, timestamp, gasUsed, gasLimit)
transactionsFull transaction list
hashBlock hash only

Example: Subscribe to Blocks

subscribe-blocks.js

javascriptimport WebSocket from "ws";
import "dotenv/config";

const ws = new WebSocket(`wss://shared.eu-central-1.getblock.io/${process.env.ACCESS_TOKEN}`);

ws.on("open", () => {
  console.log("Connected");

  // Subscribe to new blocks
  ws.send(
    JSON.stringify({
      jsonrpc: "2.0",
      id: 1,
      method: "subscribe",
      params: ["bdnBlocks", { include: ["header", "transactions", "hash"] }],
    }),
  );
});

ws.on("message", (data) => {
  const message = JSON.parse(data);

  // Subscription confirmation
  if (message.id === 1 && message.result) {
    console.log("Subscribed! ID:", parseInt(message.result));
    return;
  }

  // Block notification
  if (message.params?.result) {
    const block = message.params.result;
    console.log("New block:", {
      number: parseInt(block.header.number, 16),
      hash: block.header.hash,
      txCount: block.transactions?.length || 0,
    });
  }
});
jsonConnected
Subscribed! ID: 9
New block: { number: 84411561, hash: undefined, txCount: 86 }
New block: { number: 84411562, hash: undefined, txCount: 105 }
New block: { number: 84411563, hash: undefined, txCount: 66 }
New block: { number: 84411564, hash: undefined, txCount: 66 }
New block: { number: 84411565, hash: undefined, txCount: 78 }
New block: { number: 84411566, hash: undefined, txCount: 93 }
New block: { number: 84411567, hash: undefined, txCount: 63 }

2. Transaction Streams (newTxs / pendingTxs)

Subscribe to mempool transactions as they propagate through the network.

Include Options

FieldDescription
tx_hashTransaction hash
fromSender address
toRecipient address
valueTransaction value (hex)
gas_priceGas price (hex)
inputTransaction calldata

Filters

Reduce bandwidth by filtering server-side:

FilterTypeDescription
toarrayTransaction recipient addresses
fromarrayTransaction sender addresses
method_idarrayFunction selectors (first 4 bytes of calldata)

Example: Monitor PancakeSwap Transactions

<pre class="language-javascript" data-title="monitor-pancakeswap.js"><code class="lang-javascript">import WebSocket from "ws"; import { ethers } from "ethers"; import "dotenv/config";

const PANCAKE_ROUTER = "0x10ED43C718714eb63d5aA57B78B54704E256024E";

const SWAP_METHODS = { "0x38ed1739": "swapExactTokensForTokens", "0x7ff36ab5": "swapExactETHForTokens", "0x18cbafe5": "swapExactTokensForETH", "0xfb3bdb41": "swapETHForExactTokens", "0x5c11d795": "swapExactTokensForTokensSupportingFeeOnTransferTokens", "0xb6f9de95": "swapExactETHForTokensSupportingFeeOnTransferTokens", };

function monitorPancakeSwap() { const ws = new WebSocket(wss://shared.eu-central-1.getblock.io/${process.env.ACCESS_TOKEN});

ws.on("open", () => { console.log("🔌 Connected to BSC stream"); console.log("📊 Monitoring PancakeSwap...\n");

<strong> ws.send( </strong><strong> JSON.stringify({ </strong><strong> jsonrpc: "2.0", </strong><strong> id: 1, </strong><strong> method: "subscribe", </strong><strong> params: [ </strong><strong> "pendingTxs", </strong><strong> { </strong><strong> include: ["tx_hash", "from", "to", "value", "input", "gas_price"], </strong><strong> filters: { </strong><strong> to: [PANCAKE_ROUTER], </strong><strong> }, </strong><strong> }, </strong><strong> ], </strong><strong> }), </strong><strong> ); </strong><strong> }); </strong> ws.on("message", (data) => { const msg = JSON.parse(data);

// Subscription confirmed if (msg.id === 1) { console.log("✅ Subscribed to PancakeSwap transactions\n"); return; }

// New transaction if (msg.params?.result) { const tx = msg.params.result; const methodId = tx.input?.slice(0, 10); const methodName = SWAP_METHODS[methodId] || "unknown";

if (SWAP_METHODS[methodId]) { const value = ethers.formatEther(tx.value || "0"); const gasPrice = ethers.formatUnits(tx.gas_price || "0", "gwei");

console.log("🔄 Swap detected:"); console.log( Hash: ${tx.tx_hash.slice(0, 18)}...); console.log( Method: ${methodName}); console.log( From: ${tx.from.slice(0, 10)}...); console.log( Value: ${value} BNB); console.log( Gas: ${gasPrice} gwei); console.log(""); } } });

ws.on("close", () => { console.log("Disconnected, reconnecting in 5s..."); setTimeout(monitorPancakeSwap, 5000); });

ws.on("error", (err) => { console.error("WebSocket error:", err.message); }); }

monitorPancakeSwap();

</code></pre>

json{
  "tx_hash": "0xdef456...",
  "from": "0xabc...",
  "to": "0x10ED43C718714eb63d5aA57B78B54704E256024E",
  "value": "0x16345785d8a0000",
  "gas_price": "0xb2d05e00",
  "input": "0x38ed1739..."
}

<pre class="language-js" data-overflow="wrap"><code class="lang-js">import WebSocket from 'ws'; import 'dotenv/config';

const PANCAKE_ROUTER = '0x10ED43C718714eb63d5aA57B78B54704E256024E';

const SWAP_METHODS = { '0x38ed1739': 'swapExactTokensForTokens', '0x7ff36ab5': 'swapExactETHForTokens', '0x18cbafe5': 'swapExactTokensForETH', '0xfb3bdb41': 'swapETHForExactTokens', };

function monitorNewTxs() { const ws = new WebSocket(wss://shared.eu-central-1.getblock.io/${process.env.ACCESS_TOKEN});

ws.on('open', () => { console.log('Connected to BSC stream'); console.log('Monitoring confirmed PancakeSwap swaps...\n');

<strong> ws.send(JSON.stringify({ </strong><strong> jsonrpc: '2.0', </strong><strong> id: 1, </strong><strong> method: 'subscribe', </strong><strong> params: ['newTxs', { </strong><strong> include: ['tx_hash', 'from', 'to', 'value', 'input'], </strong><strong> filters: { </strong><strong> to: [PANCAKE_ROUTER], </strong><strong> method_id: Object.keys(SWAP_METHODS) </strong><strong> } </strong><strong> }] </strong><strong> })); </strong><strong> }); </strong> ws.on('message', (data) => { const msg = JSON.parse(data);

if (msg.id === 1) { console.log('Subscribed to confirmed PancakeSwap transactions\n'); return; }

if (msg.params?.result) { const tx = msg.params.result; const methodId = tx.input?.slice(0, 10); const methodName = SWAP_METHODS[methodId] || 'unknown';

console.log('Confirmed swap:'); console.log( Hash: ${tx.tx_hash}); console.log( Method: ${methodName}); console.log( From: ${tx.from}); console.log( To: ${tx.to}); console.log( Value: ${tx.value}); console.log(''); } });

ws.on('close', () => { console.log('Disconnected, reconnecting in 5s...'); setTimeout(monitorNewTxs, 5000); });

ws.on('error', (err) => { console.error('WebSocket error:', err.message); }); }

monitorNewTxs();

</code></pre>

bash{
    "jsonrpc":"2.0",
    "id":1,
    "result":"0x11"
}

3. Transaction Receipts (txReceipts)

Subscribe to transaction confirmations.

Include Options

FieldDescription
receiptFull transaction receipt
logsEvent logs emitted

Example:

<pre class="language-javascript"><code class="lang-javascript">import WebSocket from 'ws'; import 'dotenv/config';

const ws = new WebSocket(wss://shared.eu-central-1.getblock.io/${process.env.ACCESS_TOKEN});

ws.on("open", () => { console.log("Connected");

// Subscribe to new blocks <strong> ws.send(JSON.stringify({ </strong><strong> jsonrpc: '2.0', </strong><strong> id: 1, </strong><strong> method: 'subscribe', </strong><strong> params: ['txReceipts', { </strong><strong> include: ['receipt', 'logs'] </strong><strong> }] </strong><strong> })); </strong><strong>}); </strong> ws.on("message", (data) => { const message = JSON.parse(data); console.log(message)}) </code></pre>

bash{ 
  jsonrpc: '2.0', 
  id: 1, 
  result: '0x10' 
}

Unsubscribing

To stop receiving notifications, unsubscribe using the subscription ID:

unsubscribe.js

jsimport WebSocket from "ws";
import "dotenv/config";
const ws = new WebSocket(`wss://shared.eu-central-1.getblock.io/${process.env.ACCESS_TOKEN}`);
ws.on("open", () => {
  console.log("Connected");

ws.send(
  JSON.stringify({
    jsonrpc: "2.0",
    id: 10,
    method: "unsubscribe",
    params: ["23"], // Subscription ID
  }),
);})

ws.on("message", (data) => {
  const message = JSON.parse(data);
  console.log(JSON.stringify(message, null, 2));
});
bash{ 
    jsonrpc: '2.0', 
    id: 10, 
    result: true 
}

Best Practices

1. Request Only What You Need

js// ❌ Bad: Request everything
include: ['tx_contents']  // Large payloads, high bandwidth
js// ✅ Good: Request specific fields
include: ['tx_hash', 'from', 'to', 'value']  // Small payloads
  1. Use Server-Side Filters
js// ❌ Bad: Filter client-side
params: ['newTxs', { include: ['tx_contents'] }]
// Then filter in your code... wasteful!
js// ✅ Good: Filter server-side
params: ['newTxs', {
  include: ['tx_hash', 'to', 'input'],
  filters: { to: ['0xTargetContract'] }
}]
  1. Handle Reconnection
javascriptfunction createConnection() {
  const ws = new WebSocket(`wss://bsc.getblock.io/mev/ws?api_key=${API_KEY}`);
  
  ws.on('close', () => {
    console.log('Disconnected, reconnecting...');
    setTimeout(createConnection, 5000);
  });
  
  ws.on('open', () => {
    // Re-subscribe after reconnect
    subscribeToFeeds(ws);
  });
  
  return ws;
}
  1. Process Messages Asynchronously
javascriptws.on('message', async (data) => {
  // Don't block the message loop
  setImmediate(() => {
    processMessage(JSON.parse(data));
  });
});

Rate Limits

TierConcurrent SubscriptionsMessage Rate
Standard51,000/sec
Pro2010,000/sec
EnterpriseUnlimitedUnlimited

Contact support to upgrade your tier.

Troubleshooting

ProblemSolution
No messages received<ul><li>Check subscription response—did you receive a subscription ID?</li><li>Verify filters aren't too restrictive</li><li>Confirm your API key is valid and active</li></ul>
High latency<ul><li>Reduce the number of <code>include</code> fields</li><li>Add filters to reduce message volume</li><li>Check your network connection</li></ul>
Missed messages<ul><li>Process messages asynchronously</li><li>Increase your message queue buffer</li><li>Consider multiple connections for different streams</li></ul>
Connection Drop<ul><li>Implement automatic reconnection (see example above)</li><li>Re-subscribe after each reconnect—subscriptions don't persist</li><li>Check your network stability</li></ul>

Next Step

  1. Submitting transactions to public mempool with accelerated propagation
  2. Using private transactions for MEV protection or without MEV Protection