Ephemeral messaging applications, many of which effectively function as “disappearing messaging apps,” are on the rise and no, I am not only referring to millennials on Snapchat!
The rise of self-destructing messaging apps such as SnapChat, Wickr, and a host of others enable users to send messages which are automatically deleted within a particular time after the message is received. Because messages are deleted so soon after being read, personal conversations feel more private and authentic conversations ensue.
In this tutorial, you will learn how to build an ephemeral React chat app. In the app, when you send a message, it lasts until the recipient has seen it. As soon as the recipient reads it, there is a five-second window before the message gets deleted. This deletion happens on the receiver's device, the sender's device, and the system servers, ensuring total privacy. You will achieve this using React, CometChat, and Bootstrap. You can find the entire code for this project on this repo.
Here is a GIF of what you will build:
Creating a CometChat app
Before going ahead, you need a CometChat app. First, ensure you have an active CometChat account, but if you don’t, create one here.
Hold your horses ⚠️🐴!
To follow this tutorial or run the example source code you'll need to create a V1 application.
v2 will be out of beta soon at which point we will update this tutorial.
Next, go to your dashboard and create a new app called react-ephemeral-chat. After creating a new app, you will find the APP ID attached close to the name of your app. If you open the app and go to the API Keys tab, you will see a key with fullAccess scope. Copy it out as well as the APP ID. You’ll need these shortly.
Scaffolding a React project
You’ll use the create-react-app package to scaffold a new react project. Open a terminal window, move into the directory where you usually save your projects and run this command:
This step installs all the necessary dependencies needed to start this project. This process can take a couple of minutes to complete depending on your internet speed. After it’s done, open your new project in your preferred text editor.
Installation of dependencies
The next thing you will do is to install dependencies peculiar to the project. For this project, you’ll use:
react-router-dom: Since this is a single page application, we need this package for client-side routing.
To install the above dependencies, move into the project directory and run this command:
In this tutorial, you will use Bootstrap for styling. We need to include a link to the CDN in your src/public/index.html, under the <head> tag:
Initializing CometChat Pro SDK
Now that you have installed your dependencies, go ahead and open your project in any IDE of your choice. When your project is open, create a file called .env at the root of the project folder and add this code to it:
Replace the placeholders with the actual credentials from your CometChat dashboard. Also, note that you should not commit this file to version control.
After replacing your keys, open the index.js file located in the src folder and replace it with the following snippet:
This will ensure that CometChat is initialized in your application.
Setting up common utility functions
Here, you will create a file that stores utility functions that are not tied to the UI. In the src directory, create a utils directory and then create a CometChat.js file inside it. After creating the file, add the following code to it:
Here, you have created a light wrapper to help you achieve some tasks quickly. Here is a breakdown of what the various functions do:
getToken - This function fetches the user’s token from the local storage.
setToken - This function saves the user’s token to the local storage.
loginUser - This function logs in a user with an entered UID.
loginUserWithToken - This function logs in a user with a token.
sendTextMessage - This function sends a message to the group - supergroup. This group comes default with every CometChat app you create.
fetchMessages - This function fetches previous messages. You can set a limit for the number of messages to be fetched using the setLimit function of the MessagesRequestBuilder.
logoutUser - This function logs out the currently logged-in user.
Setting up navigation with React Router
For this project, you need two routes, one to house the Login component and the other for the Home component, where all the chat and magic happens. To begin, replace the code in src/App.js with this:
Here, you declared two routes. One leads to the Home component and the other to the Login component. In the next section, you will start building out your components.
Building the login component
You will now build the Login component. This component will house the login form and all the functionalities needed to authenticate users. Create a components directory inside the src directory. Then inside the components directory, create a Login.js file. Inside the file, import the core modules from React and the newly created utils folder by adding this code:
Next, you will set the initial state for this component with the help of the useState hook. It is a function that takes in an initial value and returns the state and a function to update that state. Add the following code under the import sections:
The next step is to perform some side effects on the component that triggers when the component mounts. If you’re familiar with React but still getting to grips with React hooks, you can think of useEffect Hook as componentDidMount, componentDidUpdate, and componentWillUnmount combined.
In this case, you are only concerned about performing this effect on componentDidMount and componentWillUnmount to know when the component is mounted or not. This comes in handy to avoid setting the state on an unmounted component. Add this snippet under the state variables:
The useEffect hook can optionally return a cleanup function that runs when the component unmounts and in this case all you're doing is resetting the value of isMounted to false.
Next, you’ll define some functions that will handle changes to the username input and form submission. Add this just after the useEffect function:
From the snippet above, you declared two functions, one updates the state whenever the user types a username and another that is called when the form is submitted. If the authentication is successful, the token is stored in local storage to persist the authenticated user. This is to avoid making additional network requests.
To avoid setting state on an unmounted component, state updates are only performed when the current value of isMounted is true i.e if the component is mounted.
Now, add this snippet below the handleSubmit function:
Here, you first check the value of isRedirected declared in state and if it is true, the user is redirected to the Home component; else you render the Login component. In the return statement, you render the login form and display validation errors if any exist.
You can try logging in now with the default usernames provided by CometChat which includes SUPERHERO1, SUPERHERO2, SUPERHERO3 **** and if you don’t get any errors, you will be redirected to the home component. Otherwise, it should display an error message.
Building the Home component
After a successful login, this component gets rendered so let’s build it. Create a Home.js file in the src/components directory and paste this snippet inside:
After that, set the initial state for the component by adding this code under the import section:
By default, the H``ome component renders without a user being authenticated and that’s because we need a mechanism for checking the stored token in local storage to determine if a user is logged in. Now, add this snippet just below the state variables:
In the snippet above, there is a check to ensure that the token is valid else the user will be redirected back to the login component. For development, CometChat provides an access token that stores the credentials of logged-in users. Hence, you can use that to authenticate users in your app. The token is stored in the local storage.
In essence, this useEffect hook gets triggered when the component first mounts and when the authToken changes. You can have as many useEffect calls as possible and an optional dependency array as the second argument to watch for changes. Therefore, it makes sense that you are first checking for an authenticated user before any other thing.
Another side effect you will handle is the fetching of messages. Here, when the component first mounts, you want to fetch messages that were sent to the group previously. Add this code under the previous useEffect function:
In this snippet performs three operations:
Updating the state of the current messages array,
Scrolling the user to the bottom and
Making sure that you mark any message that has not been previously deleted as read.
Since you need a way to know that a recipient has read a message to be able to perform any deletion, CometChat provides a markMessageAsRead function to mark messages as read. So, as soon as messages are fetched, they are marked as read.
Notice that you have not defined any scrollToBottom function. You will define that now and add this just below the useEffect function:
This function attaches a reference to the main element and automatically adjusts the scroll position to the bottom when a new message is received or sent.
Now that you have finished fetching previous messages, you will now enable users to send new messages. For this, you’ll need to create a separate component to hold the message form. Create a Footer.js component in src/components and paste this code:
This component takes in a couple of props passed from the home component and renders a form in the UI. Three important props to take note of is the message, handleChange, and handleSendMessage representing the actual message being typed, the state updater of that message and the function that handles the form submission respectively.
Now go back to the home component in src/components/Home.js and declare these functions under the scrollToBottom function:
From the code above, you can see that the handleChange function is used to update the message state whenever the user types a new message. You also called the sendTextMessage declared in src/utils/CometChat.js when the message form is submitted.
So far, your application can fetch previous messages sent to the group, and allow users to send new ones. Now you need to attach an event listener to the application that gets called every time messages are being sent or received. CometChat provides a set of functions that get triggered on the message listener. The ones we care about right now include:
You will setup the message listener with another useEffect hook. Add this function in your Home.js file:
In the above snippet, you have three message listeners:
onTextMessageReceived - This function gets called whenever a new message is received on the group. The message listener needs a unique id to listen on. Since there is only one group, one unique listener is enough. Apart from marking messages as read when you fetch previous messages, you need to mark incoming messages too. This listener comes in handy to achieve this.
onMessageRead - This listener tells you when a message has been read by the receiver. After the message is read, the message stays for five seconds before it is deleted. Also, before deleting, you need to filter out messages that have been deleted previously to avoid a 404 error and then update the state with the current messages.
Note that you can decide to implement this feature as you desire. You may wish to extend the time, you may wish to delete messages when the chat window is closed like Snapchat, or otherwise.
onMessageDeleted - In this function, you want to delete the messages on the sender’s end. All you are doing here is updating the UI by filtering out the currently deleted message and displaying the rest.
The next step is to render messages in the UI. Now, you will create a MessageList component that will show all the messages sent and received on the group. Create a new file MessageList.js in the src/components directory and paste this snippet inside:
This component displays messages in a list item. In this component, you also conditionally render a different color to the list item to differentiate messages sent and received.
Now that you are done with everything concerning messages, it’s time to handle the logout functionality. To achieve this, create a Header.js file in the src/components and paste this code:
You need to pass in the handleLogout and the user prop to this component passed down from the Home component. Now, go back to the home component and add this function:
Finally, it’s a good time to add the JSX to the home component:
In this snippet, you did a conditional rendering to make sure only an authenticated user access the component and that can be achieved by first checking whether there is an authToken present or the redirect state variable is true.
Right now, you’ve successfully built your app. Run this command in the directory of your project to run the app:
In this article, you have built an ephemeral chat app. You learned how to utilize some advanced CometChat functionalities such as read receipts, message listeners, etc to achieve this. There is still a lot more you can do with CometChat. For example, you can enable end-to-end encryption to messages in your app to provide an added layer of security. This can be achieved by enabling CometChat's Virgil Security extension, find out more here.