Skip to content

Real-Time Database Overview

As users work with data in your application, the client program and the server-side business logic may create new, update or delete existing objects in the database. In some cases, it is important to instantly reflect these changes for other users of the app. For example, suppose a mobile and/or web app displays remaining available quantity for a product in an online store. As users purchase the product, it is important to update the remaining quantity for all other users of the app. The Backendless RT (Real-Time) Database system enables Backendless client applications to maintain constant real-time synchronization with the Backendless database.

Important

The Real-Time Database functionality introduces a new library dependency - socket.io. Make sure to read the Client-side Setup section for more information.

The Backendless RT system relies on listeners which can be registered at a table level. A listener is notified when an object is created, updated or deleted. For example, the following sample registers a listener which is notified when an object is created in the Person table:

final EventHandler<Map> personTableRT = Backendless.Data.of( "Person" ).rt();

personTableRT.addCreateListener( new AsyncCallback<Map>()
{
  @Override
  public void handleResponse( Map createdObject )
  {
    Log.i( "MYAPP", "Object has been created in the table" );
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
    Log.e( "MYAPP", "An error has occurred - " + fault.getMessage() );
  }
} );

Notice the callback method receives the created object. The callback is invoked any time a new object is saved in the database.

The RT system makes it possible to receive conditional updates. This means the listeners are notified only when objects which meet a certain criteria are created, updated or deleted. For example, the following code requests that any update in the Address table for the objects with the city name set to Tokyo  is delivered to the listener:

final EventHandler<Map> addressTableRT = Backendless.Data.of( "Address" ).rt();
addressTableRT.addUpdateListener( "cityName = 'Tokyo'", new AsyncCallback<Map>()
{
  @Override
  public void handleResponse( Map updatedAddress )
  {
     Log.i( "MYAPP", "Received updated address for Tokyo" );
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
     Log.e( "MYAPP", "An error has occurred - " + fault.getMessage() );
  }
} );

Notice the code uses a whereClause which creates a condition for the updated objects which must be delivered to the client:

cityName = 'Tokyo'

Any updated object in the Address table, which matches the specified condition, is instantly delivered to the client applications.

Backendless RT is a multi-platform database, it works across all supported languages and client SDKs. Objects can be created/updated/deleted in Backendless using any of the supported APIs will be triggering RT events regardless of the operating system running on the client-side. For example, an iOS client can create a Real-Time database listener who will be receiving events resulting from a REST client saving objects in the database.