How to stream Solana blocks in real-time
Use Helius Laserstream to get real-time block, slot, and transaction updates with sub-50ms latency via WebSocket. Copy the code below to add streaming to your own app.
Professional Plan Required
Interactive Demo
Copy the code
These snippets show exactly how to connect and subscribe to Laserstream. Paste them into your project.
1. Connect to WebSocket
Establish a WebSocket connection and subscribe to slot notifications.
// Connect to Laserstream WebSocket
const ws = new WebSocket(
'wss://atlas-mainnet.helius-rpc.com/?api-key=YOUR_API_KEY'
);
ws.onopen = () => {
console.log('Connected to Laserstream');
// Subscribe to slot notifications
ws.send(JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'slotSubscribe',
params: []
}));
};
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
// Handle subscription confirmation
if (data.id === 1) {
console.log('Subscribed with ID:', data.result);
return;
}
// Handle slot notifications
if (data.method === 'slotNotification') {
const { slot, parent, root } = data.params.result;
console.log(`Slot: ${slot}, Parent: ${parent}, Root: ${root}`);
}
};
ws.onerror = (error) => console.error('WebSocket error:', error);
ws.onclose = () => console.log('Disconnected from Laserstream');2. Subscribe to Blocks
Get full block data including transactions as they are confirmed.
// Subscribe to block notifications with transaction details
ws.send(JSON.stringify({
jsonrpc: '2.0',
id: 2,
method: 'blockSubscribe',
params: [
'all', // or { mentionsAccountOrProgram: '<PUBKEY>' }
{
commitment: 'confirmed',
encoding: 'json',
transactionDetails: 'signatures',
showRewards: false,
maxSupportedTransactionVersion: 0
}
]
}));
// Handle block notifications
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data.method === 'blockNotification') {
const block = data.params.result.value.block;
console.log(`Block at slot ${data.params.result.context.slot}`);
console.log(`Transactions: ${block.signatures?.length || 0}`);
console.log(`Block time: ${new Date(block.blockTime * 1000)}`);
}
};3. Monitor Account Changes
Subscribe to real-time updates for specific accounts like wallets or program accounts.
// Subscribe to account changes (e.g., monitor a wallet)
const accountPubkey = 'YourAccountPublicKeyHere';
ws.send(JSON.stringify({
jsonrpc: '2.0',
id: 3,
method: 'accountSubscribe',
params: [
accountPubkey,
{
commitment: 'confirmed',
encoding: 'jsonParsed'
}
]
}));
// Handle account change notifications
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data.method === 'accountNotification') {
const accountInfo = data.params.result.value;
console.log('Account updated!');
console.log(`Lamports: ${accountInfo.lamports}`);
console.log(`Owner: ${accountInfo.owner}`);
console.log(`Data: ${JSON.stringify(accountInfo.data)}`);
}
};When to use Laserstream
Trading Bots
React to on-chain events in milliseconds. Monitor DEX trades, liquidations, or arbitrage opportunities as they happen.
Live Dashboards
Build real-time analytics showing network activity, transaction throughput, or protocol metrics.
Wallet Notifications
Alert users instantly when their wallet receives tokens, NFTs, or when transactions confirm.
Program Monitoring
Watch your program accounts for state changes and trigger backend logic in response.