Let’s Build a WhatsApp Clone Using Vue.js and Firebase
Tags: whatsappclone, socialmediaapp, vue, firebase
Published: 2021-04-23 16:48:40


## Let’s Build a WhatsApp Clone Using Vue.js and Firebase

How to build a clone of WhatsApp Web Using Vue.js, Firebase, and Spring Boot.

![Whatspp Clone for realtime chat](https://cdn-images-1.medium.com/max/5572/1*U6iilfktVJipMcEM79O1OQ.png)

## **Introduction**

We use a lot of social media platforms every day, like Facebook, LinkedIn, Twitter, Whatsapp, Instagram. One of the most used features every day is real time chat. In this tutorial, we are going to build one, using Vue.js and Firebase.

This is part of a series of tutorials, where we will be building a fully functional social networking app, where users can log in, post, add friends, and chat.

Previously, we used Firebase to create a social login using Spring Boot in the backend and Vue.js in the frontend.
[**Let’s Build a Robust Social Login Using Spring Boot, Vue.js and Firebase**](https://medium.com/javarevisited/lets-build-a-robust-social-login-using-spring-boot-vue-js-and-firebase-36fc5bac0d0b)

Live Demo:
[**simplecoding-social**](https://simplecoding-social.netlify.app/)

Github code for this tutorial:
[**webtutsplus/social-network-frontend**](https://github.com/webtutsplus/social-network-frontend/tree/whatapp-tutorial)

## Project Structure

![Relevant files for this tutorial](https://cdn-images-1.medium.com/max/2000/1*y-jQn6Nvelqg8-8lkfeASg.png)

## Let’s Build the Chat page

Let's build the view for the entire page, it has two components:

 1. **ChatSidebar**

 2. **ChatView**

![](https://cdn-images-1.medium.com/max/2000/1*mqxiM6_jXbYuGMoocQkbyg.png)

![ChatSidebar and ChatView components](https://cdn-images-1.medium.com/max/3632/1*bcOv46UhYItqXb7EgKpUYA.png)
{% gist https://gist.github.com/nilmadhab/a828c299a69cd784aed5cd3594f93f1e.js %}
For each friend, we have a room name, which we store in the backend. And we retrieve it via this API.
{% gist https://gist.github.com/nilmadhab/38637b87c98e06818a29a691bb09a4a7.js %}
Complete code for the above can be found here:
[**webtutsplus/social-network-frontend**](https://github.com/webtutsplus/social-network-frontend/blob/whatapp-tutorial/src/views/Chat.vue)

**Response of the API**
{% gist https://gist.github.com/nilmadhab/f0a115f9f65b045e4907ac8ccb89bd62.js %}
Once we have the rooms information, we send it to the child components. Let's look at the ChatSidebar component

## ChatSidebar Component

It gets the rooms prop from the parent Chat and display a list of **SidebarChatUserRow** components for each user from the rooms list prop passed from parent Chat.vue.

It also has 3 icons in the top right, which look really good!

![](https://cdn-images-1.medium.com/max/2000/1*Bd6yDKJE0rnSNim3itt_qw.png)
{% gist https://gist.github.com/nilmadhab/79450526f47bbfe61aea5bda759a6422.js %}
## **SidebarChatUserRow component**

This component is very simple — it’s just to display the email and avatar for each user. We can add the last message later, if we want.

![](https://cdn-images-1.medium.com/max/2000/1*iASVNPS7dLSkZO9-15qe9A.png)
{% gist https://gist.github.com/nilmadhab/17baac3590844050caa588b372daad8d.js %}
## ChatView component

This is the right side part of the chat, where we display the actual chats. First, we will discuss how we are displaying the chats and then we will see, when we click a different user, how to update the chats.

![chat main](https://cdn-images-1.medium.com/max/3668/1*x2M0pY1heUMnRkVLok4slA.png)

By default, we pass the first user’s room, so it has a prop room, which also has the user information (avatar, email) which we display in the header.

From the roomname, we find the associated roomId in Firebase and get all the associated chats and display those chats.

![Firebase chats store](https://cdn-images-1.medium.com/max/2252/1*EXMzTn79VtLxpyzMM7yU7g.png)
{% gist https://gist.github.com/nilmadhab/29cb90e682a1fa9dbda6efcc3ad9892e.js %}
## Creating a new chat

There is a form, where we hide the send button and onclick action. We prevent the default submit and refresh action of form by v-on:submit.prevent directive.

We get the data for inputMsg div and add a new entry in firebase by adding the chat in room name.

![submit form](https://cdn-images-1.medium.com/max/3744/1*Qg4hHsm9iXs5rIbYTStt2g.png)
{% gist https://gist.github.com/nilmadhab/bd42b041007bca629d4ae4c495aa5cb0.js %}
Now we have one important function left, which is about selecting a different friend from the Sidechatbar and how to update the chatview.

![](https://cdn-images-1.medium.com/max/5276/1*UHbXsLXR7qqyWOwS8N5YdA.png)

As they are not a parent-child type, we can’t directly pass prop or update the props. The answer is emitting the event and listening for it.

![Emit an event in **SidebarChatUserRow** component](https://cdn-images-1.medium.com/max/2200/1*nCVbuO4UOfKyY0e6VLO1Tg.png)

![Retrieving the event in Chatview and updating everything](https://cdn-images-1.medium.com/max/3632/1*Mte9u27vGsahn0V64E7eFQ.png)

The complete code for the chatview.view component:
[**webtutsplus/social-network-frontend**](https://github.com/webtutsplus/social-network-frontend/blob/whatapp-tutorial/src/components/Chat/ChatView.vue)

In this tutorial, we have covered some really interesting modern JavaScript topics like:

 1. How to pass props.

 2. How to render components.

 3. How to emit events and use them.

 4. How to integrate Firebase.

That's it. You can run the frontend code on your local computer and comment here if anything goes wrong. Thanks for reading!

## References
[**Let’s Build a Robust Social Login Using Spring Boot, Vue.js and Firebase**](https://medium.com/javarevisited/lets-build-a-robust-social-login-using-spring-boot-vue-js-and-firebase-36fc5bac0d0b)

*More content at [plainenglish.io](http://plainenglish.io/)*