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.