Realm: What and Why?

In this blog, I will be introducing the Realm along with its ecosystem and components in detail.

Introduction

The realm is a NoSQL based server and a client component. The components connect via a fast and efficient sync protocol. It enables real-time, connected apps and services that are responsive and performant irrespective of network status.

The design of the realm is based on the principle `Offline First`. It is widely used and is a popular architecture for apps that are built for mobile devices and other devices that are on move and are dependent on the availability of the network. Let us quickly understand what is an offline first principle.

Offline First Principle

offline-first

This principle forms the basis of the offline first architecture . In this architecture, an important underlying rule is that the designing and development of the application is done as if there was no internet connection at all. Offline-first architecture depends on the local storage as a primary source of data, changes are made to this storage. The application also can have networking components but basic flow does not know about that application connects to a server. Communication with the backend server is just a side effect.

The advantages of Offline First Architecture

Let us take a few examples to understand how powerful and extremely user impacting it is.

  • Imagine you want to check that recent email to remind yourself about the address you were to go and your email client application refuses to give it to you because you have no access to the internet.
  • Imagine you are going by train or aeroplane and you are writing this great new post for your blog and your blogging application refuses to save it and you are getting low on battery.

If we closely understand the above two examples, in each situation the root cause of the problems is – internet access was not available at a particular moment. The applications work only in online-mode that is the state when a stable and fast connection to the back-end server is available for as long as the user wants to work and interact with them. The application is useless in the absence of network connectivity and is a pain for users. There are plenty of apps that fail to even load on the absence of network connectivity.

Solution

The evident solution looks like to have the data or the backend on the device itself. But what if we really need that backend source of data and what if you want to send some data to it as well? The solution is to make it offline-first – design and write your application as if there was no internet connection at all. The additional part that has to be taken care of is the syncing between the backend server and the local one.

realm-sync
Syncing between the local and the back-end server

The Offline first is the counterpart of the REST. As opposed to Offline first it is built on the assumption that the network will be reliable and always available. REST is not a perfect choice for mobile apps as the network is bound to be dropped, which leads to stuck apps causing a lot of discontent among the users.

Realm

The realm sync protocol is built on the principle that communication will be interrupted at any time and will also be resumed back automatically at any time. Realm provides a two-way data sync and collaboration. The realm platform(I will discuss it in detail below) works with live object approach with an embedded database to ensure apps can work seamlessly with or without network.

Realm Ecosystem

Let me now elaborate on the realm ecosystem and the components. There are three major components:

  • Database
  • Realm Object Server (ROS)
  • Sync

The realm platform is the combination of ROS and Database. And the Sync forms the heart of it. Let us discuss these components in a little detail.

realm-ecosystem

Realm Ecosystem

Realm Components

Database

  • Database: It is responsible for persisting data locally. It is an embedded and fully object-oriented database. And as soon as it connects to ROS, it becomes a distributed database. The data from all the embedded clients synchronise to the realm database in the Realm Object server. And then the Realm Object Server Syncs it across all the clients.

Sync

  • Sync: The heart of the ROS. It syncs data across all devices and servers in real time. It has conflict resolving algorithms embedded in it.

Connect

  • Realm Connect: This component is responsible for converting existing REST API’s and data sources to live objects.

Others

  • Permissions: This component is responsible for providing control access and rights in the realm.
  • Event Handling: It is there to support server-side functionality. Any changes in the server data cause triggers to execute a piece of code.

Studio

  • Realm Studio: It is an external component. It is a dashboard that gives control over data, platform functions, configurations and users.

ROS

  • Realm Object Server = Database + Sync +Event Handling +Permissions

Quick Start the Realm

The realm natively supports the following langauges

  • Java (Android)
  • Objective-C
  • React Native
  • Swift
  • Xamarin
  • Also, it exposes a GRAPHQL API that can be leveraged in any language.

Now let us quickly see how to set up the Realm Object Server. The Realm Object Server is a Node application that is distributed via npm.

Prerequisites

  • The realm currently supports the following OS: macOS, Ubuntu, RHEL, CentOS, the docker image can be used for other OS.
  • ROS uses ports so for communication, we have to allow the firewall settings to support TCP communication. By default, this is port 9080 for HTTP traffic
  • To run the ROS server, we must ensure to have Node installed on the machine.
  • Download and unzip the Realm Studio.

Starting a Realm Object Server

  • To set up a local instance run the `install` script
curl -s https://raw.githubusercontent.com/realm/realm-object-server/master/install.sh | bash
  • Alternatively run the below commands(for Ubuntu):
sudo apt-get update
sudo apt-get install build-essential libssl-dev
sudo apt-get install python
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.4/install.sh | bash
source ~/.profile
nvm install --lts
npm install -g node-gyp
install -g realm-object-server
  • To create a new ROS instance
ros init <name_of_your_ROS_server_instance>
  • Configure the feature token in the index.ts file. You can obtain the feature token: https://realm.io/trial/self-hosted-standard-plan/
cd <name_of_your_ROS_server_instance>
cat src/index.ts
set the featureToken: <feature_token>
  • Start the server
cd <name_of_your_ROS_server_instance>/
npm start
  • In case you want to run it as a docker image, use the following:
docker pull realm/realm-object-server

docker run -it --rm -p 9080:9080 -e SYNC_WORKER_FEATURE_TOKEN="[FEATURE_TOKEN]" realm/realm-object-server

Viewing the data

  • To view the content and add data in the ROS, execute the realm studio and click Connect to Server. Provide the following details URL, Username and Password. The default value is http://localhost:9080 for the URL and you can leave the Username/Password empty. After that, you simply need to click on the Connect and you will get started.
  • There are three main tabs available in the dashboard namely Realms, User and Logs
  • To add a new User, Users -> Create new User and set Username and Password. Each user has a Unique UserId assigned to it.
  • Next click on Realms -> Create new Realm, specify the path, userId/<Name_of_realm> or ~/<Name_of_realm>. With ~ the realm is created for the admin user.

References


knoldus-advt-sticker

Written by 

Pallavi is a Software Consultant, with more than 3 years of experience. She is very dedicated, hardworking and adaptive. She is Technology agnostic and knows languages like Scala and Java. Her areas of interests include microservices, Akka, Kafka, Play, Lagom, Graphql, Couchbase etc. Her hobbies include art & craft and photography.

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!