Event subscription


#1

Hello again.

Is it currently possible to fire an event and then subscribe for it from the SDK (or poll for the results). If so, can you be please so kind to tell me how.


#2

I know the official middleware has websockets to which you can subscribe and it will inform you of changes in states.

But this is not part of the main node. you could check if the middleware helps you with this https://github.com/aeternity/aepp-middleware


#3

Hey, thanks for answering. I know about the middleware, but the description of the websockets leads me to the conclusion that the event listening is not supported. And that’s why I am asking for an example.

The middleware permits you to subscribe to events via a websocket, and receive updates when the chain state changes. Currently you can subscribe to key blocks, micro blocks, and soon to all events involving a particular on-chain object (contract, account, ...).

#4

As it says, you can currently subscribe to keyblocks and microblocks.


#5

And I don’t need that. That’s why I opened this thread. Can anyone actually provide me an answer?


#6

do you mean events in smart contracts? then yes, this is possible and documented here:

regarding the SDK supporting subscription. I assume this is not available ATM. but I think @noandrea can answer this question best :slight_smile:


#7

Yes, I want to subscribe or read past events fired from the smart contract.


#8

Hi,

It’s nice to meet you guys.


#9

Hey @kraykov we will answer shortly, thanks.


#10

Still waiting for an answer.


#11

Hello @kraykov, the middleware does not support subscription to events and indexing yet. This shouldn’t keep you from using them though, the node can return all emitted events for a call via the transactions/{tx_hash}/info endpoint.


#12

@piwo How can I do that without knowing the tx hash? Because if I know the tx hash I wouldn’t need the event info.


#13

why don’t you know the transaction hash for a transaction you did?

If you need to find a transaction hash you want to consider you can do this using the middleware.


#14

Okay, if you are going to ask me questions on something we have thought about for a long time I’ll directly give you the use case and then maybe you can give me better directions how to accomplish it.

The use case:

  1. There are 100 active users
  2. One user makes an action after which the other 99 users needs to be notified.

Long polling on the client side is not an option, because it will take the node down (let’s assume it’s not 100 but 100 000)


#15

Sounds like this is not a task that is to be solved by using a blockchain protocol but some indexing infrastructure.

I would advise you to use a trustless backend for this task, that takes care of indexing and notifying and is probably connected via websockets.


#16

Exactly. That’s why I need event subscription or a possibility to read past events (without using tx hash)


#17

what keeps you from building this? index your transactions, then create your interface to subscribe to.

/transactions/{tx_hash}/info provides you the information from events.


#18

Because that’s not the correct way of doing this. This will create a lot of attack vectors, that can be avoided by simply having an event subscription. Or at least reading all transactions made to a certain contract (maybe at certain block height).


#19

where would you suggest this event subscription to be made? why should this provider be less of an attack vector than having a backend?


#20

Because the service will subscribe for events independently from the client. Clients will subscribe for events using that “backend” service therefore the data will be always correct because:

  1. users cannot interfere with the “backend” service
  2. the service will read the data directly from the network only once for every user.