subscribe - XPRL

This method requests periodic notifications from the server when certain events happen.

Parameters

ParameterTypeRequiredDescription
streamsarrayNoStreams to subscribe
accountsarrayNoAccounts to watch
accounts_proposedarrayNoAccounts for proposed txs
booksarrayNoOrder books to watch

Request Example

subscribe works over WebSocket only. Connect to wss://shared.eu-central-1.getblock.io/<ACCESS_TOKEN> and keep the connection open to receive updates.

bashwscat -c wss://shared.eu-central-1.getblock.io/<ACCESS_TOKEN>

# After it connects, send:
{"id": 1, "command": "subscribe", "streams": ["ledger"]}
javascriptconst WebSocket = require('ws');

const ws = new WebSocket('wss://shared.eu-central-1.getblock.io/<ACCESS_TOKEN>');

ws.on('open', () => {
    ws.send(JSON.stringify({
        id: 1,
        command: 'subscribe',
        streams: ['ledger']
    }));
});

ws.on('message', (data) => {
    console.log(JSON.parse(data));
});

Response Example

json{
    "result": {
        "fee_base": 10,
        "fee_ref": 10,
        "ledger_hash": "3A0B41C31B13436610C5961E2C914529CAD3CCC4A7859ED6AABFCD0700847BC1",
        "ledger_index": 63632030,
        "ledger_time": 674581032,
        "reserve_base": 10000000,
        "reserve_inc": 2000000,
        "status": "success"
    }
}

Response Parameters

ParameterTypeDescription
ledger_indexnumberCurrent ledger
statusstringSubscription status

Use Cases

  • Real-time ledger updates
  • Monitor account activity
  • Track order book changes

Error Handling

Error CodeDescription
invalidParamsInvalid parameters

SDK Integration

javascriptconst xrpl = require('xrpl');

async function subscribe() {
    const client = new xrpl.Client('wss://shared.eu-central-1.getblock.io/<ACCESS_TOKEN>');
    await client.connect();
    
    client.on('ledgerClosed', ledger => {
        console.log('New ledger:', ledger.ledger_index);
    });
    
    await client.request({
        command: 'subscribe',
        streams: ['ledger']
    });
}

subscribe();
pythonfrom xrpl.clients import WebsocketClient
from xrpl.models.requests import Subscribe, StreamParameter

url = "wss://shared.eu-central-1.getblock.io/<ACCESS_TOKEN>"

with WebsocketClient(url) as client:
    client.send(Subscribe(streams=[StreamParameter.LEDGER]))
    for message in client:
        print(message)