# How to query based on Block-Height and listen to new Block/Transaction events | Also Sockets-API

**URL:** <https://forum.cardano.org/t/how-to-query-based-on-block-height-and-listen-to-new-block-transaction-events-also-sockets-api/12472>\
**Category:** Developers\
**Tags:** cardano\
**Created:** [29 May 2018 07:48 UTC](https://forum.cardano.org/t/how-to-query-based-on-block-height-and-listen-to-new-block-transaction-events-also-sockets-api/12472 "2018-05-29T07:48:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rahil\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@Rahil\_Shaikh](https://forum.cardano.org/u/Rahil_Shaikh)\
**Post date:** [29 May 2018 07:48 UTC](https://forum.cardano.org/t/how-to-query-based-on-block-height-and-listen-to-new-block-transaction-events-also-sockets-api/12472/1 "2018-05-29T07:48:44Z")

</div>

Hello,

A part of our infra is to keep processing new blocks to check if any currency(here ADA) is sent to address from a list.  
To do this in case of other blockchains we listen to events of new blocks and process them, we also store the blockheight so in case of a reboot the databases can be synchronized.

But in case of ADA I guess there aren’t any events, and also there isn’t an api to fetch the data based on block-height.

Right now my option is to query the node with /api/blocks/pages/total then if the pageno has increased then subsequently call /api/blocks/pages, scan for the slots/blocks and for each blocks call /api/blocks/txs/{blockhash}, then scan the outputs for the matching address.

This looks like a long process. If we can have events of new blocks a lot of the above steps can be reduced. Or as an alternative if there is an api that can directly query on the blockheight.

I found some event related info here [https://cardanodocs.com/technical/explorer/](https://cardanodocs.com/technical/explorer/)  
but I couldn’t make it work.

Any guidance on the flow, api or events would be appreciated.

---

<div class="post-metadata">

**Author:** ![vantuz-subhuman](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.cardano.org/vantuz-subhuman/32/3976_2.png) [@vantuz-subhuman](https://forum.cardano.org/u/vantuz-subhuman)\
**Post date:** [29 May 2018 09:21 UTC](https://forum.cardano.org/t/how-to-query-based-on-block-height-and-listen-to-new-block-transaction-events-also-sockets-api/12472/2 "2018-05-29T09:21:26Z")

</div>

> [@Rahil\_Shaikh](#):
>
> Right now my option is to query the node with /api/blocks/pages/total then if the pageno has increased then subsequently call /api/blocks/pages, scan for the slots/blocks and for each blocks call /api/blocks/txs/{blockhash}, then scan the outputs for the matching address.
> 
> This looks like a long process.

Hi! From what I understood - you don’t really need to process the whole history of the blockchain, sp there’s no need to process every block and every transaction, because if you have, for example, a list of 10 addresses - then you would process 21K blocks and 15K transactions every epoch just to find maybe 1 or 2, or even 0 transfers to your addresses of interest.

Instead, you can directly poll summaries for each of the addresses, using method: [http://cardanoexplorer.com/api/addresses/summary/DdzFFzCqrht2WKNEFqHvMSumSQpcnMxcYLNNBXPYXyHpRk9M7PqVjZ5ysYzutnruNubzXak2NxT8UWTFQNzc77uzjQ1GtehBRBdAv7xb](http://cardanoexplorer.com/api/addresses/summary/DdzFFzCqrht2WKNEFqHvMSumSQpcnMxcYLNNBXPYXyHpRk9M7PqVjZ5ysYzutnruNubzXak2NxT8UWTFQNzc77uzjQ1GtehBRBdAv7xb)

So you can configure the poll-time as you wish (for example once a minute) and then just iterate thru all addresses and query a summary for each one, and see if there’re any new transactions. This way you will only be processing the information of direct interest to you, and you can store in the database the last known number of transactions for each address, or an ID of the last known transaction.

But if at some point you will need to have a **history** of the blockchain processed, or some blocks-metadata collected - then the way you have described it is the only one, afaik, for now.

---

<div class="post-metadata">

**Author:** ![Rahil\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@Rahil\_Shaikh](https://forum.cardano.org/u/Rahil_Shaikh)\
**Post date:** [29 May 2018 09:39 UTC](https://forum.cardano.org/t/how-to-query-based-on-block-height-and-listen-to-new-block-transaction-events-also-sockets-api/12472/3 "2018-05-29T09:39:11Z")

</div>

Thanks @vantuz-subhuman.

My bad when I said list I should have mentioned the volume.

We have around 3mil addresses to begin with, so the method you mentioned though would work for a smaller list but in our case it is not possible.

The the Goal here is to continuously listen to new confirmed transactions and check if there is any ADA incoming to one of our addresses and subsequently update our database with the amount.

---

<div class="post-metadata">

**Author:** ![vantuz-subhuman](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.cardano.org/vantuz-subhuman/32/3976_2.png) [@vantuz-subhuman](https://forum.cardano.org/u/vantuz-subhuman)\
**Post date:** [29 May 2018 09:42 UTC](https://forum.cardano.org/t/how-to-query-based-on-block-height-and-listen-to-new-block-transaction-events-also-sockets-api/12472/4 "2018-05-29T09:42:11Z")

</div>

> [@Rahil\_Shaikh](#):
>
> We have around 3mil addresses to begin with, so the method you mentioned though would work for a smaller list but in our case it is not possible.

Hot damn! Than yes - the method you have described is the only way for now 🙂

> [@Rahil\_Shaikh](#):
>
> The the Goal here is to continuously listen to new confirmed transactions and check if there is any ADA incoming to one of our addresses and subsequently update our database with the amount.

Note that **“confirmed”** is the key word in this idea. Mind this document, please 🙂

[https://cardanodocs.com/cardano/transaction-assurance/](https://cardanodocs.com/cardano/transaction-assurance/)

---

<div class="post-metadata">

**Author:** ![Rahil\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@Rahil\_Shaikh](https://forum.cardano.org/u/Rahil_Shaikh)\
**Post date:** [29 May 2018 09:45 UTC](https://forum.cardano.org/t/how-to-query-based-on-block-height-and-listen-to-new-block-transaction-events-also-sockets-api/12472/5 "2018-05-29T09:45:07Z")

</div>

Thanks!

By any chance you have any idea how the events described here works? Can this be useful for us?

> **[Cardano SL Explorer](https://cardanodocs.com/technical/explorer/)**
>
> SL Documentation

---

<div class="post-metadata">

**Author:** ![vantuz-subhuman](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.cardano.org/vantuz-subhuman/32/3976_2.png) [@vantuz-subhuman](https://forum.cardano.org/u/vantuz-subhuman)\
**Post date:** [29 May 2018 10:27 UTC](https://forum.cardano.org/t/how-to-query-based-on-block-height-and-listen-to-new-block-transaction-events-also-sockets-api/12472/6 "2018-05-29T10:27:16Z")

</div>

> [@Rahil\_Shaikh](#):
>
> By any chance you have any idea how the events described here works? Can this be useful for us?

Well, it **seems** like it should support the “Subscribe SubTx” event and similar, but I haven’t actually tried those out yet, so cannot provide any more **reliable** info about it 🙁 (And also couldn’t find any other documentation on it).

**It requires a running SL node** on the same host, so you could connect to `localhost:8110`. If you manage to get it working - would be really cool and might be really useful. If you ready to wait a bit - I could try it out tonight (couldn’t find a reason to do it for long time 🙂 ) and some manuals if I manage to get it working.

---

<div class="post-metadata">

**Author:** ![Rahil\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@Rahil\_Shaikh](https://forum.cardano.org/u/Rahil_Shaikh)\
**Post date:** [29 May 2018 10:41 UTC](https://forum.cardano.org/t/how-to-query-based-on-block-height-and-listen-to-new-block-transaction-events-also-sockets-api/12472/7 "2018-05-29T10:41:51Z")

</div>

Nah, I tried it but couldn’t get it working.

May be I’m doing something wrong. Have raised a git issue.

> <https://github.com/input-output-hk/cardano-sl/issues/3006>

---

<div class="post-metadata">

**Author:** ![vantuz-subhuman](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.cardano.org/vantuz-subhuman/32/3976_2.png) [@vantuz-subhuman](https://forum.cardano.org/u/vantuz-subhuman)\
**Post date:** [29 May 2018 10:48 UTC](https://forum.cardano.org/t/how-to-query-based-on-block-height-and-listen-to-new-block-transaction-events-also-sockets-api/12472/8 "2018-05-29T10:48:24Z")

</div>

> [@Rahil\_Shaikh](#):
>
> Nah, I tried it but couldn’t get it working.

Yeah, I had the same feeling about this thing. I’ve literally seen zero discussion about socket-API before your post 🙂 Let’s hope they answer something, and maybe I’ll take a look into code to see if it’s still running according to the docs.

I will change the topic-title a bit, maybe someone with some experience on the issue will help us out.

---

<div class="post-metadata">

**Author:** ![Rahil\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@Rahil\_Shaikh](https://forum.cardano.org/u/Rahil_Shaikh)\
**Post date:** [29 May 2018 10:54 UTC](https://forum.cardano.org/t/how-to-query-based-on-block-height-and-listen-to-new-block-transaction-events-also-sockets-api/12472/9 "2018-05-29T10:54:16Z")

</div>

Thanks, that would be of great help.

---

<div class="post-metadata">

**Author:** ![\_ilap](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.cardano.org/_ilap/32/80886_2.png) [@\_ilap](https://forum.cardano.org/u/_ilap)\
**Post date:** [30 May 2018 06:57 UTC](https://forum.cardano.org/t/how-to-query-based-on-block-height-and-listen-to-new-block-transaction-events-also-sockets-api/12472/10 "2018-05-30T06:57:10Z")

</div>

The issue seems to me relating to the some wrong Unicode encoding in hte explorer.

I debugged a bit and the result from explorer is

```auto
	�0{"pingInterval":25000,"pingTimeout":60000,"upgrades":["websocket"],"sid":"PR0kCDESAAQwORQULxk0"}"

```

instead of

```auto
97:0{"pingInterval":25000,"pingTimeout":60000,"upgrades":["websocket"],"sid":"PR0kCDESAAQwORQULxk0"}.  

```

The first few bytes are **00 09 07 ef bf bd** instead of “97:”, the EFBFBD is the UTF8 for the Unicode’s invalid character FFFD , means that the explorer somewhy could not build the proper string. The 00 09 07 is CBOR representation of 0 9 7, the length of the payload, the string w/ the curly brackets. The 0 before the “{” means type is connet.

---

<div class="post-metadata">

**Author:** ![\_ilap](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.cardano.org/_ilap/32/80886_2.png) [@\_ilap](https://forum.cardano.org/u/_ilap)\
**Post date:** [30 May 2018 08:14 UTC](https://forum.cardano.org/t/how-to-query-based-on-block-height-and-listen-to-new-block-transaction-events-also-sockets-api/12472/11 "2018-05-30T08:14:16Z")

</div>

I was wrong, the ebbfbd, comes from node.js [Socket.io](http://Socket.io), what really comes from Explorer is  
000907FF, and [socket.io](http://socket.io)’s UTF converted the 0xFF to the Unicode’s invalid char.

Anyway, that result directly from explorer does not make sense as it’s not a valid CBOR representation, something missing. FF is the break/stop code of the indefinite length of some major types (map, array, bytestring etc.).

**UPDATE** : Nothing wrong w/ 000907FF, [Socket.io](http://Socket.io) should handle it, but the underlying XHR convert it to UTF8 (reponseText instead of response), despite the “Content-Type” is “application/octet-stream”. Probably, it got confused w/ the “Transfer-Encoding: chunked” header that comes from Cloudflare.

So, the socket.io-client or engine.io-client should be forced to use binaryType, somehow. I will check it when I am back from work.

**UPDATE2** : It cannot be forced to binaryType, as **socket.io-client** and **engine.io-client** use **_xmlhttprequest-ssl_** which is not XHR2 compliant, means the socket.io-client implementation does not support binary payload encode/decode in node.js, that’s sad.

What can be requested from IOHK, is to use string representation of the payload in their code instead.

---

<div class="post-metadata">

**Author:** ![Rahil\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@Rahil\_Shaikh](https://forum.cardano.org/u/Rahil_Shaikh)\
**Post date:** [4 June 2018 08:02 UTC](https://forum.cardano.org/t/how-to-query-based-on-block-height-and-listen-to-new-block-transaction-events-also-sockets-api/12472/12 "2018-06-04T08:02:05Z")

</div>

I think there could be one more way to do this.

I can use `/api/epochs/{epoch}/{slot}` api to get new blocks.

So I can have any block as my genesis block(starting point). Then my process would check the latest epoch/slot and then increment the slot by 1 to query the next block. (if the block is not yet formed, the slot is returned as empty array). Now once the slot no reaches the max\_slot\_per\_epoch I could increment the epoch by one and start with 1st slot.

@vantuz-subhuman What do you think about this?

I would need to know for sure what is the max no of slots in an epoch. And would also need to keep track if at anytime this config changes.

What is the current config for max\_no\_of\_slots in an epoch?

---

<div class="post-metadata">

**Author:** ![vantuz-subhuman](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.cardano.org/vantuz-subhuman/32/3976_2.png) [@vantuz-subhuman](https://forum.cardano.org/u/vantuz-subhuman)\
**Post date:** [4 June 2018 10:03 UTC](https://forum.cardano.org/t/how-to-query-based-on-block-height-and-listen-to-new-block-transaction-events-also-sockets-api/12472/13 "2018-06-04T10:03:37Z")

</div>

> [@Rahil\_Shaikh](#):
>
> I can use `/api/epochs/{epoch}/{slot}` api to get new blocks.
> 
> So I can have any block as my genesis block(starting point). Then my process would check the latest epoch/slot and then increment the slot by 1 to query the next block. (if the block is not yet formed, the slot is returned as empty array). Now once the slot no reaches the max\_slot\_per\_epoch I could increment the epoch by one and start with 1st slot.

Yeah, I think you may use it, if your program really requires having a block without any delay, so you gonna make network calls every 20 seconds anyway.

On the other hand - if your program does not require such **immediate** requirement - I would make rarer network calls and receive more data at once (block pages).

But I would not probably recommend this approach, since it might cause some complications:

> [@Rahil\_Shaikh](#):
>
> I would need to know for sure what is the max no of slots in an epoch. And would also need to keep track if at anytime this config changes.
> 
> What is the current config for max\_no\_of\_slots in an epoch?

Currently an epoch is 21600 **slots** (with 20 seconds per slot). Unfortunately I couldn’t find any automatic way to get the epoch length, except calling `/api/epochs/{epoch}` and checking if you have processed the same amount of blocks as total amount of pages =\

Be aware that slot-length might change when network transitions to Ouroboros Praos (no definite date yet), and idk about an epoch length.

### Also be aware:

Slots may be empty. The protocol does not guarantee that every slot will have a block in it, and Ouroboros Praos - in opposite - **guarantees** that some slots will remain empty. So your program must be ready for that.

That’s why I would consider processing blocks by **height** to be the recommended way of dealing with it.

---

<div class="post-metadata">

**Author:** ![Rahil\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@Rahil\_Shaikh](https://forum.cardano.org/u/Rahil_Shaikh)\
**Post date:** [4 June 2018 10:11 UTC](https://forum.cardano.org/t/how-to-query-based-on-block-height-and-listen-to-new-block-transaction-events-also-sockets-api/12472/14 "2018-06-04T10:11:07Z")

</div>

> That’s why I would consider processing blocks by height to be the recommended way of dealing with it.

Yup, that is the best way according me as-well, but the problem remains there is no api for getting blocks by height.  
You only get height in tx summary api call.

---

<div class="post-metadata">

**Author:** ![vantuz-subhuman](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.cardano.org/vantuz-subhuman/32/3976_2.png) [@vantuz-subhuman](https://forum.cardano.org/u/vantuz-subhuman)\
**Post date:** [4 June 2018 10:20 UTC](https://forum.cardano.org/t/how-to-query-based-on-block-height-and-listen-to-new-block-transaction-events-also-sockets-api/12472/15 "2018-06-04T10:20:05Z")

</div>

> [@Rahil\_Shaikh](#):
>
> Yup, that is the best way according me as-well, but the problem remains there is no api for getting blocks by height.  
> You only get height in tx summary api call.

Yes ☹ I meant just `GET /api/blocks/pages`

---

<div class="post-metadata">

**Author:** ![Rahil\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@Rahil\_Shaikh](https://forum.cardano.org/u/Rahil_Shaikh)\
**Post date:** [4 June 2018 10:41 UTC](https://forum.cardano.org/t/how-to-query-based-on-block-height-and-listen-to-new-block-transaction-events-also-sockets-api/12472/16 "2018-06-04T10:41:53Z")

</div>

> Currently an epoch is 21600 slots (with 20 seconds per slot).

Is there a configuration file that dictates this? In future if it changes what place can I refer?

---

<div class="post-metadata">

**Author:** ![vantuz-subhuman](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.cardano.org/vantuz-subhuman/32/3976_2.png) [@vantuz-subhuman](https://forum.cardano.org/u/vantuz-subhuman)\
**Post date:** [4 June 2018 10:47 UTC](https://forum.cardano.org/t/how-to-query-based-on-block-height-and-listen-to-new-block-transaction-events-also-sockets-api/12472/17 "2018-06-04T10:47:24Z")

</div>

> [@Rahil\_Shaikh](#):
>
> Is there a configuration file that dictates this? In future if it changes what place can I refer?

Here it may be found in the sources: [https://github.com/input-output-hk/cardano-sl/blob/master/explorer/frontend/src/Explorer/State.purs#L101](https://github.com/input-output-hk/cardano-sl/blob/master/explorer/frontend/src/Explorer/State.purs#L101) 🙂

I think you should also keep a close eye on the community, imo, it should definitely cause some noise if/when epoch/slot length would change 🙂 And keep an eye on technical reports.

---

<div class="post-metadata">

**Author:** ![quannd-ibl](https://avatars.discourse-cdn.com/v4/letter/q/65b543/32.png) [@quannd-ibl](https://forum.cardano.org/u/quannd-ibl)\
**Post date:** [22 June 2018 07:58 UTC](https://forum.cardano.org/t/how-to-query-based-on-block-height-and-listen-to-new-block-transaction-events-also-sockets-api/12472/18 "2018-06-22T07:58:11Z")

</div>

I have the same issue. Could you help me please? i can not connect port 8110 by node js socket.io-client

---

<div class="post-metadata">

**Author:** ![vantuz-subhuman](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.cardano.org/vantuz-subhuman/32/3976_2.png) [@vantuz-subhuman](https://forum.cardano.org/u/vantuz-subhuman)\
**Post date:** [22 June 2018 08:42 UTC](https://forum.cardano.org/t/how-to-query-based-on-block-height-and-listen-to-new-block-transaction-events-also-sockets-api/12472/19 "2018-06-22T08:42:10Z")

</div>

> [@quannd-ibl](#):
>
> i can not connect port 8110 by node js socket.io-client

Socket API seem to not work for the explorer, use the web API, please:

[https://cardanodocs.com/technical/explorer/api/](https://cardanodocs.com/technical/explorer/api/)

---

<div class="post-metadata">

**Author:** ![quannd-ibl](https://avatars.discourse-cdn.com/v4/letter/q/65b543/32.png) [@quannd-ibl](https://forum.cardano.org/u/quannd-ibl)\
**Post date:** [22 June 2018 09:44 UTC](https://forum.cardano.org/t/how-to-query-based-on-block-height-and-listen-to-new-block-transaction-events-also-sockets-api/12472/20 "2018-06-22T09:44:12Z")

</div>

if I want to use socket. What do I do? please help me.

[Next page](https://forum.cardano.org/t/how-to-query-based-on-block-height-and-listen-to-new-block-transaction-events-also-sockets-api/12472.md?page=2)
