programSubscribe
Subscribe to a program to receive notifications when the lamports or data for an account owned by the given program changes. This method establishes a persistent WebSocket connection that will send real-time notifications whenever accounts owned by the specified program are created, modified, or deleted. You can filter results by data size, account owner, or custom criteria to only receive notifications for accounts that match your requirements.
Request object for subscribing to program account changes via WebSocket
JSON-RPC protocol version
2.0
Possible values: Request identifier that will be returned in the response
1
The WebSocket method name
programSubscribe
Possible values: WebSocket notification for program account changes
Subscription successfully created
Bad Request - Invalid parameters
Unauthorized
Forbidden
Too Many Requests
Internal Server Error
const ws = new WebSocket('wss://public.rpc.solanavibestation.com');
ws.onopen = function() {
// Subscribe to Token Program accounts
ws.send(JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'programSubscribe',
params: [
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
{
encoding: 'jsonParsed',
filters: [{ dataSize: 165 }],
commitment: 'finalized'
}
]
}));
// Subscribe to System Program accounts with data size filter
ws.send(JSON.stringify({
jsonrpc: '2.0',
id: 2,
method: 'programSubscribe',
params: [
'11111111111111111111111111111111',
{
encoding: 'base64',
filters: [{ dataSize: 80 }],
commitment: 'confirmed'
}
]
}));
};
ws.onmessage = function(event) {
const data = JSON.parse(event.data);
if (data.method === 'programNotification') {
const { pubkey, account } = data.params.result.value;
const slot = data.params.result.context.slot;
console.log(`[Slot ${slot}] Account ${pubkey} changed:`);
console.log('Owner:', account.owner);
console.log('Lamports:', account.lamports);
console.log('Data:', account.data);
} else if (data.result) {
console.log('Subscription ID:', data.result);
}
};
{
"jsonrpc": "2.0",
"method": "programNotification",
"params": {
"result": {
"context": {
"slot": 5208469
},
"value": {
"pubkey": "H4vnBqifaSACnKa7acsxstsY1iV1bvJNxsCY7enrd1hq",
"account": {
"data": [
"11116bv5nS2h3y12kD1yUKeMZvGcKLSjQgX6BeV7u1FrjeJcKfsHPXHRDEHrBesJhZyqnnq9qJeUuF7WHxiuLuL5twc38w2TXNLxnDbjmuR",
"base58"
],
"executable": false,
"lamports": 33594,
"owner": "11111111111111111111111111111111",
"rentEpoch": 636,
"space": 80
}
}
},
"subscription": 24040
}
}
Last updated