ObjectBox C and C++ API  4.0.3
obx::SyncServer Class Reference

The ObjectBox Sync Server to run within your application (embedded server). Note that you need a special sync edition, which includes the server components. Check https://objectbox.io/sync/. More...

#include <objectbox-sync.hpp>

Inheritance diagram for obx::SyncServer:
Inheritance graph

Public Member Functions

 SyncServer (Options &storeOptions, const std::string &url)
 Prepares an ObjectBox Sync Server to run within your application (embedded server) at the given URI. This call opens a store with the given options (as Store() does). Get it via store(). The server's store is tied to the server itself and is closed when the server is closed. Before actually starting the server via start(), you can configure: More...
 
 SyncServer (Options &&storeOptions, const std::string &url)
 Rvalue variant of SyncServer(Options& storeOptions, const std::string& url) that works equivalently. More...
 
 SyncServer (Options &storeOptions, const void *flatOptions, size_t flatOptionsSize)
 
 SyncServer (Options &&storeOptions, const void *flatOptions, size_t flatOptionsSize)
 Rvalue variant of SyncServer(Options& storeOptions, const void* flatOptions, size_t flatOptionsSize) that works equivalently. More...
 
 SyncServer (SyncServer &&source) noexcept
 
 SyncServer (const SyncServer &)=delete
 Can't be copied, single owner of C resources is required (to avoid double-free during destruction) More...
 
 ~SyncServer () override
 
Storestore ()
 The store that is associated with this server. More...
 
void close () final
 Closes and cleans up all resources used by this sync server. Does nothing if already closed. It can no longer be used afterwards, make a new sync server instead. More...
 
bool isClosed () override
 Returns if this sync server is closed and can no longer be used. More...
 
void setCertificatePath (const std::string &path)
 Sets SSL certificate for the server to use. Use before start(). More...
 
void setCredentials (const SyncCredentials &creds)
 Sets credentials for the server to accept. Use before start(). More...
 
void enableAuthenticationType (OBXSyncCredentialsType type)
 Sets credentials for the server to accept. Use before start(). More...
 
void setWorkerThreads (int threadCount)
 Sets the number of worker threads. Calll before start(). More...
 
void setHistoryMaxSizeKb (uint64_t maxSizeKb, uint64_t targetSizeKb=0)
 Sets a maximum size for sync history entries to limit storage: old entries are removed to stay below this limit. Deleting older history entries may require clients to do a full sync if they have not contacted the server for a certain time. More...
 
void start ()
 Once the sync server is configured, you can "start" it to start accepting client connections. This method triggers communication in the background and will return immediately. More...
 
void stop ()
 Stops this sync server. Does nothing if it is already stopped. More...
 
bool isRunning ()
 Returns if this sync server is running. More...
 
std::string url ()
 Returns a URL this server is listening on, including the bound port (see port(). More...
 
uint16_t port ()
 Returns a port this server listens on. This is especially useful if the bindUri given to the constructor specified "0" port (i.e. automatic assignment). More...
 
uint64_t connections ()
 Returns the number of clients connected to this server. More...
 
std::string statsString (bool includeZeroValues=true)
 Get server runtime statistics. More...
 
uint64_t statsValueU64 (OBXSyncServerStats counterType)
 Get u64 value for sync server statistics. More...
 
double statsValueF64 (OBXSyncServerStats counterType)
 Get double value for sync server statistics. More...
 
void setChangeListener (std::shared_ptr< SyncChangeListener > listener)
 
void setObjectsMessageListener (std::shared_ptr< SyncObjectsMessageListener > listener)
 
void send (SyncObjectsMessageBuilder &&message)
 Broadcast the given 'objects message' from the server to all currently connected (and logged-in) clients. More...
 
OBX_sync_servercPtr () const
 
- Public Member Functions inherited from obx::Closable
virtual ~Closable ()=default
 

Static Public Member Functions

static bool isAvailable ()
 

Detailed Description

The ObjectBox Sync Server to run within your application (embedded server). Note that you need a special sync edition, which includes the server components. Check https://objectbox.io/sync/.

Constructor & Destructor Documentation

◆ SyncServer() [1/6]

obx::SyncServer::SyncServer ( Options storeOptions,
const std::string &  url 
)
inlineexplicit

Prepares an ObjectBox Sync Server to run within your application (embedded server) at the given URI. This call opens a store with the given options (as Store() does). Get it via store(). The server's store is tied to the server itself and is closed when the server is closed. Before actually starting the server via start(), you can configure:

  • accepted credentials via setCredentials() (always required)
  • SSL certificate info via setCertificatePath() (required if you use wss)
    Note
    The model given via store_options is also used to verify the compatibility of the models presented by clients. E.g. a client with an incompatible model will be rejected during login.
    Parameters
    storeOptionsOptions for the server's store. Will be "consumed"; do not use the Options object again.
    urlThe URL (following the pattern protocol:://IP:port) the server should listen on. Supported protocols are "ws" (WebSockets) and "wss" (secure WebSockets). To use the latter ("wss"), you must also call obx_sync_server_certificate_path(). To bind to all available interfaces, including those that are available from the "outside", use 0.0.0.0 as the IP. On the other hand, "127.0.0.1" is typically (may be OS dependent) only available on the same device. If you do not require a fixed port, use 0 (zero) as a port to tell the server to pick an arbitrary port that is available. The port can be queried via obx_sync_server_port() once the server was started. Examples: "ws://0.0.0.0:9999" could be used during development (no certificate config needed), while in a production system, you may want to use wss and a specific IP for security reasons.

◆ SyncServer() [2/6]

obx::SyncServer::SyncServer ( Options &&  storeOptions,
const std::string &  url 
)
inlineexplicit

Rvalue variant of SyncServer(Options& storeOptions, const std::string& url) that works equivalently.

◆ SyncServer() [3/6]

obx::SyncServer::SyncServer ( Options storeOptions,
const void *  flatOptions,
size_t  flatOptionsSize 
)
inlineexplicit

◆ SyncServer() [4/6]

obx::SyncServer::SyncServer ( Options &&  storeOptions,
const void *  flatOptions,
size_t  flatOptionsSize 
)
inlineexplicit

◆ SyncServer() [5/6]

obx::SyncServer::SyncServer ( SyncServer &&  source)
inlinenoexcept

◆ SyncServer() [6/6]

obx::SyncServer::SyncServer ( const SyncServer )
delete

Can't be copied, single owner of C resources is required (to avoid double-free during destruction)

◆ ~SyncServer()

obx::SyncServer::~SyncServer ( )
inlineoverride

Member Function Documentation

◆ close()

void obx::SyncServer::close ( )
inlinefinalvirtual

Closes and cleans up all resources used by this sync server. Does nothing if already closed. It can no longer be used afterwards, make a new sync server instead.

Implements obx::Closable.

◆ connections()

uint64_t obx::SyncServer::connections ( )
inline

Returns the number of clients connected to this server.

◆ cPtr()

OBX_sync_server* obx::SyncServer::cPtr ( ) const
inline

◆ enableAuthenticationType()

void obx::SyncServer::enableAuthenticationType ( OBXSyncCredentialsType  type)
inline

Sets credentials for the server to accept. Use before start().

Parameters
datamay be NULL in combination with OBXSyncCredentialsType_NONE

◆ isAvailable()

static bool obx::SyncServer::isAvailable ( )
inlinestatic

◆ isClosed()

bool obx::SyncServer::isClosed ( )
inlineoverridevirtual

Returns if this sync server is closed and can no longer be used.

Implements obx::Closable.

◆ isRunning()

bool obx::SyncServer::isRunning ( )
inline

Returns if this sync server is running.

◆ port()

uint16_t obx::SyncServer::port ( )
inline

Returns a port this server listens on. This is especially useful if the bindUri given to the constructor specified "0" port (i.e. automatic assignment).

◆ send()

void obx::SyncServer::send ( SyncObjectsMessageBuilder &&  message)
inline

Broadcast the given 'objects message' from the server to all currently connected (and logged-in) clients.

◆ setCertificatePath()

void obx::SyncServer::setCertificatePath ( const std::string &  path)
inline

Sets SSL certificate for the server to use. Use before start().

◆ setChangeListener()

void obx::SyncServer::setChangeListener ( std::shared_ptr< SyncChangeListener listener)
inline

◆ setCredentials()

void obx::SyncServer::setCredentials ( const SyncCredentials creds)
inline

Sets credentials for the server to accept. Use before start().

Parameters
datamay be NULL in combination with OBXSyncCredentialsType_NONE

◆ setHistoryMaxSizeKb()

void obx::SyncServer::setHistoryMaxSizeKb ( uint64_t  maxSizeKb,
uint64_t  targetSizeKb = 0 
)
inline

Sets a maximum size for sync history entries to limit storage: old entries are removed to stay below this limit. Deleting older history entries may require clients to do a full sync if they have not contacted the server for a certain time.

Parameters
maxSizeKbOnce this maximum size is reached, old history entries are deleted (default 0: no limit).
targetSizeKbIf this value is non-zero, the deletion of old history entries is extended until reaching this target (lower than the maximum) allowing deletion "batching", which may be more efficient. If zero, the deletion stops already stops when reaching the max size (or lower).

◆ setObjectsMessageListener()

void obx::SyncServer::setObjectsMessageListener ( std::shared_ptr< SyncObjectsMessageListener listener)
inline

◆ setWorkerThreads()

void obx::SyncServer::setWorkerThreads ( int  threadCount)
inline

Sets the number of worker threads. Calll before start().

Parameters
thread_countThe default is "0" which is hardware dependent, e.g. a multiple of CPU "cores".

◆ start()

void obx::SyncServer::start ( )
inline

Once the sync server is configured, you can "start" it to start accepting client connections. This method triggers communication in the background and will return immediately.

◆ statsString()

std::string obx::SyncServer::statsString ( bool  includeZeroValues = true)
inline

Get server runtime statistics.

◆ statsValueF64()

double obx::SyncServer::statsValueF64 ( OBXSyncServerStats  counterType)
inline

Get double value for sync server statistics.

◆ statsValueU64()

uint64_t obx::SyncServer::statsValueU64 ( OBXSyncServerStats  counterType)
inline

Get u64 value for sync server statistics.

◆ stop()

void obx::SyncServer::stop ( )
inline

Stops this sync server. Does nothing if it is already stopped.

◆ store()

Store& obx::SyncServer::store ( )
inline

The store that is associated with this server.

◆ url()

std::string obx::SyncServer::url ( )
inline

Returns a URL this server is listening on, including the bound port (see port().

Member Data Documentation

◆ change

std::shared_ptr<SyncChangeListener> obx::SyncServer::change

◆ mutex

std::mutex obx::SyncServer::mutex

◆ object

std::shared_ptr<SyncObjectsMessageListener> obx::SyncServer::object