XRPLF / xrpl.js

A JavaScript/TypeScript API for interacting with the XRP Ledger in Node.js and the browser
https://xrpl.org/
1.2k stars 511 forks source link

How to listen to events on the XRP Ledger (transaction, ledger, account) by a special account? #809

Closed passionofvc closed 4 years ago

passionofvc commented 6 years ago

I know subscribe ledger event with below code, but how to listen to events on the XRP Ledger (transaction, ledger, account) by a special account?

api.connect().then( () => {
       console.log('Connection is open now.');
       return api.on('ledger', message => {
                 //console.log(message);
                 console.log(JSON.stringify(message, null, 2));
             }
        );
 });
sharafian commented 6 years ago
const account = /* ... */
api.connect().then( () => {    
   api.connection.on('transaction', (ev) => {
      console.log(JSON.stringify(ev, null, 2))
   }) 
   return api.connection.request({
     command: 'subscribe',
     accounts: [ account ]
   })
})

This code allows you to listen for transactions on a given account. You can read more about the fields here

sublimator commented 6 years ago

You guys survived ? :)

passionofvc commented 6 years ago

@sharafian thanks, if I have 1 million account, whether I can subscribe account by 1 million or should I call api.connection.request 1 million time for one account? I am sorry to be late.


const account = /* ... */
api.connect().then( () => {    
   api.connection.on('transaction', (ev) => {
      console.log(JSON.stringify(ev, null, 2))
   }) 
   return api.connection.request({
     command: 'subscribe',
     accounts: [ 1 million account ]
   })
})
sharafian commented 6 years ago

Yep, the accounts field can contain many accounts if you want to subscribe to more than one

passionofvc commented 6 years ago

@sharafian very thanks, I am newbie to javascript, also learn many this time.

mannutech commented 6 years ago

This thing was not on their official documentation at all. Thanks for saving

mannutech commented 6 years ago

I figured it out as per the latest build :

client.connect().then((ok) => {
        client.connection._onMessage(JSON.stringify({
            type: 'transaction'
        }));
        client.connection.on('transaction', (EventBody) => {
            console.info(This is where you get your event body');
        });
 return client.connection.request({
            command: 'subscribe',
            accounts: []
        })
})

Hope should work for you.

intelliot commented 6 years ago

In 1.0.0-beta.0 and later, use the request() method directly (docs).

tangten commented 5 years ago

Hi @intelliot, do we have any way to listen new ledger or new transaction?

intelliot commented 5 years ago

@tangnv Yes, use api.request() to subscribe to ledger for new ledgers, and transactions for new transactions. For transactions affecting a specific account, subscribe to accounts and use an array to specify the account(s) you're interested in. See Listening to streams.

dmitryrn commented 5 years ago

@tangnv Hi, what if I need to listen for new transactions for 1000000 accounts? If I will use subscribe with array of billion accounts than how it may affect performance or something else? Maybe it can break something at all? Are there any restrictions with it? Thanks you!

tuloski commented 5 years ago

@tangnv Hi, what if I need to listen for new transactions for 1000000 accounts? If I will use subscribe with array of billion accounts than how it may affect performance or something else? Maybe it can break something at all? Are there any restrictions with it? Thanks you!

In that case it's better to just listen to all transactions.

intelliot commented 4 years ago

This is complete. Docs: https://xrpl.org/rippleapi-reference.html#listening-to-streams

mmo2112 commented 4 years ago

@tangnv Hi, what if I need to listen for new transactions for 1000000 accounts? If I will use subscribe with array of billion accounts than how it may affect performance or something else? Maybe it can break something at all? Are there any restrictions with it? Thanks you!

I think it's really not good cuz ripple may save list subscription of address on memory and will be dropped if ws connection is broken by undefined reason. Example on Bitcoin, we have importaddress for watching address option and the physical node will storage those addresses in disk, so we can access them anytime, no need to worry about connection reset. Just imagine that we have 1 milliion ripple addresses and the connection is broken, we must re-subscribe those 1 milliion addresses or following traditional way is reading all transactions from every block, the hardest thing is speed of creating Ripple block too fast, so can be missed a lot of transactions or delay reading block. Please give me some advice, thanks so much.