C++ APISession methods

connect

Opens the websocket connection and selects a Blazeauth API server automatically.

connect opens the websocket connection for the current blaze::session. The library resolves a suitable server automatically and returns basic metadata about the selected server.

Function variants

blaze::api_server connect();
blaze::api_server connect(std::error_code& ec) noexcept;
void async_connect(std::function<void(std::error_code, blaze::api_server)> callback);
blaze::coro::awaitable_result<blaze::api_server> co_connect();

No API status here

connect does not return blaze::status. If connection setup fails, the failure is reported through std::error_code or std::system_error.

Result

struct api_server {
  std::string location;
};

Prop

Type

Behavior notes

  • connect opens the websocket transport and selects a server automatically.
  • After a successful connect, call initialize.
  • The returned location is informational. The library manages the actual connection endpoint internally.
  • After connect finishes, last_operation_took is updated with the measured connection/setup time, even if connect ends with an error.
  • Sync overloads use the session timeout configured in the constructor or via with_timeout(...).
  • Coroutine overloads are available only when the library is built with coroutine support.

Example

Compilation note

Synchronous and Asynchronous examples compile in the same form from C++14 to C++26.

#include "blazeauth/api/api.hpp"

#include <iostream>
#include <string>
#include <system_error>

void print_error_code(const std::string& context, const std::error_code& ec) {
  std::cout << context << ": " << ec.message() << " ("
            << ec.value() << " : " << ec.category().name() << ")\n";
}

int main() {
  blaze::session session;

  try {
    const blaze::api_server server = session.connect();

    std::cout << "Connected to server location: " << server.location << '\n';

    session.shutdown();
    std::cout << "Connection closed successfully\n";
  } catch (const std::system_error& e) {
    print_error_code("Connect failed", e.code());
    return 1;
  }

  return 0;
}
#include "blazeauth/api/api.hpp"

#include <future>
#include <iostream>
#include <string>
#include <system_error>

void print_error_code(const std::string& context, const std::error_code& ec) {
  std::cout << context << ": " << ec.message() << " ("
            << ec.value() << " : " << ec.category().name() << ")\n";
}

int main() {
  blaze::session session;

  std::promise<int> completion;
  std::future<int> result = completion.get_future();

  session.async_connect([&session, &completion](std::error_code ec, blaze::api_server server) {
    if (ec) {
      print_error_code("Connect failed", ec);
      completion.set_value(1);
      return;
    }

    std::cout << "Connected to server location: " << server.location << '\n';

    session.async_shutdown([&completion](std::error_code shutdown_ec) {
      if (shutdown_ec) {
        print_error_code("Shutdown failed", shutdown_ec);
        completion.set_value(1);
        return;
      }

      std::cout << "Connection closed successfully\n";
      completion.set_value(0);
    });
  });

  return result.get();
}

Project must be built with C++20 support and with coroutine support available both in the language mode and in the standard library implementation for this example to compile correctly.

#include "blazeauth/api/api.hpp"

#include <future>
#include <iostream>
#include <string>
#include <system_error>

void print_error_code(const std::string& context, const std::error_code& ec) {
  std::cout << context << ": " << ec.message() << " ("
            << ec.value() << " : " << ec.category().name() << ")\n";
}

#if BLAZEAUTH_HAS_COROUTINES

blaze::coro::task<std::error_code> run_example(blaze::session& session) {
  const auto [connect_ec, server] = co_await session.co_connect();
  if (connect_ec) {
    co_return connect_ec;
  }

  std::cout << "Connected to server location: " << server.location << '\n';

  const auto [shutdown_ec] = co_await session.co_shutdown();
  co_return shutdown_ec;
}

int main() {
  blaze::session session;

  std::promise<std::error_code> completion;
  std::future<std::error_code> result = completion.get_future();

  blaze::coro::co_spawn([&session, &completion]() -> blaze::coro::task<void> {
    const std::error_code ec = co_await run_example(session);
    completion.set_value(ec);
    co_return;
  }());

  const std::error_code ec = result.get();
  if (ec) {
    print_error_code("Connect failed", ec);
    return 1;
  }

  std::cout << "Connection closed successfully\n";
  return 0;
}

#else

int main() {
  std::cout << "This example requires coroutine support in the Blazeauth library build.\n";
  return 0;
}

#endif

Error handling

You can match generic failures against blaze::errc, for example: if (ec == blaze::errc::service_unavailable).

Error conditionWhen it can happen
already_connectedconnect was called on a session that is already connected.
timed_outA sync overload exceeded the configured session timeout while waiting for completion.
operation_would_blockA sync overload was called from the library I/O thread.
service_unavailableThe balancer reported that no currently usable server could be selected.
connection_lostNetwork or TLS transport failed while establishing the websocket session.
protocol_errorThe balancer response or websocket handshake was not valid for the expected protocol.

Balancer-specific error codes

When you need more detail than generic blaze::errc, connect may also surface raw balancer-specific std::error_code values from blaze::error::balancer_error.

Balancer errorWhen it can happen
internal_errorBalancer failure did not map to a more specific public availability code.
response_schema_mismatchBalancer returned JSON that did not match the schema expected by the library.
no_servers_availableBalancer has no usable servers to return.
no_servers_onlineMatching servers exist, but none of them are currently online.
no_servers_for_this_versionNo server is available for the API version used by this library.

On this page