0

I am trying to make events work in Hyperledger Fabric. I have written the setEvent function in my chaincode and added a listener (addContractListener) in my application file. Nothing seems to happen

In my contract file, right after a putState function :

await this.ctx.stub.setEvent('event1', data);

In my application file, right after the submitTransaction function:

let eventTxn = await contract.addContractListener('some-string', 'trade-network',
        (err, event, blkNum, txid, status) => {
            console.log('event received', status, event, blkNum, txid);
            if (err) {
                this.emit('error', err);
            } else if (status && status === 'VALID') {
                // only if a valid block is committed should we emit an event
                let evt = event.payload.toString('utf8');
                evt = JSON.parse(evt);
                if (Array.isArray(evt)) {
                    for(const oneEvent of evt) {
                        this.emit('ChaincodeEvent', oneEvent);
                    }
                }
                else {
                    this.emit('ChaincodeEvent', evt);
                }
            }
       },
       {filtered: false}
    );

I have added the above lines of code in my contract and application. On executing the application function, nothing seems to happen and the program is stuck returning no response. Even the 'console.log' isn't executed which means it didnt enter the function fully.

I was expecting the application function to get executed. Could someone guide me as to how to make a simple event work in Hyperledger Fabric?

Yash Damani
  • 105
  • 3
  • 12
  • 1
    addContractListener starts listenning from latest block. Are you sure you have this event in latest block? Have you tried sending the transaction AFTER starting the listener? – Itération 122442 Jun 20 '19 at 05:55
  • @FlorianCastelain Yes, tried sending the transaction after starting the listener. And regarding the event being in the latest block, how do I check that? I have written it after the putState in my contract, so I am hoping it is in the latest block? The function 'addContractListener' is just being skipped and the rest of the code executes. But the response doesn't go and it just hangs. Can you share any resource/code snippet on how to set and listen to events using javascript? Thanks! – Yash Damani Jun 20 '19 at 06:27
  • 1
    Maybe there is a mismatch between the name of the event of the chaincode`event1` and the event that you are listening to `trade-network`. – Leonardo Carraro Jun 20 '19 at 06:30
  • Ohhhh! My bad. Yes! That Worked! thank you so much. @FlorianCastelain – Yash Damani Jun 20 '19 at 06:56
  • @LeonardoCarraro That was a horrible mistake. Thanks for pointing it out. – Yash Damani Jun 20 '19 at 06:57

1 Answers1

1

Maybe there is a mismatch between the name of the event of the chaincode event1 and the event that you are listening to trade-network

Leonardo Carraro
  • 1,532
  • 1
  • 11
  • 24