1

In my chaincode I am emitting an event with the following command

ctx.stub.setEvent('sampleEvent', 'somedata');

I want to Listen to this event on the client application using the Node SDK.

I have the following questions,

  • Do I need to use ChannelEventHub?

  • If yes, from where do I import/require it?

  • Which npm module do I need?

  • I believe, the channelEventHub has the registerChaincodeEvent method, are there any examples on how to use it?

Dheeraj Kumar
  • 374
  • 5
  • 14

1 Answers1

3

I can strongly recommend this page. There you will also find tutorials on how to implement it.

The package responsible for ChannelEventHub is the fabric-client.

npm install fabric-client

And using it in code:

var Fabric_Client = require('fabric-client');

Also you probably want to install fabric-ca-client for user registration/enrolling as well. As stated above you will find everything necessary on the page of the provided first link.

Hope that helps!

kajuken
  • 279
  • 2
  • 10
  • This was the same tutorial I was following and because it was not helpful I came here asking for help. But nonetheless. you cleared some of the doubts. Thank you. And this tutorial not very helpful. – Dheeraj Kumar Mar 08 '19 at 09:36
  • In the tutorial they say to use const channel_event_hub = channel.newChannelEventHub('peer0.org1.example.com'); Without ever mentioning Where to import channel from. And then when I imported channel from the Fabric-Client module and tried to instantiate the ChannelEventHub using the given command I get the following error TypeError: channel.newChannelEventHub is not a function. – Dheeraj Kumar Mar 08 '19 at 09:38
  • 1
    After playing around with the module functions, I found that this is the right command to instantiate the ChannelEventHub --> const channelEventHub = new ChannelEventHub('mychannel','peer0.org1.example.com'); ChannelEventHub was imported from the Fabric-Client module. Hope this helps anyone trying to register to chaincode events. – Dheeraj Kumar Mar 08 '19 at 09:40