A chat app with React Native

__

 VERSIONS

Jul 28th (latest)
version 3.0.0
Jun 10th
version 2.0.0
Jun 10th
version 1.0.0
Setup
1 / 8

This is the first blog in a multipart series where we will be building Chatty, a WhatsApp clone, using React Native and Apollo.

Overview

Each part of this series will be focused on teaching a core concept of Apollo or React Native. We’ll start from scratch, and by the end of the series, we’ll have a kick-ass group messaging application with real-time updates. Future posts beyond the core series will cover more complex features like push notifications, file uploads, and query optimizations.

Since we are using shiny new tech, this series will be a living document. I will update each post as the tools we use continue to advance. My goal is to use this series as a best practices model for building a complex application using some of the best software available.

With that in mind, if you have any suggestions for making this series better, please leave your feedback!

The Stack

Chatty will use the following stack:

This is a pretty awesome stack for building complex real-time native applications.

For those of you who are new to Apollo, I just want to point out some of the coolest built-in features for Apollo with React:

  • Smart query caching (client side state gets updated and cached with each query/mutation)
  • Subscriptions (realtime updates pushed by server)
  • Optimistic UI (UI that predicts how the server will respond to requests)
  • SSR support
  • Prefetching

That’s a ton of buzzwords! In the end, what that all really adds up to is our app will be data driven, really fast for users, and get real-time updates as they happen.

Part 1 Goals

Here’s what we are going to accomplish in this first tutorial: 1. Set up our dev environment 2. Start an Apollo Server 3. Create our first GraphQL Schema 4. Start a basic React Native client 5. Connect our Apollo Server and RN client!

Getting started

For this tutorial series, we’re going to start from absolute scratch. My style is to keep everything really simple and refactor as we add complexity. Let’s start with this basic directory structure:

/chatty
  /node_modules
  package.json
  /server
    ... server files
  /client
    /node_modules
    package.json
    ... RN files

We will keep our React Native code separate from our server code. This will also keep server dependencies separate from React Native dependencies, which means we will have 2 package.json files. That may sound weird/bad, but trying to get everything set up with one packager is a huge hassle. It will also save us from a few other issues down the line.

Here’s the terminal code to get us started:

# make our directory
mkdir chatty
cd chatty

# start npm package managing
npm init

# build some server folders and files
mkdir server
cd server
touch index.js

Setting up the dev environment

We’ll start setting up our dev env with the following features: 1. Server stays running and reloads when we modify code 2. ES6 syntax including import syntax in our server code 3. ESLint with AirBNB presets

# from root dir..

# add dev dependencies
npm i -g eslint-cli # eslint is an excellent linter

# i -D is short for install --save-dev ;)
npm i -D babel-cli babel-preset-es2015 babel-preset-stage-2 nodemon eslint babel-eslint
eslint --init  # choose airbnb preset or your preferred setup

My eslintrc.js file looks like this:

Step 1.2: Add eslint, babel, and nodemon

Added .eslintrc.js

Create our start script inside package.json:

Step 1.3: Create start script

Changed package.json

Starting the Express server

Let’s import apollo-server in index.js using ES6 syntax. 1. npm i apollo-server graphql (apollo-server requires graphql) 2. Add the following to index.js:

Step 1.4: Add ApolloServer

Changed package.json
Changed server/index.js

Quickly verify our setup works by running npm start: 🚀 Server ready at http://localhost:8080/

We have a great starting point. Our start script will transpile ES6 code, spin up our Apollo Server, and refresh as we make changes to server code. Nice!

But wait, there's more! Apollo Server comes with some amazing features out of the gate, including GraphQL Playground. Head on over to http://localhost:8080/ and you should see a slick playground for us to test GraphQL queries against our server!

Type in {testString} and you should get back a response: Playground Image

Great! So now we have a server that runs the most basic GraphQL. We could build up our GraphQL backend a bit more, but I’d prefer to connect our server and React Native client before we make our Schema any more complex.

Starting the React Native client

First we’ll download the dependencies and initialize our React Native app. For the sake of brevity, I’m going to focus on iOS, but all our code should also work with Android.

# you'll need to also install XCode for iOS development
# double check these setup instructions at https://facebook.github.io/react-native/docs/getting-started.html#installing-dependencies
brew install node
brew install watchman

# from root dir...
npm i -g react-native-cli

# initialize RN with name chatty
react-native init chatty

# --- from here on out we'll be doing our own thing ---

# change name of RN folder to client
mv chatty client

# run the app in simulator
cd client
react-native run-ios # and grab a snack or something cause this might take a while the first run...

Running the initialization will create an index.js file. In this file is boilerplate code that creates a React component and registers it with AppRegistry, which renders the component.

Let’s pull out the Chatty component from index.js and stick it in its own file. I prefer to organize my files by type rather than feature, but you’re welcome to organize differently if you feel strongly about it.

So I’m going to place the Chatty component code into client/src/app.js and rename the component App.

Step 1.6: Move app code to /src

Changed client/index.js
Changed client/App.js

Adding Apollo to React Native

We’re going to modify app.js to use React-Apollo and Redux. While Apollo can be used sans Redux, the developer experience for React Native is much sweeter with Redux for monitoring our app's state, as you'll soon see.

We need to add a bunch of Apollo packages and a couple Redux ones:

# **make sure we're adding all react native and client related packages to package.json in the client folder!!!**
cd client

npm i apollo-cache-redux apollo-client apollo-link apollo-link-error apollo-link-http apollo-link-redux graphql graphql-tag react-apollo react-redux redux redux-devtools-extension

We need to do the following: 1. Create a Redux store 2. Create an Apollo client 3. Connect our Apollo client to our GraphQL endpoint via apollo-link-http 4. Catch and log any GraphQL errors via apollo-link-error 5. Connect Redux to our Apollo workflow via apollo-link-redux. This will let us track Apollo events as Redux actions! 6. Set our Apollo client's data store (cache) to Redux via apollo-cache-redux

We can also swap out compose for composeWithDevTools, which will let us observe our Redux state remotely via React Native Debugger.

Step 1.7: Add ApolloClient

Changed client/package.json
Changed client/src/app.js

Finally, we need to connect our app to Apollo and Redux. We'll wrap our App component in the ApolloProvider component from react-apollo and the Provider component from redux.

Step 1.8: Add ApolloProvider to App

Changed client/src/app.js

If we reload the app (CMD + R), there hopefully should be no errors in the simulator. We can check if everything is hooked up properly by opening Redux Native Debugger and confirming the Redux store includes apollo: Redux Devtools Image

Setup
1 / 8