I implemented the Dev Community Notification Center with React, Novu, and Websockets š„
No matter what application you are building, you will probably send the user notifications at some point. It can be over Emails, SMSs, Push notifications, or a Notification center like the one you have on the DEV Community. I will show you how toā¦

For this demonstration, I created theĀ DEV CommunityĀ design ā at least, I tried to. I will send a notification in the bell icon every time there is a new post. And in case we get more than one post in 30 seconds, I will merge them into one notification (Digest/Batch).
Thatās actually a great way to get engagement in your app, but thatās for another day š¤Æ
What is Websockets (Socket.io)?
WebSockets create a connection between a client and a server, allowing them to send data both ways; client-server and server-client. Compared to HTTP, WebSockets provide a lasting bi-directional client-server connection, making it possible to send and receive messages in real-time.
In this article, Iāll useĀ Socket.ioĀ for real-time communication because it follows the WebSocket protocol and provides excellent functionalities, such as fallback to HTTP long-polling or automatic reconnection, which enables us to build efficient real-time applications.
How to connect a React app to Socket.io š
Here, weāll set up the project environment for the DEV Community clone. Youāll also learn how to add Socket.io to a React and Node.js application and connect both development servers for real-time communication via Socket.io.
Create the project folder containing two sub-folders named client and server.
Navigate into the client folder via your terminal and create a new React.js project.
Install Socket.io client API and React Router.Ā React RouterĀ is a JavaScript library that enables us to navigate between pages in a React application.
Delete the redundant files such as the logo and the test files from the React app, and update theĀ App.jsĀ file to display Hello World as below.
Navigate into the server folder and create aĀ package.jsonĀ file.
Install Express.js, CORS, Nodemon, and Socket.io Server API.
Express.jsĀ is a fast, minimalist framework that provides several features for building web applications in Node.js.Ā CORSĀ is a Node.js package that allows communication between different domains.
NodemonĀ is a Node.js tool that automatically restarts the server after detecting file changes, andĀ Socket.ioĀ allows us to configure a real-time connection on the server.
Create anĀ index.jsĀ file ā the entry point to the web server.
Set up a simple Node.js server using Express.js. The code snippet below returns a JSON object when you visit theĀ http://localhost:4000/apiĀ in your browser.
Import the HTTP and the CORS library to allow data transfer between the client and the server domains.
Next, add Socket.io to the project to create a real-time connection. Before theĀ app.get()Ā block, copy the code below. Next, add Socket.io to the project to create a real-time connection. Before theĀ app.get()Ā block, copy the code below.
From the code snippet above, theĀ socket.io("connection")Ā function establishes a connection with the React app, then creates a unique ID for each socket and logs the ID to the console whenever a user visits the web page.
When you refresh or close the web page, the socket fires the disconnect event showing that a user has disconnected from the socket.
Configure Nodemon by adding the start command to the list of scripts in theĀ package.jsonĀ file. The code snippet below starts the server using Nodemon.
You can now run the server with Nodemon by using the command below.
Open theĀ App.jsĀ file in the client folder and connect the React app to the Socket.io server.
Start the React.js server.
Check the terminal where the server is running; the ID of the React.js client should appear on the terminal.
Congratulations š„ , the React app has been successfully connected to the server via Socket.io.
š” For the remaining part of this article, I will walk you through creating the pages for the application. Weāll create a Home page ā where users sign in to the application, a 404 page for unauthenticated users, and a protected Post page only visible to authenticated users where they can create, view, and react to posts.
Creating the home page of the application
Here, weāll create the home page for the application that accepts the username and saves it to the local storage for identification.
Create a folder named components within theĀ client/srcĀ folder. Then, create the Home page component.
Copy the code below into theĀ Home.jsĀ file. The code snippet displays a form input that accepts the username and stores it in the local storage.
Configure React Router to enable navigation between the pages of the application. Copy the code below into theĀ src/App.jsĀ file and create the referenced components.
The code snippet assigns different routes to the Home, Post page, and Null page using React Router v6 and passes the Socket.io library into theĀ PostPageĀ component.
Navigate into theĀ src/index.cssĀ file and copy the code below. It contains all the CSS required for styling this project.
Weāve created the home page of our DEV Community clone. Next, letās design the user interface for the post route.
Creating the 404 page for unauthenticated users
In this section, weāll create a simple 404 page for unauthenticated users or users who are not on any of the defined routes of the application.
Navigate into theĀ components/NullPage.jsĀ and paste the code below:
Creating the protected Post page
In this section, weāll create theĀ PostPageĀ and make it visible to authenticated users only. Users can create, view, and react to posts and get notified when a user creates a post.
From the image above, the Post page is divided into three sections:
NavĀ component ā containing the DEV Community logo, the bell icon, and the logout buttonCreatePostĀ component ā containing the form inputs, and the buttonPostĀ component containing the already created posts.
Since weāve been able to define the layout for the Post page, we can now create the components for the design.
Copy the code below into theĀ PostPage.jsĀ file. You will need to create Nav, CreatePost, and Posts components.
The code snippet above checks if the user is signed-in before displaying the contents of the Post page; otherwise, it renders the 404 page (Null Page).
Building the Nav component
Copy the code below into theĀ Nav.jsĀ file.
The code snippet above displays the DEV Community logo and two buttons representing the logout and the notification icon. The logout button signs users out and redirects them to the home page.
Building the CreatePost component
Here, weāll create the form that allows users to create blog posts. The code snippet below accepts the title and content of the blog post and logs them to the console.
Building the Posts component
Copy the code below. The blog posts will be dummy posts for now.
Congratulations!šš» Weāve completed the user interface for the DEV Community clone. Next, letās create the required functionalities.
How to communicate between the React app and Socket.io server
In this section, youāll learn how to send messages from the React app to the Node.js server and vice-versa via Socket.io.
From theĀ App.jsĀ file, pass Socket.io down into theĀ PostPageĀ component ā where will be communicating with the server via web sockets.
Creating blog posts
Here, we will make it possible for users to create blog posts.
Update theĀ PostPage.jsĀ file to accept Socket.io as a prop and pass it into theĀ CreatePostĀ component.
Update theĀ CreatePostĀ component to send the posts to the backend Node.js server.
From the code snippet above, theĀ addNewPostĀ function emits a message labeledĀ newPostĀ containing a random id, the title, content, username, and the initial number of likes for the post.
Create the event listener on the backend by copying the code below within the Socket.io block
Displaying the blog posts to users
In the previous section, we successfully sent the posts to the backend. Here, we will send the posts back to the React app for display.
Create an array in theĀ server/index.jsĀ and add the newly created posts to the array.
Create another event that sends the array of posts to the React app.
Head back to theĀ PostPage.jsĀ file, create a listener to the āpostā event, and pass the details into theĀ PostsĀ component for display.
Render the posts via theĀ PostsĀ component. The code snippet below checks if the post array is not empty before passing the data into the user interface.
Next, letās enable users to like their favorite posts and update the number of likes accordingly.
Creating the ālike postā functionality
In this section, Iāll walk you through adding the ālike postā functionality to the application, enabling users to react to any post of their choice.
Pass Socket.io into theĀ PostsĀ component and create aĀ postLikedĀ function which triggers an event that sends the id of the post liked by the user to the Node.js server.
Create a listener on the Node.js server that accepts the post id, updates the number of likes, and sends the post with its newly updated likes count back to the React app.
Congratulations!š„š The application is almost complete.
Weāve been able to able set up the communication channels between the React app and the Node.js server. Next, letās learn how to display notifications to the users when a new blog post is created or liked.
How to add Novu to a React & Node.js app
In this section, youāll learn how to add Novu to the DEV Community clone to enable us to send notifications from the React app to the Socket.io server.
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 the Dev Community āĀ Websockets), Emails, SMSs and so on.
We are putting on awesome stuff every week!Follow us on Twitter so you can get things we donāt put here!https://twitter.com/novuhq
Navigate into the client folder and create a Novu project by running the code below.
You will need to sign in with Github before creating a Novu project. The code snippet below contains the steps you should follow after runningĀ npx novu init.
Visit the demo web pageĀ http://localhost:57807/demo, copy your subscriber ID from the page, and click the Skip Tutorial button. Weāll be using it later in this tutorial.
Install Novu Notification package as a dependency in your React project.
Update theĀ components/Nav.jsĀ file to contain Novu and its required elements from theĀ documentation.
The code snippet above adds Novuās notification bell icon to the Nav component, enabling us to view all the notifications in our app.
š” TheĀ NovuProviderĀ component requires your Subscriber ID ā copied earlier fromĀ http://localhost:57807/demoĀ and your application ID available in the Settings section under API Keys on theĀ Novu Manage Platform.
Navigate into the server folder and install the Novu SDK for Node.js.
Import Novu from the package and create an instance using your API Key.
Create a new POST route on the server ā from which Novu will send the notifications.
Sending in-app notifications with Novu
In the previous section, I walked you through setting up Novu on the React and Node.js server. Here, I will guide you through sending notifications via Novu in your web application.
Open theĀ Novu Manage PlatformĀ in your browser; a notification template is in the Notifications tab.
Select the template, click on Workflow Editor, and ensure the workflow is as below:
Novu DigestĀ allows you to control how you send notifications in your app. It collects multiple trigger events and sends them as a single message.
Click theĀ In-AppĀ step and edit the template to contain the content below
š” Novu allows you to add dynamic content or data to the templates usingĀ the Handlebars templating engine. The data for theĀ usernameĀ variable will be passed into the template when making the POST request to Novu.
Save the template by clickingĀ UpdateĀ button, then head back to your code editor, and update the POST route from the previous section as below:
The code snippet above triggers the notification template via its ID and also provides the required data āĀ usernameĀ as a payload to the template.
Having configured Novu on the server and created the route for sending the notifications, send a request to fetch the notifications on the frontend.
Update theĀ CreatePost.jsĀ file as below:
Congratulations! šš» Weāve completed the code for this project. You can view the notifications by clicking on the notification bell in the nav bar.
Conclusion
So far, youāve learnt how to add Novu to a React and Node.js application, send notifications with Novu, set up Socket.io in a React and Node.js application, and send messages between the client and a Node.js server.
This article demonstrates what you can build using Socket.io and Novu. Feel free to improve on the project by:
- adding an authentication library
- saving the blog posts to a database that supports real-time communication
- adding the ability to comment on each blog post
- sending notifications via Novu when a user reacts and comments on a blog post.
The complete code for this tutorial is available here:Ā https://github.com/novuhq/blog/tree/main/devto-notifications-novu
Thank you for reading!
P.SĀ We are putting on awesome stuff every week!Follow us on Twitter so you can get things we donāt put here!https://twitter.com/novuhq