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:
Navcomponent – containing the DEV Community logo, the bell icon, and the logout buttonCreatePostcomponent – containing the form inputs, and the buttonPostcomponent 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