Learn to build a chat app with Firebase and MessageKit! By Yusuf Tör.
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
30 mins
It seems like every major app out there has a chat feature, and yours should be no different! This Firebase tutorial will show you how to add real-time chat to your app.
However, creating a chat tool can seem like a daunting task. There are no native UIKit controls specifically designed for chat, and you need a server to coordinate and store the conversations between users.
Fortunately, there are some great frameworks out there to help you:
In this tutorial, youâll build RWRC, or Ray Wenderlich Relay Chat, an anonymous chat app. If youâve used IRC or Slack, youâre already familiar with this sort of app.
Along the way, youâll learn how to:
Click Download Materials at the top or bottom of this tutorial to download the starter project.
Open the starter project and take a quick look around. The starter project contains a simple dummy login screen that saves the credentials to UserDefaults
. It also has a few helper classes for sending data to Firebase and saving data to UserDefaults
.
In the starter project, youâll find ChannelsViewController.swift, which listens to changes in a Firebase Firestore database and updates a table view whenever the user adds a new channel. Youâll build a similar implementation to display chat messages instead of channels.
Youâll find the Firebase SDK and MessageKit are already in the project as Swift Packages. These will automatically install when you open the project.
Before you can run the app, youâll need to configure Firebase.
If youâre new to Firebase, youâll need to create an account. Donât worry! Itâs easy and free.
Head to the Firebase signup site and create an account. Then create and name a new Firebase project called RWRC. Make sure that you disable support for Google Analytics as it wonât be necessary for this tutorial.
In Xcode, click the target and change the Bundle Identifier to any value you like. Then select a Team in the Signing section.
In the Project Overview in Firebase, click iOS. Youâll see instructions to add Firebase to your iOS app:
Next, enter the appâs bundle ID (the one you chose earlier in Xcode) and name (RWRC) into the form and click Register app:
Download and add GoogleService-Info.plist to your project under the Supporting Files group as shown in the Firebase instructions. This file contains the configuration information you need to integrate Firebase with your app:
Now build and run. Youâll see the following:
Thatâs a good start, but right now the application login screen doesnât actually do anything. Youâll now hook that up to Firebase.
Firebase lets users log in through email or social accounts. However, it can also authenticate users anonymously, giving them unique identifiers without knowing their personally identifiable information.
To set up anonymous authentication, open the Firebase console for the app you made earlier. Select Authentication on the left and click Get started:
Then select Anonymous. Toggle Enable and click Save:
Just like that, you enabled super secret stealth mode! Okay, so itâs just anonymous authentication. But hey, itâs still cool. :]
Itâs now time to set up the login within the app itself.
Open LoginViewController.swift. Under import UIKit
, add:
import FirebaseAuth
To log in to chat, the app will need to authenticate using the Firebase authentication service. Add the following code to the end of signIn()
:
Auth.auth().signInAnonymously()
This method asynchronously logs into Firebase anonymously. If the device has already signed in, then the existing user is signed in, otherwise a new user is created. Once the sign in has completed Firebase posts the AuthStateDidChange
notification that AppController
is listening for. AppController
updates the root view controller for you when the notification fires.
Build and run. Enter a display name and tap Get Started:
Once the user signs in, they automatically navigate to the ChannelsViewController
. Theyâll see a list of current channels and have the option to create new channels. The table has a single section to display all available channels.
At the bottom, theyâll see a toolbar with a sign-out button, a label displaying their name and an add button.
Before you dive into sending messages in real-time, take a minute to learn about the databases Firebase has to offer.
Firebase comes with two NoSQL JSON databases: Firestore and Realtime Database.
Initially, Firebase only had Realtime Database, an efficient, low-latency database that stores data in one big JSON tree.
However, this wasnât the best solution for all use cases. So the Firebase team improved on the success of Realtime Database with a new, more intuitive data model called Firestore.
Firestore stores data as documents that contain a set of key-value pairs. It organizes these documents into collections. Each document can have sub-collections.
Each database has strengths and weaknesses.
The Realtime Database:
Strengths
Weaknesses
The Firestore database:
Strengths
Weaknesses
For this tutorial, youâll use Firestore as your database. However, in a production chat app with lots of reads and writes to the database, you may choose the Realtime Database to reduce costs.
You can also use both the Firestore and Realtime Database within your app. For more information about these databases, take a look at Firebaseâs documentation.
Now that you know a little about the Firebase database, it is time to learn about the structure of the data youâll store in the database.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.
Learn more