addListener

Adds a listener to an event. When an event is triggered, listeners are triggered in the order in which they were added. Theses listener will receive a const std::string as argument. This string is a JSON payload representing the event.

Arguments

Copied to clipboard!
virtual kuzzleio::KuzzleEventEmitter* addListener(
    kuzzleio::Event event,
    kuzzleio::SharedEventListener listener);

Argument Type Description
event
kuzzleio::Event
An enum representing the listened event
listener
kuzzleio::SharedEventListener
Smart pointer to a c++11 lambda

event

One of the following event:

Copied to clipboard!
KUZZLE_EVENT_CONNECTED,
KUZZLE_EVENT_DISCARDED,
KUZZLE_EVENT_DISCONNECTED,
KUZZLE_EVENT_LOGIN_ATTEMPT,
KUZZLE_EVENT_NETWORK_ERROR,
KUZZLE_EVENT_OFFLINE_QUEUE_POP,
KUZZLE_EVENT_OFFLINE_QUEUE_PUSH,
KUZZLE_EVENT_QUERY_ERROR,
KUZZLE_EVENT_RECONNECTED,
KUZZLE_EVENT_JWT_EXPIRED,
KUZZLE_EVENT_ERROR

listener

EventListener is defined as const std::function<void(const std::string)>

SharedEventListener is defined as std::shared_ptr<EventListener>

Return

The Kuzzle instance.

Usage

Copied to clipboard!
{
  using kuzzleio::SharedEventListener;
  using kuzzleio::EventListener;

  SharedEventListener listener = std::make_shared<EventListener>(
    [](const std::string &payload) {
      std::cout << payload << std::endl;
    });

  SharedEventListener other_listener = std::make_shared<EventListener>(
    [](const std::string &payload) {
      std::cerr << payload << std::endl;
    });

  kuzzle
    ->addListener(kuzzleio::KUZZLE_EVENT_CONNECTED, listener)
    ->addListener(kuzzleio::KUZZLE_EVENT_CONNECTED, other_listener);
}

std::cout << "Listener successfully added" << std::endl;