Skip to content

Expo based React Native app that uses cbl-reactnative and the Travel-Sample bucket dataset with the hotels and landmark collections

License

Notifications You must be signed in to change notification settings

couchbase-examples/expo-cbl-travel

Repository files navigation

Couchbase Lite React Native - Expo Travel Sample App 👋

Overview

This is an example app of a simple React Native app that uses the Couchbase Lite database. The app lists a set of Landmarks and Hotels that are provided as part of the Couchbase Travel-Sample dataset.

This app assumes you have a Capella Free Tier operational cluster with the travel-sample dataset installed. To find out more information about the free tier, please visit this blog post Couchbase Cloud.

Prerequisites

  • iOS: iOS 15.0 or higher
  • Android: Android SDK 24 (Android 7.0) or higher
  • Node.js: Latest LTS version recommended
  • Expo CLI: For running the development server

Couchbase Lite Version

This application uses Couchbase Lite 3.3 with the following modern APIs:

Key Features

  • LogSinks API: Modern logging configuration with LogSinks.setConsole() for flexible log management
  • CollectionConfiguration: Constructor-based replicator setup with collection-specific configurations
  • Collection-based Replication: Sync specific collections from your Capella App Services endpoint
  • Full-Text Search: Implemented with MATCH() queries for hotel search functionality
  • Value Indexes: Optimized query performance for landmark and hotel searches

API Highlights

// Modern logging with LogSinks
await LogSinks.setConsole({
  level: LogLevel.DEBUG,
  domains: [LogDomain.ALL]
});

// Collection-based replication
const collectionConfigs = collections.map(col => new CollectionConfiguration(col));
const config = new ReplicatorConfiguration(collectionConfigs, targetUrl);

App Services Setup

Log into your Capella Free-Tier account. A listing of Operational Clusters should appear. Click on your demo-cluster in the listing. This should bring up the Home page for your cluster.

Create a new App Service

On the Home tab under the Explore your cluster section find the section Try App Services and click on the link Deploy App Services.

On the main App Services page click the Create App Service button. The Create App Services page should appear.

On the Create App Services page, give your app service a name. You can name it anything you like, for example demo-app-service. Under the Linked Cluster section select demo-cluster from the pull-down select list. Finally, you can click the Create App Service button.

It will take between 5 and 25 minutes for your new App Service to be created.

Creating a new Endpoint

Once your cluster is created and the status is listed as Healthy, click on the newly created App Service name in the list, i.e. demo-app-service.

One the App Endpoints screen, click the Create App Endpoint button to create a new Endpoint.

On the Create App Endpoint page, enter the name locations in the App Endpoint Name field. In the Bucket selection list, select the travel-sample bucket. In the Scope selection list, select the inventory scope.

Under the Choose collections to link section, click the Link switch for both the landmark and hotel collections. This will link those two collections to this App Endpoint allowing us to sync data from those collections to our mobile app.

Finally, click the Create App Endpoint button. The App Endpoints listing page should appear.

Setup App Endpoint Security

On the App Endpoints listing page, click on the newly created endpoint locations. This will bring up the App Endpoint section of Capella App Services.

On the Security page the Access and Validation screen should appear. A message on this page states that the App Endpoint is paused. Click the Resume app endpoint link to resume the endpoint. This may take a few seconds to complete.

We are going to use the default Access Control and Data Validation scripts, so click the App Users tab from the navigation menu on the left.

On the App Users page, click the Create App User button. This will bring up the Create App User page. Enter a username and password for the new user. For this example, you can use demo@example.com for the username and P@ssw0rd12 for the password.

Click the Configure Access Grants link to expand the grants section. Under the Assign Channels section locate the hotel listing under LINKED COLLECTIONS and add an Admin Channel name of hotel and hit the enter key. It should show the name in a chip format. Now locate the landmark listing under LINKED COLLECTIONS and add an Admin Channel name of landmark and hit the enter key. It should show the name in a chip format. This will give the new user access to the hotel and landmark collections.

Click the Create App User button to create the new user.

Setup Delta Sync (optional)

Click on the Settings tab on the top navigation menu. From the App Endpoint Settings screen click on the Delta Sync tab located in the CONFIGURATION menu on the left side of the page.

Click the Enable Delta Sync switch to enable Delta Sync. Click the Save button to save the changes.

Get the Endpoint URL

Click on the Connect tab on the navigation menu in the header of the page. The Connect page should appear. The URL for the App Endpoint is listed in the Public Connection section. You will need this URL to connect the mobile app to the App Endpoint. Click the two sheets of paper (copy) button next to the public connection string in order to copy the URL to your clipboard on your computer. We can then paste in the URL into the configuration.

Pull down the code

From a terminal you can use the git command to pull down the code from the repository:

git clone https://github.com/couchbase-examples/couchbase-tutorials.git
cd expo-cbl-travel

Set up the Mobile App

To set up the mobile app, first install the npm dependencies from the terminal:

npm install

Update the Configuration

Open the app.json file and locate the extra section at the bottom. You will need to update three configuration values:

"extra": {
  "capellaEndpointUrl": "wss://xxxxxx.apps.cloud.couchbase.com:4984/travel-location",
  "capellaUsername": "demo@example.com",
  "capellaPassword": "P@ssw0rd12"
}

Replace the following values:

  1. capellaEndpointUrl: Paste the connection string you copied from your Capella App Services endpoint (from the setup steps above)
  2. capellaUsername: Enter the username you created in the App Users section
  3. capellaPassword: Enter the password you created for that user

Example:

"extra": {
  "capellaEndpointUrl": "wss://abcd1234.apps.cloud.couchbase.com:4984/locations",
  "capellaUsername": "demo@example.com",
  "capellaPassword": "P@ssw0rd12"
}

Running the app

iOS Setup

Prerequisites:

  • macOS with Xcode installed
  • CocoaPods installed (sudo gem install cocoapods)
  • iOS 15.0+ simulator or device

Steps:

  1. Install iOS dependencies:
cd ios && pod install && cd ..
  1. Run the iOS app:
npx expo run:ios

Or use the Expo development server:

npm run start
# Then press 'i' to open iOS simulator

Android Setup

Prerequisites:

  • Android Studio installed
  • Android SDK 24 (Android 7.0) or higher
  • Android emulator or physical device

Steps:

  1. Run the Android app:
npx expo run:android

Or use the Expo development server:

npm run start
# Then press 'a' to open Android emulator

Note for Production Builds: This example app uses default Android signing for simplicity. For production or distribution, you must:

  1. Generate your own keystore: keytool -genkey -v -keystore my-release-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000
  2. Configure signing in android/app/build.gradle
  3. Never commit your keystore to version control
  4. See React Native Signed APK Guide for details

Development Server

To run an expo development server with more options:

npm run start

In the output, you'll find options to open the app in an iOS simulator or Android emulator.

More information on developing mobile apps with Expo can be found in the links below:

Features

This sample app demonstrates the following Couchbase Lite features:

Database Operations

  • Database Creation: Encrypted local database with DatabaseConfiguration
  • Collections: Organized data into scoped collections (inventory.hotel and inventory.landmark)
  • Indexing: Full-text search indexes and value indexes for optimized queries

Queries

  • SQL++ Queries: Using Couchbase's powerful SQL++ query language
  • Full-Text Search: Find hotels using the MATCH() function
  • Filtered Queries: Search landmarks by name, location, and activity type

Replication

  • Bi-directional Sync: Real-time synchronization with Capella App Services
  • Collection-based Replication: Selective sync of specific collections
  • Continuous Replication: Automatic background synchronization
  • Authentication: Secure connection with BasicAuthenticator

Logging

  • LogSinks API: Modern, flexible logging configuration
  • Console Logging: Debug output for development and troubleshooting

Project Structure

expo-cbl-travel/
├── app/                      # Expo Router screens
│   ├── (tabs)/              # Tab-based navigation
│   │   ├── index.tsx        # Landmarks screen
│   │   └── hotel.tsx        # Hotels screen
│   └── _layout.tsx          # Root layout
├── components/              # Reusable React components
├── hooks/                   # Custom React hooks
│   ├── getHotels.ts        # Hook for fetching hotels
│   ├── getLandmarks.ts     # Hook for fetching landmarks
│   └── startLogging.ts     # Logging configuration
├── providers/              # React Context providers
│   └── DatabaseProvider.tsx # Database initialization context
├── services/               # Business logic services
│   └── database.service.ts # Core database operations
├── models/                 # TypeScript interfaces
└── app.json               # Expo and app configuration

Learn more

To learn more about Couchbase Lite and React Native, look at the following resources:

To learn more about Expo, look at the following resources:

Join the community

Join our community of developers:

About

Expo based React Native app that uses cbl-reactnative and the Travel-Sample bucket dataset with the hotels and landmark collections

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors