Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Real Time Synchronization

Wisk client applications (either Web or iOS) use real time mechanisms to receive the data from the backend.

Currently there are two different mechanisms in place:

  • Firestore (legacy) (https://firebase.google.com/docs/firestore)
  • Couchbase (https://www.couchbase.com/)

Some venues are already using couchbase on production, and we will remove completely Firestore once all of them are migrated.

How it works

Wisk data is stored mostly on a Postgresql database. Backend code stores everything in that database, but some of the data is also mirrored in a separate realtime database (firestore or couchbase)

Client apps can read directly from the realtime database, which is much quicker than having to do REST calls to the backend to fetch the data.

Firestore (legacy)

In firestore, data is stored in documents inside collections. Backend will push the data from the database to specific collections, while client apps will read from the collections and then listen to changes on them. This way, client applications can be reactive to any changes.

Couchbase

In couchbase, data is stored as documents in a bucket in json format. Couchbase database is kept in memory all the time, which make access super quick both for reading and writing.

Client applications can replicate the whole database on the initial load, and then keep the local and remote database in sync seamlessly. The client application will then read always from the local database and can also listen to changes on it.

Only read, don’t write

In both scenarios, client applications do not write directly to the real time database. Instead, they communicate with the backend with REST API calls with the operations to be performed and then listen to the changes on the data once the backend processed the operations.

Accessing the data

Firestore (legacy)

For dev environment, the data can be queried from here: https://console.firebase.google.com/u/0/project/wisk-dev-platform/firestore/data/

img_15.png

Couchbase

For dev environment, the data can be queried from here: https://couchbase-ui.wisk.dev/ui/index.html

img_16.png

Write data from api

Most of the data in firestore/couchbase are written from the wisk-api project. The is usually managed automatically by some optional methods from TableCRUD`

In order to make an entity (table) synchronized to firestore/couchbase, the entity needs to implement the following methods from TableCRUD (this example is using TaxRatesTable as example):

   static var documentType: DocumentType = .TaxRate`
   
   func getVenueId() -> Int? {
        return venueId
   }

   func getType() -> String? {
        return "tax_rate"
   }

   func getDocumentId() -> String? {
        return id.string
   }

   func getFirestoreDocument() async throws -> [String: Any]? {
        return toAPI.toDict
   }

With that, the entity will be automatically synchronized to firestore/couchbase on every insert/update.

Resync the data

In some situations, either because of an issue or because of a change in the data structure, some of the data needs to be resync from the Postgresql database to the realtime one.

We do this using postman calls to the API.

Firestore (legacy)

There are specific queue operations for each type of data (items, pos_items, prices, inventories,…). Check the QueueOperation.swift to look for your desired operation type.

img_17.png

NOTE - These operations are both syncing the data to firestore and couchbase.

Couchbase

For couchbase there’s a new generic operation that can be parametrized to sync each type of data for the desired subset of venues

img_18.png img_19.png

NOTE - This operation will NOT sync to firestore, so if the desired data refresh affects to both firestore and couchbase clients, we should use the previous one.