1

I am currently emitting an event from my chaincode when adding an asset.

async addRequestNode(ctx, sampleAssetId, sampleData) {
    //console.info('============= Adding Sample Asset ===========');

    await ctx.stub.putState(sampleAssetId,sampleData);
    //console.info('============= Sample Asset Added ===========');
    ctx.stub.setEvent('sampleAssetAdded', 'sampleData');
}

Here as you can see, I am emiiting an event 'sampleAssetAdded'. I want to capture this event on my client application.

I have a server setup and is listening at port 8080. In the server I have instantiated the channelEventHub and have given my chaincode code ID and the event name.

    const channelEventHub = new ChannelEventHub('mychannel','peer0.org1.example.com');
    let eventCapture = channelEventHub.registerChaincodeEvent('fabcar','sampleAssetAdded',(event, block_num, txnid, status) => {



        console.log('Successfully got a chaincode event with transid:'+ txnid + ' with status:'+status);


        storeBlockNumForLater(block_num);


        let event_payload = event.payload.toString('utf8');
        if(event_payload.indexOf('CHAINCODE') > -1) {
            clearTimeout(handle);

            channel_event_hub.unregisterChaincodeEvent(regid);
            console.log('Successfully received the chaincode event on block number '+ block_num);
            resolve('RECEIVED');
        } else {
            console.log('Successfully got chaincode event ... just not the one we are looking for on block number '+ block_num);
        }
    },(error) => {

        console.log('Got Some Error'+error);

    })

But When I add the asset, the event is not captured. I don't know what is going wrong.

I even console logged eventCapture and got this

Event Capture ----> ChaincodeRegistration {
  ccid: 'fabcar',
  eventNameFilter: /sampleAssetAdded/,
  event_reg: 
   EventRegistration {
     _onEventFn: [Function],
     _onErrorFn: [Function],
     unregister: false,
     disconnect: false,
     unregister_action: [Function] } }

I am not sure if it's actually registering to the event or not.

I am using the Node SDK and I have referred this site https://fabric-sdk-node.github.io/release-1.4/tutorial-channel-events.html, but it is not at all helpful as they don't have the complete code and instead have vague snippets with incorrect or not working code.

Can someone please help on how to set this entire thing up and test it too.

Dheeraj Kumar
  • 374
  • 5
  • 14
  • 1
    would be nice if you could share the tutorial/your solution. Im kind of lost there too and would like to know a solution to that. Good luck! – kajuken Mar 08 '19 at 17:37
  • Yes, I will. Once I get this thing working, will try to make a Blog post or a small tutorial to help others out. – Dheeraj Kumar Mar 09 '19 at 11:14

2 Answers2

2

You shouldn't be instantiating a ChannelEventHub directly as it requires a user context in order to connect. You should get a channel event hub from the channel object using either

channel.getChannelEventHub(peer)

or

channel.newChannelEventHub(peer)

depending on whether you want the event hub cached in the channel object or not. Suggest reading the at https://fabric-sdk-node.github.io for each of these apis to explain more.

Next you need to connect it and specify true for full blocks rather than filtered blocks. Also you need to wait for the eventhub to be connected before you try committing a transaction that emits an event otherwise you won't receive it.

eventHub.connect(true, (err, eventHub) => {
   if (err) {
      // Error connecting
   } else {
      // connected successfully
   });

Once connected you then register for chaincode events.

david_k
  • 4,867
  • 2
  • 7
  • 15
  • Thank you for pointing out the mistake. Will try this and hopefully it should work. – Dheeraj Kumar Mar 09 '19 at 11:06
  • I tried implementing your solution, but I ran into some error. Can you have a look at it? I have posted another question which has a lot more details in it https://stackoverflow.com/questions/55099489/hyperledger-fabric-1-4-getting-an-error-while-initializing-channel-getchannelev – Dheeraj Kumar Mar 11 '19 at 10:13
0

You need to convert payload data into bytes ex.

async addRequestNode(ctx, sampleAssetId, sampleData) {
   await ctx.stub.putState(sampleAssetId,sampleData);
   ctx.stub.setEvent('sampleAssetAdded', Buffer.from('payload Data'));
}

and in client sdk you need to convert into string

await contract.addContractListener('sampleAssetAdded', 'sampleAssetAdded', (err, event, blkNum, txid, status, options) => {
  console.log('event received', status, event, blkNum, txid);  
  if (err) {
     this.emit('error', err);
  } else if (status && status === 'VALID') {
     console.log("payload ",event.payload.toString());
  }

}