Build a realtime mobile chat app using 3factor architecture
Tags: 3factor, chatapp, tutorial, graphql
Published: 2019-12-12 06:37:44

---
title: Build a realtime mobile chat app using 3factor architecture
published: true
date: 2019-04-17 04:04:44 UTC
tags: 3factor, chat app, tutorial, graphql
canonical_url: https://blog.hasura.io/build-a-realtime-mobile-chat-app-using-3factor-architecture/
---

![](https://blog.hasura.io/content/images/2019/04/christian-wiediger-606395-unsplash.jpg)

_This tutorial was written by_ [_Umar Abdullahi_](https://twitter.com/codebyomar) _and published as part of the [Hasura Technical Writer Program](https://blog.hasura.io/the-hasura-technical-writer-program/) - an initiative that supports authors who write guides and tutorials for the open source Hasura GraphQL Engine._

## What is 3factor architecture?

[3factor architecture](https://3factor.app/) design pattern leverages on realtime communication, eventing and serverless computing to allow you build full stack apps quickly and efficiently. The architecture pattern is composed of three factors, obviously :), which are realtime GraphQL, reliable eventing (eg. Hasura event triggers) and async serveless.

Building scalable and high feature velocity apps sounds nice, but when directly dealing with servers and tightly coupled components, achieving scalability and high feature velocity can be quite challenging. With the 3factor architecture, GraphQL and serverless allows you to ship new features very quickly by improving developer experience on the front end and abstracting away the development operations respectively. Serverless also means auto-scaling.

## Prerequisites

Before we continue. We are going to use the [Hasura GraphQL Engine](https://hasura.io/), Hasura event triggers, and Google cloud functions to satisfy the three factors of the 3factor architecture. A prior knowledge of GraphQL, Go, and React native is needed. As our cloud functions would be written in Go and our mobile app would be written in React native and Expo.

## Modelling

Hasura GraphQL engine would automatically generate our GraphQL schemas and resolvers based on our tables and views in Postgres. So we’ll have to model the necessary tables and views for a minimal chat app.

### User

We’ll need a user table to store our users.

```
user (
  id SERIAL PRIMARY KEY
  username TEXT UNIQUE
  push_token TEXT
  last_seen timestamp with time zone
  last_typed timestamp with time zone
  date_created timestamp with time zone DEFAULT now() NOT NULL
)
```

To create a user, only a username is required for our simple chat app, no passwords and email address. The last\_seen and last\_typed fields would be used to implement features like typing indicators and user online status.

### Room

To be able to categorize discussions we’ll need to store messages of similar topics in a room table.

```
room (
    id SERIAL NOT NULL PRIMARY KEY
    name TEXT NOT NULL
    room_type TEXT NOT NULL
)
```

All rooms are public by default i.e any user can join, we won’t be needing fields like is\_private to identify private rooms in this example.

### Message

Messages sent by users will be stored in the message table.

```
message (
id SERIAL NOT NULL PRIMARY KEY
"text" TEXT NOT NULL
username INT FOREIGN KEY REFERENCES user(username) NOT NULL
room_id INT FOREIGN KEY REFERENCES room(id) NOT NULL
"timestamp" timestamp with time zone DEFAULT now() NOT NULL
)
```

The room\_id and username would be used to identify which user sent the message and which room it belongs to respectively.

### User\_rooms

We are creating a user\_rooms table to be able to get a list of rooms a user belongs to.

```
user_rooms (
    id SERIAL NOT NULL PRIMARY KEY
    user_id INT FOREIGN KEY REFERENCES user(username) NOT NULL
    room_id INT FOREIGN KEY REFERENCES room(id) NOT NULL
)
```

Each single row would represent a user and a room joined. We can easily remove a user from a room by using the delete\_user\_rooms mutation.

**Note:** To fetch all users online we’ll need an online\_users view. An SQL script would be provided to create all the tables and views necessary.

## Setting up Hasura and Postgres

Running the Hasura GraphQL engine requires access to a Postgres database, fortunately, with [Heroku free tier](https://docs.hasura.io/1.0/graphql/manual/getting-started/heroku-simple.html) you can setup Hasura GraphQL engine with a Postgres database in a few minutes. Alternatively you can run Hasura GraphQL engine and Postgres using Docker.

### Deploy Hasura and Postgres on Heroku

Here is how you can setup Hasura GraphQL engine and a Postgres database on Heroku;

1. Click [this link](https://heroku.com/deploy?template=https://github.com/hasura/graphql-engine-heroku) to deploy to Heroku. If you don’t have an account on Heroku, you’ll need to create one. Once you sign up, you’ll be redirected to Heroku’s app creation page automatically.
2. Choose an app name and a region. A free Heroku Postgres add-on would be provided by default. Then Click deploy.
3. Once deployment is done, click view. Or head to **https:// \<YOUR\_HEROKU\_APP\_NAME\>.herokuapp.com** , you should see the Hasura console.

### Apply Schema on Postgres

On the Hasura console you’ll notice that there are no GraphQL queries, this is because we don’t have tables or views in our Postgres database. The models we created above have been combined to form a single SQL script below;

{% gist https://gist.github.com/CodebyOmar/2b4fb2495943529c642212a153aaba30.js %}

To apply the schema on our Postgres database, select the _Data_ tab in the Hasura console, click on SQL and paste the script above. Make sure you select the _track this_ checkbox before you run.

![](https://lh5.googleusercontent.com/FDZvKCkoLlHdQyx85_Qvy9r7hvJpOgWAIyhVn8ySBllUi-Aink0zzaK7ktX6_Hclnf-P6QRrtFsM72A25FXaG10GLPPc6FMKppf6UJAvdCrsHFlz3BmLX6Ecqk7JGg5cdf9OL2Vr)

Once SQL is executed successfully, the GraphQL queries, mutations, and subscriptions would appear.

**Note** : Hasura will detect relationships via foreign keys automatically. Make sure you track all relationships.

### Cloud functions

Serverless functions are a crucial part of 3factor architecture. We are going to use Google cloud functions to handle some of our backend functions. Our chat workflow would consist of functions from user registration to sending messages.

**Chat workflow** : _sign up -\> add user to rooms -\> send message -\> send push notification._

Signing up users and sending messages can be performed from the frontend directly using the generated GraphQL API. Whereas we need to write backend logic to add users to defaults rooms and also send push notifications. This backend logic would be composed as cloud functions which would be triggered by Hasura event triggers.

### Building and testing locally

Since Google cloud functions execute in runtime environments based on open source languages and frameworks, we can build and test our Go functions locally before deploying them. We won’t go into details on how to set up your Go development environment so let’s begin writing our functions right away.

Google cloud functions expects a _http.HandlerFunc_ to be the entry point of a HTTP function executed in a Go runtime environment

![](https://blog.hasura.io/content/images/2019/04/hello_http-1.png)<figcaption>hello_http.go</figcaption>

In order to test our function locally we would need a _http.HandleFunc_ to register our handler functions. Create a new directory and paste the following code in your _main.go_ file;

![](https://blog.hasura.io/content/images/2019/04/main-1.png)<figcaption>main.go</figcaption>

We can’t run our code just yet. We made reference to two handler functions _rooms.AddToRooms_ and _notifications.SendPushNotifications_ which we haven’t created. Each function would be deployed separately so we need to put them in different packages. Let’s create the _rooms_ package first. Create a new directory “rooms” in your root directory. The following code would be in _rooms/fn.go;_

![](https://blog.hasura.io/content/images/2019/04/fn-1.png)<figcaption>rooms/fn.go</figcaption>

The file above contains our handler function for adding users to default chat rooms. Immediately a user is created, the event trigger is supposed to call this function and the user would be added to the default chat rooms. Notice we are getting our database credentials from the environment variables, since we are testing locally we need to create a .env file in our root directory.

```
DB_HOST=
DB_PORT=
DB_USER=
DB_NAME=
DB_PASSWORD=
```

Get your Postgres database credentials from Heroku and place them in the .env file in your root directory.

In order to map objects or data structures to our database tables we need to declare models, we would create our models using Golang structs. Create a file types.go in the rooms directory and paste the following code;

![](https://blog.hasura.io/content/images/2019/04/types-1.png)<figcaption>rooms/types.go</figcaption>

Our rooms package is now complete. We can create the notifications package where our handler function for sending push notification would be. Create a new directory called “notifications” in your root directory;

![](https://blog.hasura.io/content/images/2019/04/fn--1--1.png)<figcaption>notifications/fn.go</figcaption>

Notice we have two functions in _notifications/fn.go_, _SendPushNotification_ is the main function triggered by the HTTP request, while _getUsersPushToken_ gets the token of all users that are in that particular chat room.

Results of the query performed to get all the user belonging to the specific chat room are mapped to a struct type named _Result._ We are going to create the struct type in the directory _notifications/types.go_:

![](https://blog.hasura.io/content/images/2019/04/types--1--1.png)<figcaption>notifications/types.go</figcaption>

Before we run our functions, first let’s create a Go module at the root of our project directory, open your terminal within the root directory and run go mod init tutorial/3factor-chatapp. This would create a Go module for us. Run go build to ensure all dependencies are downloaded.

Finally run go run main.go. This would start the application. If the application started successfully you can follow the next step, else if you run into an error, ensure you are running a version of Go 1.11 or above and all the dependencies have been downloaded.

## Setting up event system

The event system brings together the different parts of the entire application: from the frontend which initiates the events, and our cloud functions that are triggered when events are emitted.

We’ll need to capture events in two cases;

- When a new user signs up; we need an event to trigger our backend function to add the user to the existing default chat rooms.
- When a user posts a message in a room; we need an event to also trigger our backend so that we can notify the users in that room (via push notifications).

We can set up our events system either through the hasura console or through Hasura API. In other to set up these events through Hasura API, create a file _event\_triggers.json_ and paste the following code;

![](https://blog.hasura.io/content/images/2019/04/event_triggers-1.png)<figcaption>event_triggers.json</figcaption>

Notice the webhook field is not set yet, this is because these triggers would be deployed on our Hasura engine which is running on Heroku and they cannot call our functions that are running on localhost. To solve this we are going to use a tool call [ngrok](https://ngrok.com/), this would provide us with a public URL to our localhost server. Install ngrok and run the following command;

```
$ ngrok http 5070
```

This would expose our functions which are supposed to be running on port 5070 to the public. Once you get the public URL, update your event\_triggers.json, replace the \<YOUR\_NGROK\_URL\> with the URL provided by ngrok. Next, run the following command to set up the event triggers via Hasura API;

```
$ curl -d @event_triggers.json -H 'Content-Type: application/json' https://3factor-chatapp.herokuapp.com/v1/query
```

Once the above command runs successfully, we can now run our first GraphQL query from the Hasura console. Select the GraphiQL tab in the console and run the following query;

![](https://blog.hasura.io/content/images/2019/04/insert_user_query-1.png)<figcaption>insert_user_query</figcaption>

You’ll see a new user added in the users table and also added to default rooms by our backend. To confirm that our backend was actually triggered, visit [http://localhost:4040](http://localhost:4040) to inspect the traffic of your ngrok public URL.

### Deploying cloud functions

Moving away from localhost to production would require us make a few changes to ensure our functions execute well in the production environment. On each function call we attempt to connect to the database because we require a database connection to execute our logic, this is not very efficient because database connections are very costly.

The Golang _init_ functions are defined in a package block and are used for running one time computations and many more. So let’s move away our database connection to an _init_ function;

![](https://blog.hasura.io/content/images/2019/04/file-init-func-1.png)<figcaption>file-init-func</figcaption>

This ensures that the database connection is attempted once and reduces execution time greatly. The database client is also declared globally so we’ll need to add the following variables in _types.go_;

`var once sync.Once`

`var db *gorm.DB`

Now our cloud function would connect to the database on startup, and wouldn’t have to attempt connection again each time the function is triggered.

Since each package would be deployed as a separate function we have to create a Go module within each package. Run the following command in each package to create a Go module;

```
$ go mod fn.go
```

I’ve setup a GitHub (_link to be provided_) repo so that you can refer to the code.

One of the easiest ways to deploy your Google cloud function is using the gcloud cli tool, it allows your to perform various operations using a few commands.This requires you to have a Google cloud platform account, if you don’t have an account, sign up and create a project.

Once you have the gcloud tool setup. Run the following commands in the rooms and notifications directory respectively (Note: Make sure you replace xxx with the right environment variables value);

```
$ gcloud functions deploy AddToRooms --region=us-central1 --trigger-http --runtime=go111 --set-env-vars DB_HOST=xxx,DB_PORT=xxxx,DB_USER=xxx,DB_NAME=xxxx,DB_PASSWORD=xxxx --entry-point=AddToRooms

$ gcloud functions deploy SendPushNotifications --region=us-central1 --trigger-http --runtime=go111 --set-env-vars DB_HOST=xxx,DB_PORT=xxxx,DB_USER=xxx,DB_NAME=xxxx,DB_PASSWORD=xxxx --entry-point=SendPushNotifications
```

Deployment might take a few minutes.

## Implementing the front end

We are going to use React native and Expo to develop the frontend that would communicate with our GraphQL backend. You can view and run the final code of the frontend on Expo snack ([https://snack.expo.io/@codebyomar/3factor-chatapp](https://snack.expo.io/@codebyomar/3factor-chatapp)).

### Setting usernames

Signing up a user would be handled by a single component, SignUp. The code can be found in components/SignUp.js in the Expo snack project.

First let’s create our GraphQL mutation and wrap it in gql;

```
const SIGNUP = gql`
 mutation SignUpUser($username: String!) {
   insert_users(objects: { username: $username }) {
     returning {
       id
       username
       last_seen
       last_typed
     }
   }
 }`;
```

_Note that the inner mutation, insert\_users, is a mutation we are calling from our backend._

The mutation above would be executed on submit of the form and the callback below would be executed once the mutation completes successfully;

```
async handleResponse(response) {
   let { insert_users } = response;
   let { returning } = insert_users;
   let user = returning[0];
   this.props.navigation.dispatch(
     NavigationActions.navigate({
       routeName: 'Chat',
       action: NavigationActions.navigate({
         params: { ...user },
         routeName: 'Rooms',
       }),
     })
   );
 }
```

The SignUp component would render a mutation component imported from react-apollo. We would pass the mutation we created above as a prop and the callback to be executed once mutation completes successfully.

```
render() {
   const { username } = this.state;
   return (
     <Mutation
       mutation={SIGNUP}
       ignoreResults={false}
       onCompleted={data => this.handleResponse(data)}>
       {(signup, { loading }) => {
         return (
           <View style={styles.container}>
             <Text style={styles.leadText}> Sign up </Text>
             <TextInput
               label="Choose a username"
               value={username}
               onChangeText={name => this.setState({ username: name })}
             />
             <View style={{ paddingVertical: 15 }}>
               <Button
                 loading={loading}
                 mode="contained"
                 onPress={() => signup({ variables: { username } })}>
                 Continue
               </Button>
             </View>
           </View>
         );
       }}
     </Mutation>
   );
 }
```

### Creating chat rooms

A list of chat rooms will be listed to users after signing up. We will need to write a mutation to create new chat rooms.

```
export const ADD_ROOM = gql`
 mutation create_room($room: String!, $type: String){
   insert_rooms(objects: { name: $room, room_type: $type }) {
     returning {
       id
       name
     }
   }
 }`;
```

We simply retrieve the room name from the input field and pass it as a variable to our mutation to create a new room. In other to get a fresh list of rooms we are polling data every 3000ms by passing a pollInterval prop to the Query component.

```
<Query query={GET_CREATED_ROOMS} pollInterval={3000}>
  // … other code here
</Query>
```

### Subscribing to chat rooms

We have two separate lists of rooms listed. The first is a list of rooms the user has joined and he can send messages in, the others are rooms he has not joined. We need to subscribe to the user\_rooms table so that we can quickly update his list anytime he joins a new room.

```
export const ROOM_SUBSCRIPTION = gql`
 subscription onUserJoinRoom($offset: Int!, $userId: Int) {
   user_rooms(
     offset: $offset
     where: {
       user_id: {
         _eq: $userId
       }
     }
   ){
     id
     roomsByroomId {
       id
       name
     }
   }
 }
`;
```

### Sending messages

We are using a react native library to render our messages and chat user interface. We will pass a function that would receive the message and user who sent it, then we would call our insert\_messages mutation.

```
export const SEND_MESSAGE = gql`
 mutation send_msg($objs: [messages_insert_input]){
   insert_messages(objects: $objs){
     returning{
       id
       msg_text
       msg_timestamp
       room_id
       usersByusername{
         id
         username
       }
     }
   }
 }
`;
```

A room\_id is also passed to the mutation to identify which room the message belongs to.

### Typing Indicator

We update the last\_typed field in the user table to the current timestamp anytime the user types in a few characters in the input field.

```
export const SET_USER_TYPING = gql`
 mutation set_user_typing($uid: Int!) {
   update_users( _set: { last_typed: "now()" }
     where: {
       id: {
         _eq: $uid
       }
     }
   ) {
     affected_rows
     returning {
       id
       last_typed
     }
   }
 }
`;
```

By updating the last\_typed every time a user types, we can subscribe to view the last user that typed. In other not for us to appear in the list we exclude ourselves from the list.

```
export const SUBSCRIBE_USER_TYPING = gql`
 subscription get_user_typing($myId: Int ) {
   user_typing (
     where: {
       id: {
         _neq: $myId
       }
     },
     limit: 1
     order_by: {
       last_typed: desc
     }
   ){
     last_typed
     username
   }
 }
`;
```

### Subscribing to messages

We first send a query to fetch all the messages in a particular room.

```
export const GET_MESSAGES = gql`
 query fetch_messages($room_id: Int!) {
   messages(where:{room_id:{ _eq: $room_id }}){
     id
     text
     room_id
     msg_text
     msg_timestamp
     usersByusername{
       id
       username
     }
   }
 }
`;
```

After the query results arrive, the subscribeToMore function becomes available to us. With the subscribeToMore function we will be notified with the content of the new message any time there is an update. All we need to do is append it to the previous messages.

```
<Query query={GET_MESSAGES} variables={{ room_id }}>
       {({ subscribeToMore, data, loading, error }) => {
         if (loading) return null;
         const { messages } = data;
         return (
           <MessageList
             messages={this.mapMessages(messages)}
             room_id={room_id}
             user={this.user}
             subscribeToNewMessages={(append) =>
               subscribeToMore({
                 document: MESSAGE_SUBSCRIPTION,
                 variables: { rid: room_id },
                 updateQuery: (prev, { subscriptionData }) => {
                   if (!subscriptionData.data) return prev;
                   const newMessages = this.mapMessages(
                     subscriptionData.data.messages
                   );
                   return append(newMessages);
                 }
               })
             }
           />
         );
       }}
     </Query>
```

### Sending push notifications

To send push notifications we will need the user’s push token to know the device we will be delivering the messages to. We will need the user’s permission to do that. Fortunately Expo provides a solution to do that easily. The function below is called on mount of the component to get the user’s permission;

```
async registerForPushNotificationsAsync() {
   const { status: existingStatus } = await Permissions.getAsync(
     Permissions.NOTIFICATIONS
   );
   let finalStatus = existingStatus;
   if (existingStatus !== 'granted') {
     const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
     finalStatus = status;
   }
   // Stop here if the user did not grant permissions
   if (finalStatus !== 'granted') {
     return;
   }
   // Get the token that uniquely identifies user's device
   let token = await Notifications.getExpoPushTokenAsync();
   const { navigation } = this.props;
   return this.props
     .saveToken({
       variables: { token, userId: navigation.getParam('id', 'undefined') },
     })
     .then(response => response)
     .catch(err => console.log(err));
 }
```

After a user grants us permission,  we save their token using a mutation, by updating the push\_token field in the user table.

```
export const SAVE_PUSH_TOKEN = gql`
 mutation save_token($token: String!, $userId: Int!) {
   update_users(_set: {push_token: $token}
     where: {
       id: {
         _eq: $userId
       }
     }
   ) {
     returning {
       id
     }
   }
 }
`;
```

Remember we set an event trigger for everytime a new message is posted in a room, our cloud function gets the push token for all the users in that rooms and notify them.

### User online feature

As long as a user is logged in we update their online status every 3 seconds. This help us to identify users that were seen in the last 10 seconds. By simply subscribing to the online\_users view we can get realtime update of each user’s online status.

```
const SUBSCRIBE_ONLINE_USERS = gql`
subscription isUserOnline($userId: Int) {
 online_users (
   where: {
     id: {
       _eq: $userId
     }
   }
 ) {
   id
   username
 }
}
`;
```

Now when you click a user’s avatar, a dialog would pop up displaying their online status and other details.

## Conclusion

We have been able to build a scalable chat application with Hasura graphql engine which generates most of our backend functions, while Hasura event system allowed us to glue other functionalities to the whole application. The structure of the 3factor architecture makes it possible to add new features to our chat app very quickly and also build multiple frontends, you can check out the full project on GitHub [https://github.com/CodebyOmar/3factor-chatapp](https://github.com/CodebyOmar/3factor-chatapp)

## About the author 

Umar Abdullahi is a full stack software engineer based in Abuja, Nigeria. He loves building software products and developing them into  startups. He also occasionally writes about JavaScript and Go on his [personal blog](https://codebyomar.me). He watches football and movies in his spare time. You can reach out to him on [Twitter](https://twitter.com/codebyomar).