Over a million developers have joined DZone.

Create a Realtime Chat Interface with Firebase and Angular

DZone's Guide to

Create a Realtime Chat Interface with Firebase and Angular

In this article, we walk you through the process of creating your own Twitter-style chat app using NativeScript. By the end, you should have a functioning mobile app!

· Mobile Zone ·
Free Resource

Continuing the work I did with Yowwlr, the Twitter for Cats client that I started building to show how to use Flexbox, I thought the next thing to tackle would be a clone of Twitter’s “messages” tab, which is essentially a chat interface showing bubbles when a user wants to chat. Digging into Twitter, which is a deceptively simple-looking app, you notice several peculiarities. Some of them are a little too complicated for this article, like the little speech arrow that’s added on to the bubble, but only in the case of the last part of the chat. Many elements, however, are easily clonable in a NativeScript app. I turned my attention to building a realistic chat UI similar to Twitter’s using Firebase database’s real-time features and duplicating this interface for both Android and iOS. Here’s an example of Twitter’s chat interface, as a reference:
Image title

Note the static chat box that sits on top of the Tab navigation. A ScrollView needs to sit on top of that. The first part of this challenge, then, involves going back to our tried-and-true StackLayout, which embeds a ScrollView on the top and a StackLayout on the bottom:

    <ScrollView #scrollview height="90%">
            <!--the chat takes place here--> 
    <StackLayout height="10%">
        <GridLayout columns="*,auto" style="padding: 10">
            <TextField #textfield class="chatTextField" row="0" col="0" [(ngModel)]="message"></TextField>
            <Button #btn class="chatBtn" row="0" col="1" text="send" (tap)=chat(message)></Button>

In the ScrollView, which forces the textfield and button to the bottom by maintaining 90% height, we do some sneaky things to differentiate the style of chat depending on who’s chatting. As in Twitter, if your own chat shows in the window, you don’t see your own image and the chat bubble is aligned to the right. If you’re watching your friend chat with you, those bubbles are aligned to the left and you see an image of the person (or cat) who’s chatting. 

Note: In the actual Twitter app, you see a list of potential chat-mates, choose one, and then initiate a chat. I didn’t built out that part but it wouldn’t be hard to implement. For this demo, I manually added a chat-mate as the recipient.

Differentiating your own chat words from a chat-mate's is done via CSS and some data-bound properties. Within a ListView's template, I added a StackLayout with a label and an image. The StackLayout has a class that switches depending on the identity of the users, and its horizontalAlignment switches from left to right as well. The image of the user similarly switches visibility based on the user’s status.

<ListView padding="5" #list separatorColor="#fff" [items]="chats$ | async" class="list">
    <template let-item="item">
          <GridLayout columns="*" rows="auto" class="msg">
                <StackLayout [class]="filter(item.from)" orientation="horizontal" [horizontalAlignment]="align(item.from)">
                  <Image [visibility]="showImage(item.from)" class="authorimg" stretch="aspectFill" height="30" width="30" verticalAlignment="top" src="~/images/k1.png" col="1"></Image>
                  <Label [text]="item.message" class="msg_text" textWrap="true" verticalAlignment="top"></Label>

Logging into the Yowwlr app as two different users, we can test our interface cross-platform:


Another challenge was to make the ListView display the data in reverse order. I did this by sorting the data coming from the chats in reverse order (newest on the bottom). Once received, the array is sorted by date, in reverse:

publishChatUpdates() {
    this._allChats.sort(function(a, b){
        if(a.date > b.date) return -1;
        if(a.date < b.date) return 1;
      return 0;

There are several great resources to help you get started creating the next great chat app. There are at least three plugins already built: a complete chatview plugin, a plugin to help interface with the LiveEngage service, and a WeChat-style chat client.  Bruno d’Auria has written a blog on the topic, as has Nic Raboy. I was inspired by Dave Coffin’s excellent NativeScript Snack Snippet on how to make the chat bubbles. Kudos to everyone making cool and useful chats!

Here’s How Yowwler’s Chat Function Works in Real-Time: 


mobile ,application development ,chat app

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}