Skip to content

Building a Real Time Chat Application

Original guide · All levels

Intro Thanks to the WebSockets and NodeJS integration in Wappler you can build real-time apps. One example of what you can build is a real-time chat app. Sockets provide a real-time bi-directional com

Thanks to the WebSockets and NodeJS integration in Wappler you can build real-time apps. One example of what you can build is a real-time chat app.

Sockets provide a real-time bi-directional communication channel between a client and a server. This means that the server can push messages to clients. Whenever an event occurs, the idea is that the server will get it and push it to the concerned connected clients.

In this example we will show you a simple chat app, using Data Store to store the chat history locally in your browser local storage.

First we need to setup the server-side part. Open the Server Connect panel and right click Sockets:

This socket action will be used as our chat message template, so we call it chat_message:

Section titled “This socket action will be used as our chat message template, so we call it chat_message:”

Add two variables - username and message. These are the input variables for the message which will be emitted:

Open the Type dropdown and select the chat_message socket action we just created:

Section titled “Open the Type dropdown and select the chat_message socket action we just created:”

Click the dynamic data picker for the username input parameter:

Section titled “Click the dynamic data picker for the username input parameter:”

Do the same for the message input parameter:

Section titled “Do the same for the message input parameter:”

Save your Socket Action and you are done with the server side part:

Section titled “Save your Socket Action and you are done with the server side part:”

First, let's create the page for our chat app.

Open the Pages Manager:

Select Pages and click the Create Page button:

Section titled “Select Pages and click the Create Page button:”

Add a name for your page, select a layout if you are using any and click Save:

Section titled “Add a name for your page, select a layout if you are using any and click Save:”

Now you can close the Pages Manager panel.
Now as out page has been created, let's add the required App Connect components to it. In this example we will use the Data Store component to store the chat messages in the local storage of the browser.

Open the App Connect panel, select App and add click the Add New button:

Add a name for the Data Store component and click the Define Data Store Schema button:

Section titled “Add a name for the Data Store component and click the Define Data Store Schema button:”

Right click the Data Store name and add two variables:

Section titled “Right click the Data Store name and add two variables:”

We want to store the user name and message, so we call them chat_user and chat_message:

Section titled “We want to store the user name and message, so we call them chat_user and chat_message:”

Select App and click the Add New button:

Open Sockets and select the Socket component:

Section titled “Open Sockets and select the Socket component:”

Now we are going to set up what happens, when a new message has been received.
Open the Message Type menu and select the Socket Action (or message template) called chat_message which we created earlier:

On new message, we want to add it to the Data Store. So, select Insert Record under the Data Store component:

Section titled “On new message, we want to add it to the Data Store. So, select Insert Record under the Data Store component:”

And select the data, which needs to be inserted for chat_user:

Section titled “And select the data, which needs to be inserted for chat_user:”

Now the user name and message text will be stored in the Data Store, when a new message is received by the Sockets Component.

We will store the username in a session variable, so that the user enters his username just once per each chat session.
Right Click App:

Open State Management and select Session Storage Manager:

Section titled “Open State Management and select Session Storage Manager:”

Click the Define Session Storage Items button:

Section titled “Click the Define Session Storage Items button:”

Right click the Session Storage and Add Variable:

Section titled “Right click the Session Storage and Add Variable:”

We call this variable username as it will store the username, which your user entered. Click Save:

Section titled “We call this variable username as it will store the username, which your user entered. Click Save:”

We've set up all the required components on the page, now let's build the page layout. It will consist of two parts - an input where the users enter their username and another section where the user can send messages and view the other users' messages.

Let's start by adding a container on the page. Click the Add New button:

Then select the Header and two columns preset for the container layout:

Section titled “Then select the Header and two columns preset for the container layout:”

The top row is where the username input will be placed. So click add new button in the column there:

Add an ID and name for it and and customize it, as per your needs. The styling of the input is not related to how the chat app will work:

Then with the input selected click the Add After button:

Section titled “Then with the input selected click the Add After button:”

This button will be used to enter the chat, after the username is entered. I.e. it will add the entered username to the session storage.
Style it as per your needs:

Select Set under the Session Storage component:

Section titled “Select Set under the Session Storage component:”

Choose a dynamic value for the username session variable:

Section titled “Choose a dynamic value for the username session variable:”

The chat section of our app consists of two columns - one for the message input and another one which lists the chat messages.

Click the add new element button in the left column:

With the textarea still selected, click the add after button:

Section titled “With the textarea still selected, click the add after button:”

Style it as per your needs. This button will be used to send the message you enter in the textarea:

Section titled “Style it as per your needs. This button will be used to send the message you enter in the textarea:”

Select and add Emit under the Socket component:

Section titled “Select and add Emit under the Socket component:”

Open the Type menu and select the chat_message message template:

Section titled “Open the Type menu and select the chat_message message template:”

Now let's select the dynamic data, which needs to be sent with the Emit action.
Click the dynamic data picker for the username:

The username will be stored in a session variable, so we select the session variable where we are storing it:

Section titled “The username will be stored in a session variable, so we select the session variable where we are storing it:”

This should be the value of the message text area:

Section titled “This should be the value of the message text area:”

You can clear the textarea value, after the message has been sent. Under textarea select set value and add it to the actions list:

Just enter an emtpy space there and click select:

Section titled “Just enter an emtpy space there and click select:”

Now, let's setup the repeat region, which will display the chat messages.
Click the add new element button in the right column:

This column is the message body, we need to show the username and message here. So let's add them:

Section titled “This column is the message body, we need to show the username and message here. So let's add them:”

And select paragraph, this will be the message text:

Section titled “And select paragraph, this will be the message text:”

Then select the row, wrapping our message body column. Click Make Repeat children:

Section titled “Then select the row, wrapping our message body column. Click Make Repeat children:”

This should be the data element of the Data Store component:

Section titled “This should be the data element of the Data Store component:”

Turn the App Connect mode off in order to be able to bind the data inside the repeat region:

Section titled “Turn the App Connect mode off in order to be able to bind the data inside the repeat region:”

Double click the title and click the dynamic data icon:

Section titled “Double click the title and click the dynamic data icon:”

Select the chat_user binding from the repeat region:

Section titled “Select the chat_user binding from the repeat region:”

Select the chat_message binding from the repeat region:

Section titled “Select the chat_message binding from the repeat region:”

We only want to show the username input when the user has not entered a username yet and also we want to show the chat area after the user entered a username. In order to do this we will use conditional regions.

Select the row wrapping the username input and click Make Conditional Region:

Click the Dynamic Data Picker for the condition:

Section titled “Click the Dynamic Data Picker for the condition:”

We want to show the username input only when the session variable doesn't contain any username. So we select the session variable, where we store our username:

Then select Code and add an exclamation mark ! in front of the expression. Click Select:

Section titled “Then select Code and add an exclamation mark ! in front of the expression. Click Select:”

Select the row wrapping the chat area and click Make Conditional Region:

Section titled “Select the row wrapping the chat area and click Make Conditional Region:”

Click the Dynamic Data Picker for the condition:

Section titled “Click the Dynamic Data Picker for the condition:”

We want to show the username input only when the session variable contains a username. So we select the session variable, where we store our username and click Select:

You can see the result in our browser below. The user enters a username and then he is taken to the chat area, where he can send messages to the other clients:

Result

Note: This is a brief example of a real-time chat app, which we believe explains the basics of how the things work. Feel free to extend this if you need a more complex solution.