Ionic 2 – Lifecycle hooks and chatrooms

Hi Friends,

Hope you are all well. This tutorial is a sequel to my post ‘Realtime chat using Ionic 2 and sockets‘. Though it’s not necessary I would recommend you to have a look at that once.

In this tutorial we will be seeing how lifecycle hooks in Ionic 2 work. We will be building a realtime chat application using lifecycle hooks and sockets.io. Our application will contain 2 chatrooms.

The complete code of this tutorial can be found in the links below.

Frontend – here.

Backend – here.

A screencast of this tutorial is available here.

Let’s begin.

First of all what is a lifecycle hook ?

A lifecycle hook is a function or an event handler that is triggered while navigating between views. This comes in handy in several aspects of the application. Our views don’t get removed while we navigate; they simply get cached. So such lifecycle hooks come in handy. You can read more about them here.

Ionic 2 has 7 hooks namely

  • onPageLoaded – Similar to ngOnInit
  • onPageWillEnter – Before view becomes active
  • onPageDidEnter – After view becomes active
  • onPageWillLeave – Before view loses focus
  • onPageDidLeave – After view loses focus
  • onPageWillUnload – Before page is removed from the DOM.
  • onPageDidUnload – After page is removed from the DOM.

Now these lifecycle hooks come along with NavController in Ionic 2. To understand them better let’s use a couple of these to build a Realtime Chat application with sockets.io

As I mentioned before, this application is a sequel to my previous post on building a chat application.

First, lets clone the frontend and backend repos using the below commands.

First let’s work with the client.

Navigate to the folder where you have cloned our app and generate two pages using the below command.

Now open home.html and move the code to firstroom.html. Similarly open home.js and move the code to firstroom.js

Open home.html again and add the below code.

It just adds a text input and 3 buttons. Open home.js and add the below code.

We are simply storing the name entered in the textfield to localStorage and navigating the user to the room of his choice based on which button he clicks.

Now open up firstroom.js and modify the code so that it looks as below.

Let’s break it down.

  1. Instead of sending the data as such in the sendmsg() function, we now send the data along with the room value.
  2. OnPageDidEnter is a lifecycle hook. Whenever the view becomes active it will get triggered and emit a msg about the user who entered the room.
  3. OnPageDidLeave is again a lifecycle hook. Whenever the page gets out of viewport it gets triggered and emits a similar message.
  4. We have 3 listeners which listen for flags pertaining to that specific room.

    For eg: this.socket.on(this.pkt.room +’message’) – this will listen to pings with the flag room1message or the room1message stream.

This is all we need in the front end.

Now copy the contents of firstroom.html and firstroom.js into secondroom.html and secondroom.js respectively.

Make the below change.

Now let’s tweak the backend.

Open the index.js file and modify the contents as shown below.

Let’s break this down.

  1. The listeners listen for the streams, and once data is received they get it, concatenate the room name along with the stream indicator and emit the data.
  2. This will work for any number of rooms since the server processes the room from the incoming data and emits the data back to all the listening nodes accordingly.

There, now we have a simple, clean and effective code demonstrating lifecycle hooks and a neat chatroom app.

To request me for a tutorial or ask me anything, please use the ask me page.

Thanks for reading guys. Peace. :)

Liked it? Take a second to support admin on Patreon!