Building a Notion-like system with Socket.io And React šŸ˜

We are going to build a knowledge system like Click-Up and Notion. You will be able to add posts, write comments, tag other users and show it in their notifications. In notion users can see what other users do in real-time without refreshing the page…

Author:Nevo David
Nevo David

What is WebSocket?

WebSocket is a built-in Node.js module that enables us to create a real-time connection between a client and a server, allowing them to send data in both ways. However, WebSocket is low-level and doesn’t provide the functionalities required to build complex real-time applications; this is why Socket.io exists.

Socket.ioĀ is a popular JavaScript library that allows us to create real-time, bi-directional communication between software applications and a Node.js server. It is optimised to process a large volume of data with minimal delay and provides better functionalities, such as fallback to HTTP long-polling or automatic reconnection.

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.

How to create a real-time connection with React & Socket.io

Here, we’ll set up the project environment for the notion app. 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.

mkdir notion-platform
cd notion-platform
mkdir client server

Navigate into the client folder via your terminal and create a new React.js project.

cd client
npx create-react-app ./

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.

npm install socket.io-client react-router-dom

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.

function App() {
    return (
        
            Hello World!</p>
        </div>
    );
}
export default App;

Add the Socket.io client API to the React app as below:

import { io } from "socket.io-client";

//\ud83d\udc47\ud83c\udffb http://localhost:4000 is where the server host URL.
const socket = io.connect("http://localhost:4000");

function App() {
    return (
        
            Hello World!</p>
        </div>
    );
}
export default App;

Navigate into the server folder and create a package.json file.

cd server & npm init -y

Install Express.js, CORS, Nodemon, and Socket.io Server API.

npm install express cors nodemon socket.io

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.

touch index.js

Set up a 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.

//\ud83d\udc47\ud83c\udffbindex.js
const express = require("express");
const app = express();
const PORT = 4000;

app.use(express.urlencoded({ extended: true }));
app.use(express.json());

app.get("/api", (req, res) => {
    res.json({
        message: "Hello world",
    });
});

app.listen(PORT, () => {
    console.log(`Server listening on ${PORT}`);
});

Import the HTTP and the CORS library to allow data transfer between the client and the server domains.

const express = require("express");
const app = express();
const PORT = 4000;

//\ud83d\udc47\ud83c\udffb New imports
const http = require("http").Server(app);
const cors = require("cors");

app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cors());

app.get("/api", (req, res) => {
    res.json({
        message: "Hello world",
    });
});

http.listen(PORT, () => {
    console.log(`Server listening on ${PORT}`);
});

Next, add Socket.io to the project to create a real-time connection. Before theĀ app.get()Ā block, copy the code below.

//\ud83d\udc47\ud83c\udffb New imports
.....
const socketIO = require('socket.io')(http, {
    cors: {
        origin: "http://localhost:3000"
    }
});

//\ud83d\udc47\ud83c\udffb Add this before the app.get() block
socketIO.on('connection', (socket) => {
    console.log(`\u26a1: ${socket.id} user just connected!`);

    socket.on('disconnect', () => {
      socket.disconnect()
      console.log('\ud83d\udd25: A user disconnected');
    });
});

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.

//\ud83d\udc47\ud83c\udffb In server/package.json"
scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon index.js"
  }

You can now run the server with Nodemon by using the command below.

npm start

Building the user interface

Here, we’ll create the user interface for the notion application to enable users to sign in, write posts, add comments, and tag other users.

Navigate into theĀ client/srcĀ folder and create a components folder containingĀ Login.js,Ā Home.js,Ā CreatePost.js, andĀ NotionPost.jsĀ files.

cd client
mkdir components
cd components
touch Login.js Home.js CreatePost.js NotionPost.js

Update theĀ App.jsĀ file to render the newly created components on different routes via React Router as below:

import { BrowserRouter, Routes, Route } from "react-router-dom";
import NotionPost from "./components/NotionPost";
import CreatePost from "./components/CreatePost";
import Home from "./components/Home";
import Login from "./components/Login";
import { io } from "socket.io-client";

const socket = io.connect("http://localhost:4000");

function App() {
    return (
        
            
                } />
                } />
                } />
                } />
            </Routes>
        </BrowserRouter>
    );
}

export default App;

Navigate into theĀ src/index.cssĀ file and copy the code below. It contains all the CSS required for styling this project.

The Login page

@import url("https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@300;400;500;600;700&display=swap");
* {
    box-sizing: border-box;
    padding: 0;
    margin: 0;
    font-family: "Space Grotesk", sans-serif;
}
body {
    padding: 0;
}
.login {
    width: 100%;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}
.login > h2 {
    color: #00abb3;
    margin-bottom: 30px;
}
.loginForm {
    width: 70%;
    display: flex;
    flex-direction: column;
}
.loginForm > input {
    margin: 10px 0;
    padding: 10px 15px;
}
.home__navbar {
    width: 100%;
    height: 10vh;
    padding: 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid #eaeaea;
}
.home__navbar > h2 {
    color: #00abb3;
}
.home__buttons {
    display: flex;
    align-items: center;
    justify-content: baseline;
}
.home__createBtn {
    padding: 10px;
    cursor: pointer;
    margin-right: 10px;
    background-color: #00abb3;
    border: none;
    outline: none;
    color: #fff;
}
.home__createBtn:hover,
.createForm__button:hover {
    background-color: #02595e;
}
.home__notifyBtn {
    padding: 10px;
    cursor: pointer;
    color: #00abb3;
    background-color: #fff;
    border: 1px solid #3c4048;
    outline: none;
    width: 100px;
}
.posts__container {
    width: 100%;
    min-height: 90vh;
    padding: 30px 20px;
}
.post {
    width: 100%;
    min-height: 8vh;
    background-color: #00abb3;
    border-radius: 5px;
    padding: 20px;
    display: flex;
    color: #eaeaea;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 15px;
}
.post__cta {
    padding: 10px;
    background-color: #fff;
    cursor: pointer;
    outline: none;
    border: none;
    border-radius: 2px;
}
.createPost {
    min-height: 100vh;
    width: 100%;
    padding: 30px 20px;
}
.createPost > h2 {
    text-align: center;
    color: #00abb3;
}
.createForm {
    width: 100%;
    min-height: 80vh;
    padding: 20px;
    display: flex;
    flex-direction: column;
}
.createForm__title {
    padding: 10px;
    height: 45px;
    margin-bottom: 10px;
    text-transform: capitalize;
    border: 1px solid #3c4048;
}
.createForm__content {
    padding: 15px;
    margin-bottom: 15px;
    border: 1px solid #3c4048;
}
.createForm__button {
    width: 200px;
    padding: 10px;
    height: 45px;
    background-color: #00abb3;
    color: #fff;
    outline: none;
    border: none;
    cursor: pointer;
    border-radius: 5px;
}
.notionPost {
    width: 100%;
    min-height: 100vh;
    background-color: #eaeaea;
    display: flex;
    flex-direction: column;
    padding: 30px 50px;
}
.notionPost__container {
    width: 90%;
    min-height: 70vh;
    margin-bottom: 30px;
}
.notionPost__author {
    color: #00abb3;
}
.notionPost__date {
    opacity: 0.4;
    font-size: 12px;
}
.notionPost__content {
    padding-top: 30px;
    line-height: 200%;
}
.comments__container {
    min-height: 70vh;
    border: 1px solid #3c4048;
    padding: 30px;
    box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.05), 2px 2px 3px 1px rgba(208, 213, 219, 0.28);
    border-radius: 3px;
}
.comments__inputContainer {
    display: flex;
    align-items: center;
    margin: 30px 0;
}
.comments__input {
    width: 50%;
    padding: 15px;
    margin-right: 15px;
}
.comments__cta,
.login__cta {
    padding: 15px;
    width: 200px;
    cursor: pointer;
    outline: none;
    border: none;
    background-color: #00abb3;
    color: #fff;
}
.comment {
    margin-bottom: 15px;
}

Here, the application accepts the username and saves it in the local storage for user identification. Copy the code below into the Login component.

import React, { useState } from "react";
import { useNavigate } from "react-router-dom";

const Login = () => {
    const [username, setUsername] = useState("");
    const navigate = useNavigate();

    const handleLogin = (e) => {
        e.preventDefault();
        //The username \ud83d\udc49\ud83c\udffb console.log({ username });
        localStorage.setItem("username", username);
        navigate("/dashboard");
    };
    return (
        
            Sign in to HackNotion</h2>
            
                Enter your username</label>
                 setUsername(e.target.value)}
                />
                LOG IN</button>
            </form>
        </div>
    );
};

export default Login;

The Home page

Copy the code below into theĀ Home.jsĀ file. It represents the home layout for the application.

import React from "react";
import { useNavigate } from "react-router-dom";

const Home = () => {
    const navigate = useNavigate();

    const createPostBtn = () => navigate("/post/create");
    const readMoreBtn = () => navigate("/post/:id");

    return (
        
            
                HackNotion</h2>
                
                    
                        CREATE POST
                    </button>
                    NOTIFY</button>
                </div>
            </nav>

            
                
                    How to create a new Socket.io client</h3>
                    
                        READ MORE
                    </button>
                </div>

                
                    Creating React Native project with Expo</h3>
                    
                        READ MORE
                    </button>
                </div>
            </div>
        </div>
    );
};

export default Home;

The NotionPost page

This page is dynamic and displays the content of each post via the ID passed into the URL. Here, users can read the notion post and add comments.

Copy the code below into theĀ NotionPost.jsĀ file:

import React, { useState } from "react";

const NotionPost = () => {
    const [comment, setComment] = useState("");

    const handleAddComment = (e) => {
        e.preventDefault();
        console.log({ comment });
        setComment("");
    };

    return (
        
            
                How to create a new React Native project with Expo</h1>
                
                    By Nevo David</p>
                    Created on 22nd September, 2022</p>
                </div>

                
                    For this article, I will use Puppeteer and ReactJS. Puppeteer is a
                    Node.js library that automates several browser actions such as form
                    submission.
                </div>
            </div>

            
                Add Comments</h2>
                
                     setComment(e.target.value)}
                    />
                    Add Comment</button>
                </form>

                
                    
                        Scopsy Dima</span> - Nice post
                        fam!\u2764\ufe0f
                    </p>
                </div>
            </div>
        </div>
    );
};

export default NotionPost;

The CreatePost page

Here, we’ll create a simple layout that allows users to create posts by adding the title and its content. Users will also be able to tag other users usingĀ React Tag.

React TagĀ is a library that allows us to create tags easily via a single component. It provides several features, such as autocomplete based on a suggestion list, reordering using drag-and-drop, and many more.*

Copy the code below into theĀ CreatePost.jsĀ file.

import React, { useState } from "react";
import { useNavigate } from "react-router-dom";

const CreatePost = () => {
    const navigate = useNavigate();

    const [postTitle, setPostTitle] = useState("");
    const [postContent, setPostContent] = useState("");

    //...gets the publish date for the post
    const currentDate = () => {
        const d = new Date();
        return `${d.getDate()}-${d.getMonth() + 1}-${d.getFullYear()}`;
    };

    //...logs the post details to the console
    const addPost = (e) => {
        e.preventDefault();
        console.log({
            postTitle,
            postContent,
            username: localStorage.getItem("username"),
            timestamp: currentDate(),
        });
        navigate("/dashboard");
    };

    return (
        <>
            
                Create a new Post</h2>
                
                     Title</label>
                     setPostTitle(e.target.value)}
                        className='createForm__title'
                    />

                     Content</label>
                     setPostContent(e.target.value)}
                        className='createForm__content'
                    />

                    ADD POST</button>
                </form>
            </div>
        </>
    );
};

export default CreatePost;

Import React Tags into theĀ CreatePost.jsĀ file:

import { WithContext as ReactTags } from "react-tag-input";

Update the CreatePost component to contain the code snippet below for creating tags with React Tags.

//\ud83d\udc47\ud83c\udffb The suggestion list for autocomplete
const suggestions = ["Tomer", "David", "Nevo"].map((name) => {
    return {
        id: name,
        text: name,
    };
});

const KeyCodes = {
    comma: 188,
    enter: 13,
};
//\ud83d\udc47\ud83c\udffb The comma and enter keys are used to separate each tags
const delimiters = [KeyCodes.comma, KeyCodes.enter];

//...The React component
const CreatePost = () => {
    //\ud83d\udc47\ud83c\udffb An array containing the tags
    const [tags, setTags] = useState([]);

    //...deleting tags
    const handleDelete = (i) => {
        setTags(tags.filter((tag, index) => index !== i));
    };

    //...adding new tags
    const handleAddition = (tag) => {
        setTags([...tags, tag]);
    };

    //...runs when you click on a tag
    const handleTagClick = (index) => {
        console.log("The tag at index " + index + " was clicked");
    };

    return (
        
            
                {/**...below the input fields---*/}
                
                ADD POST</button>
            </form>
        </div>
    );
};

export default CreatePost;

React Tags also allows us to customize its elements. Add the following code to theĀ src/index.cssĀ file:

/*
You can learn how it's styled here: 
https://stackblitz.com/edit/react-tag-input-1nelrc
*/
.ReactTags__tags react-tags-wrapper,
.ReactTags__tagInput {
    width: 100%;
}
.ReactTags__selected span.ReactTags__tag {
    border: 1px solid #ddd;
    background: #00abb3;
    color: white;
    font-size: 12px;
    display: inline-block;
    padding: 5px;
    margin: 0 5px;
    border-radius: 2px;
    min-width: 100px;
}

.ReactTags__selected button.ReactTags__remove {
    color: #fff;
    margin-left: 15px;
    cursor: pointer;
    background-color: orangered;
    padding: 0 10px;
    border: none;
    outline: none;
}

.ReactTags__tagInput input.ReactTags__tagInputField,
.ReactTags__tagInput input.ReactTags__tagInputField:focus {
    margin: 10px 0;
    font-size: 12px;
    width: 100%;
    padding: 10px;
    height: 45px;
    text-transform: capitalize;
    border: 1px solid #3c4048;
}

.ReactTags__selected span.ReactTags__tag {
    border: 1px solid #ddd;
    background: #63bcfd;
    color: white;
    font-size: 12px;
    display: inline-block;
    padding: 5px;
    margin: 0 5px;
    border-radius: 2px;
}
.ReactTags__selected a.ReactTags__remove {
    color: #aaa;
    margin-left: 5px;
    cursor: pointer;
}

/* Styles for suggestions */
.ReactTags__suggestions {
    position: absolute;
}
.ReactTags__suggestions ul {
    list-style-type: none;
    box-shadow: 0.05em 0.01em 0.5em rgba(0, 0, 0, 0.2);
    background: white;
    width: 200px;
}
.ReactTags__suggestions li {
    border-bottom: 1px solid #ddd;
    padding: 5px 10px;
    margin: 0;
}
.ReactTags__suggestions li mark {
    text-decoration: underline;
    background: none;
    font-weight: 600;
}
.ReactTags__suggestions ul li.ReactTags__activeSuggestion {
    background: #fff;
    cursor: pointer;
}

.ReactTags__remove {
    border: none;
    cursor: pointer;
    background: none;
    color: white;
}

Congratulations! We’ve completed the layout for the notion application. Next, let’s learn how to add all the needed functionalities with the Socket.io Node.js server.

Creating new posts with Socket.io

In this section, I’ll guide you on how to create new posts and display them on the React app with Socket.io.

Update theĀ addPostĀ function within theĀ CreatePostĀ component by sending the newly created post to the server via Socket.io.

//\ud83d\udc47\ud83c\udffb Socket.io was passed from the App.js file
const CreatePost = ({ socket }) => {
    //...other functions

    const addPost = (e) => {
        e.preventDefault();

        //\ud83d\udc47\ud83c\udffb sends all the post details to the server
        socket.emit("createPost", {
            postTitle,
            postContent,
            username: localStorage.getItem("username"),
            timestamp: currentDate(),
            tags,
        });
        navigate("/dashboard");
    };

    return ...</div>;
};

Create a listener to the event on the server.

socketIO.on("connection", (socket) => {
    console.log(`\u26a1: ${socket.id} user just connected!`);

    socket.on("createPost", (data) => {
        /*\ud83d\udc47\ud83c\udffb data - contains all the post details 
             from the React app
        */
        console.log(data);
    });

    socket.on("disconnect", () => {
        socket.disconnect();
        console.log("\ud83d\udd25: A user disconnected");
    });
});

Create an array on the backend server that holds all the posts, and add the new post to the list.

//\ud83d\udc47\ud83c\udffb generates a random ID
const fetchID = () => Math.random().toString(36).substring(2, 10);

let notionPosts = [];

socket.on("createPost", (data) => {
    const { postTitle, postContent, username, timestamp, tags } = data;
    notionPosts.unshift({
        id: fetchID(),
        title: postTitle,
        author: username,
        createdAt: timestamp,
        content: postContent,
        comments: [],
    });
    //\ud83d\udc49\ud83c\udffb We'll use the tags later for sending notifications

    //\ud83d\udc47\ud83c\udffb The notionposts are sent back to the React app via another event
    socket.emit("updatePosts", notionPosts);
});

Add a listener to the notion posts on the React app via the useEffect hook by copying the code below:

//\ud83d\udc47\ud83c\udffb Within Home.js file

useEffect(() => {
    socket.on("updatePosts", (posts) => console.log(posts));
}, [socket]);

Displaying the posts

Save the posts into a state and render them as below:

import React, { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";

const Home = ({ socket }) => {
    const navigate = useNavigate();
    const [posts, setPosts] = useState([]);

    //\ud83d\udc47\ud83c\udffb Saves the posts into the "posts" state
    useEffect(() => {
        socket.on("updatePosts", (posts) => setPosts(posts));
    }, [socket]);

    const createPostBtn = () => navigate("/post/create");

    //\ud83d\udc47\ud83c\udffb Navigates to the NotionPost page to view
    //   all the post contents
    const readMoreBtn = (postID) => {
        navigate(`/post/${postID}`);
    };

    return (
        
            
                HackNotion</h2>
                
                    
                        CREATE POST
                    </button>
                    NOTIFY</button>
                </div>
            </nav>

            
                {posts?.map((post) => (
                    
                        {post.title}</h3>
                         readMoreBtn(post.id)}>
                            READ MORE
                        </button>
                    </div>
                ))}
            </div>
        </div>
    );
};

export default Home;

So far, we can only view the posts when we add one. Next, let’s make it possible for us to display the posts when we load the page.

Create a route on the server that returns the notion posts.

app.get("/api", (req, res) => {
    res.json(notionPosts);
});

Update theĀ Home.jsĀ file fetch the notion posts and listen for new posts from the server.

useEffect(() => {
    function fetchPosts() {
        fetch("http://localhost:4000/api")
            .then((res) => res.json())
            .then((data) => setPosts(data))
            .catch((err) => console.error(err));
    }
    fetchPosts();
}, []);

useEffect(() => {
    socket.on("updatePosts", (posts) => setPosts(posts));
}, [socket]);

Completing the Notion Post component

In the previous section, you learnt how to create and display notion posts to users. Here, you’ll learn how to show the contents of each notion post when you click the Read More button.

Update theĀ readMoreBtnĀ function within theĀ Home.jsĀ file as below:

const readMoreBtn = (postID) => {
    socket.emit("findPost", postID);
//\ud83d\udc47\ud83c\udffb navigates to the Notionpost routenavigate(`/post/${postID}`);
};

The code snippet above gets the ID of the selected post and sends a Socket.io event containing the post ID to the server before redirecting to the post route.

Create a listener to theĀ findPostĀ event and return the post details via another Socket.io event.

socket.on("findPost", (postID) => {

  //\ud83d\udc47\ud83c\udffb Filter the notion post via the post ID
  let result = notionPosts.filter((post) => post.id === postID);

  //\ud83d\udc47\ud83c\udffb Returns a new event containing the post details
    socket.emit("postDetails", result[0]);
});

Listen to theĀ postDetailsĀ event with theĀ NotionPostĀ component and render the post details as below:

import React, { useState, useEffect } from "react";
import { useParams } from "react-router-dom";

const NotionPost = ({ socket }) => {
    //\ud83d\udc47\ud83c\udffb gets the Post ID from its URL
    const { id } = useParams();

    const [comment, setComment] = useState("");
    const [post, setPost] = useState({});

    //\ud83d\udc47\ud83c\udffbloading state for async request
    const [loading, setLoading] = useState(true);

    //\ud83d\udc47\ud83c\udffb Gets the post details from the server for display
    useEffect(() => {
        socket.on("postDetails", (data) => {
            setPost(data);
            setLoading(false);
        });
    }, [socket]);

    //\ud83d\udc47\ud83c\udffb Function for creating new comments
    const handleAddComment = (e) => {
        e.preventDefault();
        console.log("newComment", {
            comment,
            user: localStorage.getItem("username"),
            postID: id,
        });
        setComment("");
    };

    if (loading) {
        return Loading... Please wait</h2>;
    }

    return (
        
            
                {post.title}</h1>
                
                    By {post.author}</p>
                    Created on {post.createdAt}</p>
                </div>

                {post.content}</div>
            </div>

            
                Add Comments</h2>
                
                     setComment(e.target.value)}
                    />
                    Add Comment</button>
                </form>

                
                    
                        Scopsy Dima</span> - Nice post
                        fam!\u2764\ufe0f
                    </p>
                </div>
            </div>
        </div>
    );
};

export default NotionPost;

The Comments section

Here, I’ll guide you through adding comments to each notion post and displaying them in real-time.

Update theĀ handleAddCommentĀ function within theĀ NotionPostĀ component to send the new comments details to the server.

const handleAddComment = (e) => {
    e.preventDefault();
    socket.emit("newComment", {
        comment,
        user: localStorage.getItem("username"),
        postID: id,
    });
    setComment("");
};

Create a listener to the event on the server that adds the comment to the list of comments.

socket.on("newComment", (data) => {
    const { postID, user, comment } = data;

//\ud83d\udc47\ud83c\udffb filters the notion post via its ID
let result = notionPosts.filter((post) => post.id === postID);

//\ud83d\udc47\ud83c\udffb Adds the comment to the comments list
    result[0].comments.unshift({
        id: fetchID(),
        user,
        message: comment,
    });
//\ud83d\udc47\ud83c\udffb sends the updated details to the React app
    socket.emit("postDetails", result[0]);
});

Update theĀ NotionPost.jsĀ file to display any existing comments.

return (
    
        
            {post.title}</h1>
            
                By {post.author}</p>
                Created on {post.createdAt}</p>
            </div>

            {post.content}</div>
        </div>

        
            Add Comments</h2>
            
                 setComment(e.target.value)}
                />
                Add Comment</button>
            </form>

            {/** Displays existing comments to the user */}
            
                {post.comments.map((item) => (
                    
                        
                            {item.user}
                        </span>
                        {item.message}
                    </p>
                ))}
            </div>
        </div>
    </div>
);

Congratulations!šŸŽŠ You can now create posts and add comments with Socket.io. For the remaining part of this tutorial, I’ll guide you through sending notifications to every user you tag in your post using Novu.

How to add Novu to a React and Node.js application

Novu allows you to add various notification types, such as email, SMS, and in-app notifications. In this tutorial, you will learn how to create a Novu project, add Novu to your React and Node.js projects, and send an in-app notification with Novu.

Install the Novu Node.js SDK on the server and the Notification Center in the React app.

\ud83d\udc47\ud83c\udffb Install on the client
npm install @novu/notification-center

\ud83d\udc47\ud83c\udffb Install on the server
npm install @novu/node

Create a Novu project by running the code below. A personalised dashboard is available to you.

\ud83d\udc47\ud83c\udffb Run on the client
npx novu init

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

Now let's setup your account and send your first notification
\u2753 What is your application name? Notionging-Platform
\u2753 Now lets setup your environment. How would you like to proceed?
   > Create a free cloud account (Recommended)
\u2753 Create your account with:
   > Sign-in with GitHub
\u2753 I accept the Terms and Condidtions (https://novu.co/terms) and have read the Privacy Policy (https://novu.co/privacy)
    > Yes
\u2714\ufe0f Create your account successfully.

We've created a demo web page for you to see novu notifications in action.
Visit: http://localhost:57807/demo to continue

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.

Congratulations!šŸŽŠ You’ve successfully added Novu to your React and Node.js project. Next, let’s learn how to add in-app notifications to the notionging application to notify users when we tag them to a post.

Adding in-app notifications with Novu

Create aĀ Notify.jsĀ file within theĀ src/componentsĀ folder and copy the code below into the file. It contains the elements required for in-app notifications from theĀ documentation.

import React from "react";
import {
    NovuProvider,
    PopoverNotificationCenter,
    NotificationBell,
} from "@novu/notification-center";
import { useNavigate } from "react-router-dom";

const Notify = () => {
    const navigate = useNavigate();

    const onNotificationClick = (notification) =>
        navigate(notification.cta.data.url);

    return (
        
            
                
                    {({ unseenCount }) => }
                </PopoverNotificationCenter>
            </NovuProvider>
        </div>
    );
};

export default Notify;

The code snippet above adds Novu notification bell icon to the Notify component, enabling us to view all the notifications within the application.

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.

Import the Notify component into theĀ Home.jsĀ file and display the bell icon as below:

return (
        
            
                HackNotion</h2>
                
                    
                        CREATE POST
                    </button>
                    
                </div>
            </nav>
    </div>
)
\ufeff

Next, create the workflow for the application, which describes the features you want to add to the application.

Select Notification from the Development sidebar and create a notification template. Click the newly created template, then, Workflow Editor, and ensure the workflow is as below

From the image above, Novu triggers the Digest engine before sending the in-app notification.

Novu DigestĀ allows us to control how we want to send notifications within the application. It collects multiple trigger events and sends them as a single message. The image above sends notifications every 2 minutes, and it can be effective when you have many users and frequent updates.

Click theĀ In-Appstep from the image above and edit the notification template to contain the content below.

{{sender}} tagged  you to a post
Novu allows you to add dynamic content or data to the templates usingĀ the Handlebars templating engine. The data for theĀ senderĀ variable will be inserted into the template as a payload from the request within our app.

Save the template by clickingĀ UpdateĀ button and head back to your code editor.

Sending notifications with Novu

Since we want to send notifications to users when we tag them to a post, we will have to store each username on the server, show them on the suggestion list provided by React Tags, and send them notifications via Novu.

Update theĀ handleLoginĀ function within theĀ Login.jsĀ file to send the username to the server when they sign in.

const handleLogin = (e) => {
    e.preventDefault();
    //\ud83d\udc47\ud83c\udffb sends the username to the server
    socket.emit("addUser", username);
    localStorage.setItem("username", username);
    navigate("/dashboard");
};

Listen to the event and store the username in an array on the server.

let allUsers = [];

socket.on("addUser", (user) => {
    allUsers.push(user);
});

Also, render the list of users via another route on the server.

app.get("/users", (req, res) => {
    res.json(allUsers);
});

To show the users within the React Tags suggestion list, you need to send a request to the API route, get the list of users, and pass them into the list.Update theĀ CreatePostĀ function to fetch the list of users, and save them within the local storage before navigating theĀ post/createĀ route.

const createPostBtn = () => {
    fetchUser();
    navigate("/post/create");
};

const fetchUser = () => {
    fetch("http://localhost:4000/users")
        .then((res) => res.json())
        .then((data) => {
            //\ud83d\udc47\ud83c\udffb converts the array to a string
            const stringData = data.toString();
            //\ud83d\udc47\ud83c\udffb saved the data to local storage
            localStorage.setItem("users", stringData);
        })
        .catch((err) => console.error(err));
};

Next, retrieve all the users from the local storage and pass them into the suggestion list provided by React Tags for display within theĀ CreatePostĀ component.

const [users, setUsers] = useState([]);

useEffect(() => {
    function getUsers() {
        const storedUsers = localStorage.getItem("users").split(",");
        setUsers(storedUsers);
    }
    getUsers();
}, []);

const suggestions = users.map((name) => {
    return {
        id: name,
        text: name,
    };
});

To notify each tagged user, create a function that loops through the users on the server and sends them a notification via Novu to them.

Import and initiate Novu on the server.

const { Novu } = require("@novu/node");
const novu = new Novu("")

Update theĀ createPostĀ listener on the backend to send a notification to all tagged users.

//\ud83d\udc47\ud83c\udffb Loops through the tagged users and sends a notification to each one of them
const sendUsersNotification = (users, sender) => {
    users.forEach(function (user) {
        novuNotify(user, sender);
    });
};

//\ud83d\udc47\ud83c\udffb sends a notification via Novu
const novuNotify = async (user, sender) => {
    try {
        await novu
            .trigger("", {
                to: {
                    subscriberId: user.id,
                    firstName: user.text,
                },
                payload: {
                    sender: sender,
                },
            })
            .then((res) => console.log("Response >>", res));
    } catch (err) {
        console.error("Error >>>>", { err });
    }
};

socket.on("createPost", (data) => {
    const { postTitle, postContent, username, timestamp, tags } = data;
    notionPosts.unshift({
        id: fetchID(),
        title: postTitle,
        author: username,
        createdAt: timestamp,
        content: postContent,
        comments: [],
    });
    //\ud83d\udc47\ud83c\udffb Calls the function to send a notification to all tagged users
    sendUsersNotification(tags, username);

    socket.emit("updatePosts", notionPosts);
});

Congratulations! šŸ’ƒšŸ» We’ve completed the code for this project.

Conclusion

So far, you’ve learned how to set up Socket.io in a React and Node.js application, send messages between the client and a Node.js server, add Novu to a React and Node.js application, and send notifications with Novu.

This tutorial demonstrates what you can build using Socket.io and Novu. Feel free to improve on the project by adding an authentication library and saving the blog posts to a database that supports real-time communication.

The complete code for this tutorial is available here:Ā https://github.com/novuhq/blog/tree/main/blogging-platform-with-react-socketIO Thank you for reading!

Read More

You’re five minutes away from your first Novu-backed notification

Create a free account, send your first notification, all before your coffee gets cold... no credit card required.