Build a Real-Time Chat App with Ionic, React, and Stream

Nick Parsons
Feb 20, 2020 · 8 min read
Image for post
Image for post
How to Build an Ionic Chat App with React and Stream

There is a massive difference between platforms such as React Native and Flutter compared to Ionic. Ionic believes that the open technology that powers today’s web is the future and should be used to build mobile apps. Due to that approach, Ionic is one of the only popular platforms that allow you to reuse the same codebase for both web and mobile devices — allowing you to keep your code DRY (don’t repeat yourself).

Flutter and React Native, on the other hand, replace the web tech stack. Flutter does this with its rendering engine, and React Native hooks into the iOS and Android’s native rendering engine.

The benefit of Ionic is that you’re using web-based technologies and can reuse a single codebase. Whereas, Flutter and React Native do not allow you to reuse code as much; however, their performance is much closer to a native application feel. Still, having the ability to keep your code DRY is and should be the primary goal with any application build.

In this tutorial show, I’ll walk you through how to build a real-time chat application using Ionic, React (yes, the same version you use for the web) and Stream’s real-time Chat API.

If you want to skip ahead, a demo is available on Appetize, so you can evaluate performance yourself (note that Appetize does slow things down quite a bit though). You can also download the signed APK for Android for a more performant experience. Here is the React / Ionic GitHub repo and here is the API GitHub repo.

There are a few requirements for this, primarily the version of Node.js (I prefer to use nvm for Node version management), XCode for iOS if you’re on macOS or Android Studio if you’re on macOS or Windows and want to build against Android, and yarn for dependency management.

Let’s code! 🤓

1. Install Ionic

To get started with Ionic, download the Ionic CLI using yarn:

$ yarn global add ionic

Once installed, login to Ionic from the command line using your new CLI:

$ ionic login

For now, that’s all that we have to do. We’re going to be using Create React App (next step) to continue our installation.

2. Install Create React App and Dependencies

Similar to how we installed Ionic, let’s go ahead and install Create React App (CRA) globally using npm:

$ yarn global add create-react-app

Next, create a new directory. I’m going to be working in my ~/Code directory, but you’re free to use a directory of your choosing:

$ cd ~/Code

Now, install React using Create React App (CRA) — (ionic-chat is the name of the directory that will be generated — this is also optional as you can name it whatever you’d like):

$ npx create-react-app ionic-chat

Move into the ionic-chat directory, and we’ll start installing the necessary dependencies.

$ yarn add stream-chat stream-chat-react axios react-router react-router-dom @ionic/react

With our dependencies installed, let’s go ahead and move on to the next step of the setup.

3. Setup the API with Heroku

The API, although small, plays a vital role in chat. The API accepts user credentials from the login screen and generates a JWT for use within the chat application. It also adds the user to the channel.

To spin up the API, I’ve included a simple one-click Heroku button. This will generate a new application on Heroku and then create a Stream Chat trial for you to use.

After clicking the Heroku button, you will be prompted to add an application name — make this unique. Then click “Deploy” to kick off the Heroku deploy process.

Image for post
Image for post

Once installed, get the environment variables from Heroku (the Heroku creation generated them) and drop them in your .env file in your React app. The environment variables can be found under the “Settings” section of your Heroku dashboard as shown in this blog post by Heroku. Note that there is only one environment variable called “STREAM_URL”. The API key and secret are delimited by a : with the first being the key and the second being the secret.

Image for post
Image for post

Alternatively, if you would like to skip Heroku, you can clone this GitHub repo and run the API with the yarn start command — be sure to run yarn install before starting and also be sure to fill out your .env with credentials found on the Stream dashboard (you will need to enable a free chat trial).

4. Install the iOS Simulator (Optional)

If you have XCode installed, you’re pretty much all set. If not, and you want to download XCode, you can do so here. XCode comes bundled with an iOS Simulator by default.

Should you not wish to install XCode, you can optionally install this npm package, which will install a standalone iOS simulator for you.

$ yarn global add ios-sim

The full instructions on how to use it are located here:

5. Install Android Studio (Optional)

Running on iOS with macOS seems to be the fastest way to test your code; however, if you’re on Windows or would simply like to use Android, I’ll cover that below.

Head over to the Android Studio download page and select your download of choice. Android Studio is available for iOS, Windows, and macOS. It’s a large file — the download may take a reasonable amount of time.

Once downloaded, follow the installation instructions and open Android Studio. We’re going to download the necessary SDKs and create an Android Virtual Device (AVD).

With Android Studio open, click on “Configure” and then click “SDK Manager”.

Image for post
Image for post

Now, with the SDK Manager open, select “Android 9.0 (Pie)” and then click “Apply”.

Image for post
Image for post

Your download will begin. Once complete, head back to the main screen and click the “Configure” button, followed by “AVD Manager”. On the AVD Manager screen, you will want to click “+ Create Virtual Device”.

Next, Select the “Pixel 3 XL” device, then click “Next”. Select “Pie (28)” for your API level, followed by the “Next” button.

Image for post
Image for post

Lastly, click “Finish” and your AVD will be provisioned. Once done, you can safely exit out of the AVD screen, and you will see your newly created AVD in the AVD manager.

Image for post
Image for post

When you click on the “green play button”, your AVD will launch!

Image for post
Image for post

Congratulations! You’ve successfully generated an AVD within Android Studio! We’re not going to use it just yet, but the AVD will come in handy when testing later on in this tutorial.

6. Create Files

We have everything set up; now, it’s time to add the necessary files to make our code work! We’ll need to create a handful of files, so pay close attention:

  1. In the root of your directory, create ionic.config.json with the following contents:
"name": "Ionic Chat",
"type": "custom",
"integrations": {}

2. In public/index.html, swap out the current HTML for the following:

3. Move into the src/ directory; we’re going to create and modify a few files:

In app.css, swap out all of the existing CSS for this:

In App.js, swap out the existing code for this JavaScript (this logic will take care of routing between files):

Create a file called AuthedRoute.js and drop the contents below into the file:

Create a file named Chat.js and use the following code (this is all of the logic that powers chat):

Next, create a file named Login.js and use the following code (this will add auth to your app):

Remember to swap out the REACT_APP_TOKEN_ENDPOINT environment variable in your .env with your Heroku endpoint credentials.

Now, create a file named UnauthedRoute.js to accommodate for users who enter without being authenticated:

Create a file called withSession.js:

4. Install the Ionic build scripts in your file:

"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"ionic:build": "react-scripts build",
"ionic:serve": "react-scripts start"

Capacitor by Ionic is an open-source framework provided by Ionic that helps you build progressive native web apps, mobile, and desktops apps. It’s optimized for Ionic apps; however, it can be used with just about any framework.

We’ll be using Capacitor to lift and prepare our builds for iOS and Android. First things first, though, let’s get Capacitor installed!

$ ionic capacitor add ios

Then, start the React app with the following command from your root directory:

$ yarn start

Open on iOS:

$ ionic capacitor open ios
Image for post
Image for post

Or, open on Android:

$ ionic capacitor open android
Image for post
Image for post

Because I’m running macOS, I’m going to be using the iOS simulator. After running ionic capacitor open ios, XCode will launch. You will want to wait about a minute for it to index the project, and then you can press the run button.

Image for post
Image for post

Your iOS simulator should boot up with the application installed, and you should see a login screen similar to this:

Image for post
Image for post

Go ahead and login with your name and email address. Don’t worry, your information is only stored in local storage and is not persisted to a third-party platform of any kind. Once the chat window is loaded, you’ll be able to chat away!

Image for post
Image for post

What’s Next?

I would encourage you to continue developing against the codebase that you’ve created. If you have run into any issues, you can always clone the repo from GitHub for a fresh start.

In terms of deploying the application to a standalone device such as iOS or Android, Ionic has a great set of tutorials on how to do so. Both tutorials for iOS and Android publications can be found in the Ionic docs.

Want to know more about Stream Chat? Have a look at our interactive API tour that will walk you through the various steps of creating chat from scratch with Stream.

Stream also has fantastic API documentation and a beautiful UI Kit that will allow you to build any type of real-time messaging platform.

Last but not least, don’t forget to check out our various SDKs for Stream Chat, including tutorials for iOS/Swift and Android/Java/Kotlin.

Like demos? We have interactive demos available as well on the Stream Chat website.

Happy coding! ✌

Weekly Webtips

Explore the world of web technologies through a series of tutorials

By Weekly Webtips

Get the latest news on the world of web technologies with a series of tutorial Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Thanks to Zack Shapiro

Nick Parsons

Written by

Technical Writer🖋 Coder 👨‍💻 Outdoor Enthusiast 🏔️ Wannabe Astronaut 👨‍🚀 🐦

Weekly Webtips

Explore the world of web technologies through a series of tutorials

Nick Parsons

Written by

Technical Writer🖋 Coder 👨‍💻 Outdoor Enthusiast 🏔️ Wannabe Astronaut 👨‍🚀 🐦

Weekly Webtips

Explore the world of web technologies through a series of tutorials

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store