Class: EventService

EventService

EventService is used to monitor for new blocks on a peer's ledger. The class allows the user to register a listener to be notified when a new block is added to the ledger, when a new block is added that has a specific transaction ID, or to be notified when a transaction contains a chaincode event name of interest. The class also allows the monitoring to start and end at any specific location.

new EventService(name, channel)

Constructs a EventService object
Parameters:
Name Type Description
name string
channel Channel An instance of the Channel class were this EventService will receive blocks
Returns:
An instance of this class
Type
EventService

Extends

Methods


build(idContext, options)

This method is used to build the protobuf objects of the start request. The start request must next be signed before being sent to the peer's event service. The Proposal#buildAndSignStartRequest method should be used if the signing will be done by the application.
Parameters:
Name Type Description
idContext IdentityContext The transaction context to use for Identity, transaction ID, and nonce values
options StartRequestOptions The build
Overrides:
Returns:
The start request bytes that need to be signed.
Type
Array.<byte>

close()

Disconnects the EventService from the fabric peer service and closes all services. Will close all event listeners and send an Error to all active listeners.

getTransactionListener(txid)

Utility method to find an event listener for a specific transaction ID
Parameters:
Name Type Description
txid string the transaction ID of the event listener being searched.
Returns:
The EventListener for the transaction ID provided
Type
EventListener

hasListeners()

Use this method to indicate if this event service has event listeners EventListener assigned and waiting for an event.

isInUse()

Use this method to indicate if application has already started using this service. The service will have been asked to build the service request and will not have commpleted the service startup.

isStarted()

Use this method to indicate if this event service has an event endpoint Eventer assigned and the event endpoint has a listening stream connected and active.

registerBlockListener(callback, options)

Register a listener to receive all blocks committed to this channel. The listener's "callback" function gets called on the arrival of every block.
Parameters:
Name Type Description
callback EventCallback
options EventRegistrationOptions Options on the registrations to allow for start and end block numbers or to automatically unregister
Returns:
The EventListener instance to be used to remove this registration using EventService#unregisterEvent)
Type
EventListener

registerChaincodeListener(eventName, callback, options)

Register a listener to receive chaincode events.
Parameters:
Name Type Description
eventName string | RegExp The exact name of the chaincode event or regular expression that will be matched against the name given to the target chaincode's call stub.SetEvent(name, payload))
callback EventCallback
options EventRegistrationOptions Options on the registrations to allow for start and end block numbers, automatically unregister.
Returns:
The EventListener instance to be used to remove this registration using EventService#unregisterEvent)
Type
EventListener

registerTransactionListener(txid, callback, options)

Register a callback function to receive a notification when the transaction by the given id has been committed into a block. Using the special string 'all' will indicate that this listener will notify (call) the callback for every transaction written to the ledger.
Parameters:
Name Type Description
txid string Transaction id string or 'all'
callback EventCallback
options EventRegistrationOptions Options on the registrations to allow for start and end block numbers or to automatically unregister.
Returns:
The EventListener instance to be used to remove this registration using EventService#unregisterEvent)
Type
EventListener

<async> send(request)

This method will have this events start listening for blocks from the Peer's event service. It will send a Deliver request to the peer event service and start the grpc streams. The received blocks will be checked to see if there is a match to any of the registered listeners.
Parameters:
Name Type Description
request StartEventRequest The request options to start the stream to the event service.
Overrides:

setTargets()

Use this method to set the ServiceEndpoint for this ServiceAction class The Eventer a ServiceEndpoint must be connected before making this assignment.
Properties:
Name Type Description
targets Array.<Eventer> The connected Eventer instances to be used when no targets are provided on the send.

sign(param)

Use this method with an IdentityContext that contains a User that has a Signing Identity. OR Use this method with a byte[] to set the signature when the application has done the signed externally. Use the results of the build as the bytes that will be signed.
Parameters:
Name Type Description
param IdentityContext | Array.<byte> When 'param' is a IdentityContext the signing identity of the user will sign the current build bytes. When the 'param' is a byte[], the bytes will be used as the final commit signature.
Inherited From:
Overrides:

toString()

implementing class must implement
Inherited From:
Overrides:

unregisterEventListener(eventListener [, notThrow])

Unregister the event listener returned by the register listener methods.
Parameters:
Name Type Argument Description
eventListener EventListener The registered listener.
notThrow boolean <optional>
When the listener is not found an error will be thrown when not included or false