Examples
Ready-to-run snippets you can paste into Node, the browser, or any other Socket.IO client.
Live market chart
Subscribe to BTC/USD 1-minute candles and push updates into a chart library.
import { io } from 'socket.io-client';
const socket = io('ws://localhost:8091', {
auth: { token: 'Bearer ' + process.env.LEDGERCORE_TOKEN },
transports: ['websocket'],
});
socket.on('connected', () => {
socket.emit('request_market_data', {
symbol: 'BTC-USD',
resolution: '1m',
window: '24h',
});
});
socket.on('market_chart_initial_data', ({ series }) => {
chart.setData(series);
});
socket.on('market_chart_live_update', (tick) => {
chart.updateLastCandle(tick);
});
socket.on('market_data_error', ({ code, message }) => {
console.error(code, message);
});
Streaming query results
Fire off a SQL query against the LedgerCore query engine and render rows as they arrive.
const queryId = crypto.randomUUID();
socket.emit('query', {
sql: `SELECT date, SUM(volume_usd) AS v
FROM trades
WHERE venue = 'binance' AND date > NOW() - INTERVAL 7 DAY
GROUP BY 1 ORDER BY 1`,
applicationId: 'app_2xMn1psnHkYRTH00nxdvp02IWfr',
});
socket.on('query_started', ({ queryId: id, estimatedMs }) => {
console.log('ETA', estimatedMs, 'ms');
});
socket.on('query_progress', ({ pct }) => {
progressBar.set(pct);
});
socket.on('query_response', ({ rows }) => {
table.append(rows);
});
socket.on('query_completed', ({ totalRows, durationMs }) => {
console.log('done —', totalRows, 'rows in', durationMs, 'ms');
});
Live credit balance
Your personal room delivers every credit movement without a subscribe call. Perfect for showing a balance widget that never goes stale.
socket.on('credit_balance_updated', ({ newBalance, applicationId }) => {
balanceWidget.set(applicationId, newBalance);
});
socket.on('credits_deducted', ({ amount, action, transactionId }) => {
toast(`Used ${amount} credits for ${action}`);
});
socket.on('insufficient_credits', ({ required, available, action }) => {
modal.open('topup', { required, available, action });
});
LedgerBrain AI — streaming chat
The assistant streams deltas just like an LLM chat. Concatenate them until isFinal === true.
const conversationId = crypto.randomUUID();
let buffer = '';
socket.emit('chat', {
conversationId,
message: 'Show me the top 5 CEX inflows over the last 24 hours.',
});
socket.on('chat_typing', () => ui.showTyping());
socket.on('chat_stop_typing', () => ui.hideTyping());
socket.on('chat_message', ({ delta, isFinal }) => {
buffer += delta;
ui.renderMarkdown(buffer);
if (isFinal) {
buffer = '';
}
});
socket.on('chat_response', ({ sql }) => {
if (sql) codePane.show(sql);
});
Long-running address analysis
Fire-and-forget compliance job with progress updates.
socket.emit('address_analysis', {
chain: 'bitcoin',
address: '1Drt3c8pSdrkyjuBiwVcSSixZwQtMZ3Tew',
hops: 3,
});
socket.on('address_analysis_started', ({ jobId }) => {
console.log('job started', jobId);
});
socket.on('address_analysis_progress', ({ stage, pct }) => {
statusBar.set(`${stage} — ${pct}%`);
});
socket.on('address_analysis_response', ({ riskScore, riskLevel, signals }) => {
panel.render({ riskScore, riskLevel, signals });
});
socket.on('address_analysis_completed', ({ durationMs }) => {
console.log('done in', durationMs, 'ms');
});
Python client
Any Socket.IO 4.x client works. Here's the equivalent of the market chart example in Python:
import os
import socketio
sio = socketio.Client()
@sio.on('connected')
def on_ready(data):
sio.emit('request_market_data', {
'symbol': 'BTC-USD',
'resolution': '1m',
'window': '24h',
})
@sio.on('market_chart_initial_data')
def on_initial(data):
print('got', len(data['series']), 'candles')
@sio.on('market_chart_live_update')
def on_tick(tick):
print(tick['timestamp'], tick['close'])
sio.connect(
'ws://localhost:8091',
auth={'token': f"Bearer {os.environ['LEDGERCORE_TOKEN']}"},
transports=['websocket'],
)
sio.wait()