Build A Real-Time Chat App With VueJS, Vuex & Cloud Firestore

Lachlan Miller
Vue.js Developers
Published in
9 min readDec 10, 2017

Recently, Google’s Firebase platform released a new product, Cloud Firestore. Much like the Firebase real-time database, it is a NoSQL-based cloud database that can be used to build real-time applications. It addresses some of the problems Firebase has, like writing complex queries. You can read more about its features here.

In this post, I’ll be building a simple real-time chat application called Firechat which uses Vue and Vuex, and the new Cloud Firestore. I’ll look at how to integrate Firestore into a Vue.js application and some best practices with Vuex. You can get the final source code here on Github.

Note: this article was originally posted here on the Vue.js Developers blog on 2017/10/30

Check out my Vue.js 3 course! We cover the Composition API, TypeScript, Unit Testing, Vuex and Vue Router.

Installation

Let’s start by scaffolding a new Vue application using the vue-cli. I’ll use the webpack-simpletemplate.

$ vue init webpack-simple firechat

Before going any further, I want to use the spread operator ... and async/await. We will also be using Vuex for state management, and uuid for generating random ids, so let's install those. We will also need the Firebase module.

$ npm install babel-plugin-transform-async-to-generator babel-plugin-transform-object-rest-spread Firebase babel-polyfill vuex uuid --save

Next, in the project root directory, edit .babelrc like so:

Now we can use the spread operator ... and await/async.

Before going any further, go to Firebase and sign up if you haven’t got an account. Once you do, click “Add Project” and enter a name.

Then click “Add Firebase to your web app”.

Grab the config object and create a file called config.js under src like below. Select "test mode" - that'll be fine for learning Firestore. It means your database will be public, so don't share the link on the internet anywhere.

const config = {
apiKey: "...",
authDomain: "...",
databaseURL: "...",
projectId: "...",
storageBucket: "",
messagingSenderId: "..."
};
export default config

Head back to Firebase, and under the database tab, click “Try Firestore Beta” and “Start in Test Mode”, which will take you to a UI where you view the database as it changes in real time.

Setup Firestore with Vuex

Next, let’s set up Vuex and Firestore. Create a folder under src called store, and inside index.js. Enter the following:

Next, head over to main.js and import the store, and inject it into the app.

Lastly, visit App.vue, delete all the default content, add try console.log on the database to make sure everything is working.

Finally, run the server using npm run dev. If you didn't make any mistakes, you should see Firestore {__ob__: Observer} in the console! That means Firestore is configured and working.

Writing Seed Data To Firestore

Let’s create some initial data to work with. We can write using the Firestore API method, add. Start by creating a component called Initialize.vue in src. This component will give us an interface to seed the data. The actual creation of data will happen in the Vuex store.

We also stubbed a get() method which will get the data later on.

The application will have two Vuex modules: conversations.js and users.js. Messages will be saved in an array in conversations.js. If the application was to get bigger and more complex, I'd make a module for messages.js as well, but it will not be necessary at this stage.

Go ahead and create conversations.js and users.js inside of store.

Users Seed Data

Inside of users.js, add the following:

state simply declares the state, so Vue can add reactivity for us. currentUser will simulate having someone logged in, and used to set the sender property on messages.

db.collection('users') is part of the Firestore API. It returns a reference to the collection in the Firestore database. If it does not exist, it is created (or will be when you insert a document into it). Read more here: https://firebase.google.com/docs/firestore/data-model.

Firestore provides a set() method to add new data. You must provide a unique identifier. In this case, I'm using what would usually be known a username - mr_a, mr_b and so on. Even if a user was the change their firstName or lastName, the unique identified would remain the same. In the above snippet, I also set currentUser in the state. This will be used later to set a sender field when when sending messages. Easy.

Conversations Seed Data

Let’s see a bit more of the Firestore API by creating some seed data in conversations.js.

A bit more going on here than the users store. all will hold all the conversations in the application. allIds will be an array of conversation.id. Lastly, allMsgIds will be an array containing all the conversation.message ids. We will use allMsgIds to decide if a message is new or not later on when adding new messages.

There are actually two ways to add data to a collection in Firestore, set() and add(). When using set() you must specify an id for the document. You could make one using something like uuid (which we import at the top of the store). In users, we manually specified one.

Alternatively, we can have Firestore generate a random id for us using add(), which is what is happening above. The messages are nested in the conversation document, however, are Firestore cannot generate an id for us, so we created one using the uuid module. That's it for setting the seed data up.

Inject the modules to the Vuex store

Now we have some seed data ready to go, head back to index.js and import the conversationsand users modules, and add them the Vuex.Store constructor. Previous code has been omitted for brevity.

Modify App.vue to import and use the Initialize.vue component:

Now try clicking Initialize app base state. The data should be written to Firestore (you may need to refresh the Firestore page to see the new collections).

Fetching Data From Firestore

The next thing to do is display the Firestore data in the Vue app. Head to conversations.js and add a get() method. We will use the ES7 async/await syntax.

Again, we get a reference to the collection using db.colletion. Calling get() on the collection returns a promise. I don't check for failure here, but you can do so with a try/catch loop in a real application.

For each conversation, we then commit a mutation which we will make next, with conversation as the payload.

Let’s create the mutation now:

Some interesting stuff here. Firstly, to access the data in a Firestore document, you need to call data() on the object. If you simply do console.log(conversation), you won't see the data. conversation.id contains the unique id Firestore made for us.

Note that doing state.app[conversation.id] = conversation does not work! It will add the properties to the store, but they will NOT have Vue reactivity, which would prevent us from adding new message down the line. See https://vuejs.org/v2/guide/reactivity.html for more details.

Note that we also do not add the messages (although when we call data() we do have access to them). We'll see why later on. We also save the conversation.id in the allIds array.

Let’s do the same for users.js. We will also set mr_b as the currentUser.

That’s it for access the data. Time to create some components to display it.

Creating Components to Display Firestore Data

Now create ConversationContainer.vue and Message.vue inside src, and enter the following. We will start with Message.vue.

Nothing exciting here. A simple check to decide if the message was sent by the current user and to position it accordingly. Let’s move on to ConversationContainer.vue.

Again, nothing special. Later on in created() we will load all the messages, and listen for new ones. This component will receive a conversations as a prop, which comes from $store.state.conversations.

Now import ConversationContainer.vue in App.vue. Note this is the final implementation for App.vue.

Pretty straightforward — we loop allIds, and pass each conversation as a prop to the ConversationContainer.vue. We can use the id from Firestore as a :key for the v-for loop as a bonus. The Vuex mapState helper function makes it easy to access the state.

Lastly, update Initialize.vue to actually fetch the data (we stubbed get() out earlier, remember?)

If everything went well, you should be able to click the “GET” button and see the following:

Fetching Messages in Real Time

Finally, the bit we’ve all been waiting for — real-time messaging. In ConversationContainer.vue, update create() to look like the following:

In Firestore, you can listen to a document using the onSnapshot() method. More here: https://Firebase.google.com/docs/firestore/query-data/listen.

Firstly, refresh the Vue application and hit get to query Firestore for the conversations. Check the console. You should see Source: Server printed twice. onSnapshot() triggers immediately, returning the current state of the document. It also will trigger every time the document changes.

This is why we didn’t populate the messages in the conversations.js - module - we want to fetch the conversation once initially, to get the ID and members, but constantly be watching for new messages. We will take advantage of the initial onSnapshot() to get the current state of the conversation, specifically the messages, and then update anytime a new message is created.

Notice the line let source = convo.metadata.hasPendingWrites ? 'Local' : 'Server'. This is due to a feature called latency compensation. Basicially, when you write data, all clients listening to the document will receive a notification, before the data is even sent to Firestore. You could use perhaps use this to show a "member is typing..." notification. If hasPendingWrites is true, the data has not been written yet, and if it's false, it has.

Now, we need to add the messages to the store. Update created() like so:

You access the data in the document returned from onSnapshot() using the data() method. Let's now write the mutation to add the messages in conversations.js.

onSnapshot() returns the entire conversation, including the existing messages. By checking if allMsgIds includes the message.id, and pushing it as such, we can ensure only new message are added to the conversation. Vue's reactivity will automatically update the UI for us.

That should be enough to display the messages! Try refreshing, grabbing the data and you should see something like this.

Sending A Message In Real Time

Lastly, let’s send a message in real-time. Update ConversationContainer.vue:

Fairly straightforward. Using v-model, we bind to an <input>, and on keyup.enter send the message, passing the conversationId to the commit.

Header over to conversations.js and add the following action:

Firestore documents are updated using update(), which allows you to update certain fields, without touching the others. Read more here: https://firebase.google.com/docs/firestore/manage-data/update-data.

We simply update the messages property, with all the existing ones, plus the newly sent message. When the Firestore document is updated, onSnapshot() triggers again and adds the message to the conversation.

This should be enough to get the application working real-time! Try opening another browser and send some messages — they should be received real-time in any other clients.

Conclusion and Improvements

Firestore is very easy to get started with, and extremely powerful. Some improvements to this application include:

  • Only sending and fetching new messages. At this point, the entire conversation is sent and received. I’m not sure how to do this yet using Firestore. Likely some kind of Array.pushmethod exists - let me know if you have a good technique for this!
  • Deleting or editing messages. This would not work in the current application since when adding messages, we check by id — to allow for editing, you would need to check for the id in allMsgIds, and if the text was different.
  • Creating new conversations. At the moment only two conversations exist, the ones created by seed(). It would be a good exercise to extend the application to allow for this.
  • Auth. Firebase also provides authentication, that you could use with an application like this.

--

--

Lachlan Miller
Vue.js Developers

I write about frontend, Vue.js, and TDD on https://vuejs-course.com. You can reach me on @lmiller1990 on Github and @Lachlan19900 on Twitter.