Skip to Content

Subscribe to Hyperledger Fabric Chaincode Events

0 %
Subscribe to Hyperledger Fabric Chaincode Events

Subscribe to Hyperledger Fabric Chaincode Events

Emit chaincode events and subscribe to them using a NodeJS application.

You will learn

  • About Hyperledger Fabric chaincode events, including how they can be emitted in your chaincode
  • How to subscribe to chaincode events using the SAP API Hub

Step 1: Understand chaincode events

A blockchain network is a distributed system where multiple parties interact with each other. Instead of actively fetching the state of the ledger on a regular basis, chaincode events can be used to immediately notify when other parties interact with the chaincode by performing invokes.

Image depicting chaincode event overview

Each Hyperledger Fabric transaction can have one event attached to it. In a chaincode you can set an event via the stub.SetEvent(name string, payload []byte) function of the shim interface.


if err := stub.SetEvent("event-name", []byte("payload")); err != nil {
    return shim.Error(err.Error())
Log on to answer question
Step 2: Add event to the example chaincode
  1. In the Chaincode area of the channel dashboard, click on Example Chaincodes and select Hello World when prompted.

  2. Download the Example Chaincode and extract it.

  3. Edit the hello_world.go file and add an event at the end of the write function.

Image depicting chaincode download
Which of the following parameters can be set when emitting an event?
Step 3: Deploy and test chaincode
  1. To ensure that the new chaincode does not conflict with an existing one, edit the chaincode.yaml file and modify the Id to e.g. com-sap-icn-blockchain-example-helloWorld-events.

  2. Package your chaincode and install and instantiate the chaincode on your channel.

  3. Once completed, invoke the write function and navigate to the Explore area to verify that the event was written.

Image depicting explore area with chaincode event
Log on to answer question
Step 4: Create chaincode event subscribe application

Using one of your channels’ service keys (found under the Channel area), create an accessToken.

Now, save the example application to subscriber.js and set both the accessToken and the chaincodeId in the following example application.

const WebSocket = require('ws');

const accessToken = '< access token >';
const chaincodeId = 'com-sap-icn-blockchain-example-helloWorld-events'; // chaincode with events
const url = 'wss://' + chaincodeId + '/events';
const client = new WebSocket(url, {
    headers: {
        Authorization: 'Bearer ' + accessToken,

client.on('open', function () {
    console.log('Connection open.');
client.on('message', function (data) {
client.on('close', function (code, reason) {
    console.log('Connection closed.');
    console.log(code, reason);
Log on to answer question
Step 5: Subscribe to events
  1. Start the example application with the following command:

    node subscriber.js

  2. Now, invoke the write function of your chaincode. The application should then print the emitted event to the console.

Log on to answer question
Step 6: Discover optional API parameters

The chaincode events endpoint accepts two optional query parameters:

Parameter Name Example Description
block block=10 Returns all chaincode events that happened since block number 10.
filter filter=write Returns only chaincode events where the event name matches the regular expression write. Note: The regular expression has to be URL encoded, e.g., .* is encoded to .%2A.

Use the block parameter to fetch older events. This is useful when your application was offline for a short period of time and wants to catch up.

To stream the events starting from block 3, use /events?block=3.

Log on to answer question

Next Steps

Back to top