The event interface in the BaaS Java SDK lets you subscribe to and receive notifications for account, contract, topic, and block events on the blockchain. Events are delivered through an EventHandler callback, which is invoked when a response arrives, a timeout occurs, or an error is returned.
How it works
Each subscription method registers a listener on the BaaS node. When a matching event occurs, the node calls your EventHandler.handle(Message message) implementation with an event-specific message object.
The EventModelType parameter controls the delivery mode:
| Value | Mode | Description |
|---|---|---|
PULL | Pull | The client periodically polls the node for new events. |
PUSH | Push | The node actively pushes events to the client as they occur. |
Use PUSH for real-time event processing. Use PULL when your application polls on its own schedule.
All subscribe methods return a boolean: true if the subscription was registered successfully, false if an error occurred.
listenAccount
Subscribes to events for a specific account.
Signature
public boolean listenAccount(Identity identity, EventHandler handler, EventModelType type)Parameters
| Name | Required | Type | Description |
|---|---|---|---|
identity | Yes | Identity | The business ID of the account to subscribe to. |
handler | Yes | EventHandler | Callback invoked when a response is received, a timeout occurs, or an error is returned. |
type | Yes | EventModelType | The event delivery mode. Valid values: PULL, PUSH. |
Response
| Name | Type | Description |
|---|---|---|
result | boolean | true: subscription registered. false: error. |
Example
// Define the event handler
EventHandler handler = new EventHandler() {
@Override
public void handle(Message message) {
EventAccountMessage eventAccountMessage = (EventAccountMessage) message;
// Process the account event
}
};
// Specify the account identity (account: Tester001)
Identity identity = new Identity("c60a9d48105950a0cca07a4c6320b98c303ad42d694a634529e8e1a0a16fcdb5");
// Subscribe using PUSH mode
boolean result = sdk.getEventService().listenAccount(identity, handler, EventModelType.PUSH);listenContract
Subscribes to events for a specific smart contract.
Signature
public boolean listenContract(Identity identity, EventHandler handler, EventModelType type)Parameters
| Name | Required | Type | Description |
|---|---|---|---|
identity | Yes | Identity | The business ID of the contract to subscribe to. |
handler | Yes | EventHandler | Callback invoked when a response is received, a timeout occurs, or an error is returned. |
type | Yes | EventModelType | The event delivery mode. Valid values: PULL, PUSH. |
Response
| Name | Type | Description |
|---|---|---|
result | boolean | true: subscription registered. false: error. |
Example
// Define the event handler
EventHandler handler = new EventHandler() {
@Override
public void handle(Message message) {
EventContractMessage eventContractMessage = (EventContractMessage) message;
// Process the contract event
}
};
// Specify the contract identity
Identity identity = new Identity("a7937b64b8caa58f03721bb6bacf5c78cb235febe0e70b1b84cd99541461a08e");
// Subscribe using PUSH mode
boolean result = sdk.getEventService().listenContract(identity, handler, EventModelType.PUSH);listenTopics
Subscribes to events for one or more topics.
Signature
public boolean listenTopics(List<String> topics, EventHandler handler, EventModelType type)Parameters
| Name | Required | Type | Description |
|---|---|---|---|
topics | Yes | List<String> | The list of topics to subscribe to. |
handler | Yes | EventHandler | Callback invoked when a response is received, a timeout occurs, or an error is returned. |
type | Yes | EventModelType | The event delivery mode. Valid values: PULL, PUSH. |
Response
| Name | Type | Description |
|---|---|---|
result | boolean | true: subscription registered. false: error. |
Example
// Define the event handler
EventHandler handler = new EventHandler() {
@Override
public void handle(Message message) {
EventTopicsMessage eventTopicsMessage = (EventTopicsMessage) message;
// Process the topic event
}
};
// Build the topic list
List<String> topics = new ArrayList<>();
topics.add("call_contract");
// Subscribe using PUSH mode
boolean result = sdk.getEventService().listenTopics(topics, handler, EventModelType.PUSH);listenBlock
Subscribes to block events.
Signature
public boolean listenBlock(EventHandler handler, EventModelType type)Parameters
| Name | Required | Type | Description |
|---|---|---|---|
handler | Yes | EventHandler | Callback invoked when a response is received, a timeout occurs, or an error is returned. |
type | Yes | EventModelType | The event delivery mode. Valid values: PULL, PUSH. |
Response
| Name | Type | Description |
|---|---|---|
result | boolean | true: subscription registered. false: error. |
Example
// Define the event handler
EventHandler handler = new EventHandler() {
@Override
public void handle(Message message) {
// Process the block event
}
};
// Subscribe using PUSH mode
boolean result = sdk.getEventService().listenBlock(handler, EventModelType.PUSH);unListenAccount
Unsubscribes from account events.
Signature
sdk.getEventService().unListenAccount(identity)Parameters
| Name | Required | Type | Description |
|---|---|---|---|
identity | Yes | Identity | The identity of the account to unsubscribe. |
unListenContract
Unsubscribes from contract events.
Signature
public boolean unListenContract(Identity identity)Parameters
| Name | Required | Type | Description |
|---|---|---|---|
identity | Yes | Identity | The identifier of the contract subscription to cancel. |
unListenTopics
Unsubscribes from topic events.
Signature
public boolean unListenTopics(List<String> topics)Parameters
| Name | Required | Type | Description |
|---|---|---|---|
topics | Yes | List<String> | The list of topics to unsubscribe. |
unListenBlock
Unsubscribes from block events.
Signature
public boolean unListenBlock()