Building a chat ā Browser Notifications with React, Websockets and Web-Push š¤Æ
In the previous article in this series, we talked about Socket.io, how you can send messages between a React app client and a Socket.io server, how to get active users in your web application, and how to add the "User is typing..." feature present inā¦

What is this article about?
We have all encountered chat over the web, that can be Facebook, Instagram, Whatsapp and the list goes on.Just to give a bit of context, you send a message to a person or a group, they see the message and reply back. Simple yet complex.
In the previous article in this series, we talked aboutĀ Socket.io, how you can send messages between a React app client and a Socket.io server, how to get active users in your web application, and how to add the āUser is typingā¦ā feature present in most modern chat applications.
In this final article, weāll extend the chat application features. You will learn how to keep your users engaged by sending them desktop notifications when they are not online and how you can read and save the messages in a JSON file. However, this is not a secure way of storing messages in a chat application. Feel free to use any database of your choice when building yours.
Novu ā the first open-source notification infrastructure
Just a quick background about us. Novu is the first open-sourceĀ notification infrastructure. We basically help to manage all the product notifications. It can beĀ In-AppĀ (the bell icon like you have in Facebook āĀ Websockets), Emails, SMSs and so on.I would be super happy if you could give us a star! And let me also know in the comments ā¤ļø https://github.com/novuhq/novu
How to send desktop messages to users
Here, Iāll guide you through sending desktop notifications to offline users when they have new chat messages.
In the previous article, we created theĀ ChatFooterĀ component containing a form with an input field and a send button. Since we will be sending a notification immediately after a user sends a message, this is where the desktop notifications functionality will exist.
Follow the steps below:
Update theĀ ChatFooter.jsĀ component to contain a function namedĀ checkPageStatusĀ that runs after a message is sent to the Socket.io server. The function accepts the username and the userās message.
Tidy up theĀ ChatFooterĀ component by moving theĀ checkPageStatusĀ function into aĀ src/utilsĀ folder. Create a folder namedĀ utils.
Create a JavaScript file within theĀ utilsĀ folder containing theĀ checkPageStatusĀ function.
Copy the code below into theĀ functions.jsĀ file.
Update theĀ ChatFooterĀ component to contain the newly created function from theĀ utils/functions.jsĀ file.
You can now update the function within theĀ functions.jsĀ file as done below:
From the code snippet above, theĀ JavaScript Notification APIĀ Ā is used to configure and display notifications to users. It has three properties representing its current state. They are:
- Denied ā notifications are not allowed.
- Granted ā notifications are allowed.
- Default ā The user choice is unknown, so the browser will act as if notifications are disabled. (We are not interested in this)
The first conditional statement (if) checks if theĀ JavaScript Notification APIĀ is unavailable on the web browser, then alerts the user that the browser does not support desktop notifications.
The second conditional statement checks if notifications are allowed, then calls theĀ sendNotificationĀ function.
The last conditional statement checks if the notifications are not disabled, it then requests the permission status before sending the notifications.
Next, create theĀ sendNotificationĀ function referenced in the code snippet above.
Update theĀ sendNotificationĀ function to display the notificationās content.
The code snippet above represents the layout of the notification, and when clicked, it redirects the user toĀ http://localhost:3000/chat.
Congratulations!šš» Weāve been able to display desktop notifications to the user when they send a message. In the next section, youāll learn how to send alerts to offline users.
š” Offline users are users not currently viewing the webpage or connected to the internet. When they log on to the internet, they will receive notifications.
How to detect if a user is viewing your web page
In this section, youāll learn how to detect active users on the chat page via theĀ Ā JavaScript Page visibility API. It allows us to track when a page is minimized, closed, open, and when a user switches to another tab.
Next, letās use the API to send notifications to offline users.
Update theĀ sendNotificationĀ function to send the notification only when users are offline or on another tab.
From the code snippet above,Ā document.onvisibilitychangeĀ detects visibility changes, andĀ document.hiddenĀ checks if the user is on another tab or the browser is minimised before sending the notification. You can learn more about the different statesĀ here.
Next, update theĀ checkPageStatusĀ function to send notifications to all the users except the sender.
Congratulations!šYou can now send notifications to offline users.
Optional: How to save the messages to a JSON ādatabaseā file
In this section, youāll learn how to save the messages in a JSON file ā for simplicity. Feel free to use any real-time database of your choice at this point, and you can continue reading if you are interested in learning how to use a JSON file as a database.
Weāll keep referencing theĀ server/index.jsĀ file for the remaining part of this article.
Retrieving messages from the JSON file
Navigate into the server folder and create aĀ messages.jsonĀ file.
Add some default messages to the file by copying the code below ā an array containing default messages.
Import and read theĀ messages.jsonĀ file into theĀ server/index.jsĀ file by adding the code snippet below to the top of the file.
Render the messages via the API route.
We can now fetch the messages on the client via theĀ ChatPageĀ component. The default messages are shown to every user when they sign in to the chat application.
Saving messages to the JSON file
In the previous section, we created aĀ messages.jsonĀ file containing default messages and displayed the messages to the users.
Here, Iāll walk you through updating theĀ messages.jsonĀ file automatically after a user sends a message from the chat page.
Update the Socket.io message listener on the server to contain the code below:
The code snippet above runs after a user sends a message. It adds the new data to the array in theĀ messages.jsonĀ file and rewrites it to contain the latest update.
Head back to the chat page, send a message, then reload the browser. Your message will be displayed. Open theĀ messages.jsonĀ file to view the updated file with the new entry.
Conclusion
In this article, youāve learnt how to send desktop notifications to users, detect if a user is currently active on your page, and read and update a JSON file. These features can be used in different cases when building various applications.
This project is a demo of what you can build withĀ Socket.io; you can improve this application by adding authentication and connecting any database that supports real-time communication.
The source code for this tutorial is available here:https://github.com/novuhq/blog/tree/main/build-a-chat-app-part-two
Thank you for reading!