client-cpp  0.10.0
Event subsystem

Endpoint is able to send or receive events when it is attached to user. See User registration.

Auto-generated headers based on selected event class family schemas can be found in kaa/event/gen/.

Event classes definitions are separated into specific namespace for each event class family. Name of this namespace is defined as:
nsEventClassFamilyName

Usage

For the example, we have one event class family named "ExampleClassFamily":

[
{
"namespace": "org.kaa.example.events",
"name": "TestEvent",
"type": "record",
"fields": []
}
]

Including header for ExampleClassFamily:

#include <kaa/event/gen/ExampleClassFamily.hpp>

Getting ExampleClassFamily instance:

using namespace kaa;
ExampleClassFamily &classFamily = Kaa::getKaaClient().getEventFamilyFactory().getExampleClassFamily();

Sending an event

Sending event to all available recipients:

nsExampleClassFamily::TestEvent event;
classFamily.sendEvent(event);

or

nsExampleClassFamily::TestEvent event;
classFamily.sendEventToAll(event);

Sending event to a concrete target:

std::string target = "lZjEzq4E/D5aWjXYuG1N2sKYt/U="; // Target's public key hash.
nsExampleClassFamily::TestEvent event;
classFamily.sendEvent(event, target);

Sending bulk of events.
Event blocks in sdk are identified by TransactionId key.

Create new empty events block:

TransactionIdPtr blockId = Kaa::getKaaClient().getEventFamilyFactory().startEventsBlock();

Events block can contain events from different event class families.
Add events to a block:

nsExampleClassFamily::TestEvent event1, event2;
// Sending event1 to a concrete target and broadcasting event2 to all endpoints.
std::string target = "lZjEzq4E/D5aWjXYuG1N2sKYt/U="; // Target's public key hash.
classFamily.addEventToBlock(blockId, event1, target);
classFamily.addEventToBlock(blockId, event2);

If events block is completed use next call to send events:

Kaa::getKaaClient().getEventFamilyFactory().submitEventsBlock(blockId);

In order to remove events block (events will not be sent) use:

Kaa::getKaaClient().getEventFamilyFactory().removeEventsBlock(blockId);

Receiving an event

Define event listener:

class ExampleEventsListener : public ExampleClassFamily::ExampleClassFamilyListener {
public:
void onEvent(const nsExampleClassFamily::TestEvent &event, const std::string & source = "") {
std::cout << "Received event!" << std::endl;
}
};

Register event listener:

ExampleClassFamily::ExampleClassFamilyListener * listener = new ExampleEventsListener();
classFamily.addEventFamilyListener(&listener);

Searching for event recipients

class EventRecipientsResolveListener : public IFetchEventListeners {
public:
void onEventListenersReceived(const std::vector<std::string >& eventListeners) {
for (const auto &target : eventListeners) {
std::cout << "Recipient key hash = " << target << std::endl;
}
}
void onRequestFailed() {
std::cout << "Failed to find recipients of event org.kaa.example.events.TestEvent" << std::endl;
}
};
IFetchEventListeners *listener = new EventRecipientsResolveListener();
std::list<std::string > fqns = { "org.kaa.example.events.TestEvent" };
Kaa::getKaaClient().getEventListenersResolver().findEventListeners(fqns, listener);

NOTE: Passing multiple events fqns means that recipient MUST support receiving ALL mentioned events.

References

User registration