DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
What's in store for DevOps in 2023? Hear from the experts in our "DZone 2023 Preview: DevOps Edition" on Fri, Jan 27!
Save your seat
  1. DZone
  2. Coding
  3. Frameworks
  4. Create a Realtime Chat Interface with Firebase and Angular

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!

Jen Looper user avatar by
Jen Looper
·
Mar. 22, 17 · Tutorial
Like (1)
Save
Tweet
Share
8.31K Views

Join the DZone community and get the full member experience.

Join For Free

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:

<StackLayout>
    <ScrollView #scrollview height="90%">
            <!--the chat takes place here--> 
    </ScrollView>
    <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>
        </GridLayout>
    </StackLayout>
</StackLayout>


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>
                </StackLayout>
            </GridLayout>
      </template>
</ListView>

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


2

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;
    })
    this.chats.next([...this._allChats]);
  }

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: 

3

Interface (computing) Firebase AngularJS

Published at DZone with permission of Jen Looper, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Key Considerations When Implementing Virtual Kubernetes Clusters
  • How Do the Docker Client and Docker Servers Work?
  • Choosing the Best Cloud Provider for Hosting DevOps Tools
  • Project Hygiene

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: