[Part 1] Building a real-time Auction System with Socket.io and React.js 🤯
Like an actual auction, if you bid for a product, you get counterbids from other bidders. The auction runs on the "fast" decision bid, where somebody else will win or outbid you if you don't bid fast enough. To use online bidding, We must stick to…

There are two ways to get live information from your server about a new bid:
- Use a long-polling HTTP request, basically an HTTP request every 5 – 10 seconds, to get information about a new bid.
- Use an open socket (Websockets) to get information directly from the server when a new bid arrives.
In this article, I will talk about Websockets and specifically the Node.js library – Socket.io
Novu – the first open-source notification architecture
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 on Facebook), Emails, SMSs, and so on.
Looking for new contributors
Come help us out to build the best open-source notification infrastructure, get recognized by the community, and become a Community Hero here:https://novu.co/contributors
So what the hack is Socket.io?
Socket.io is a JavaScript library that enables us to create real-time, bi-directional communication between web browsers and a Node.js server. It is a highly performant library capable of processing a large volume of data within the shortest possible time.
Usually, to get information from the server, you need to send an HTTP request. However, with WebSockets, the server lets you know when there is new information without asking it.
In this article, we’ll leverage the real-time communication provided by Socket.io to create a bidding system that allows users to put items up for auction and bid for them. Socket.io will also notify users when an item is up for auction and after a user places a bid.
How to add Socket.io to React & Node.js applications
In this section, we’ll set up the project environment for our bidding system. 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.
Next, 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.
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.
Next, configure Nodemon by adding the start command to the list of the 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 appears in 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 flow of the bidding system, and in the upcoming article in this series, I will guide you through sending messages between the client and the server and saving them in a JSON file.💡 The JSON file will serve as the database for the application. Although this is not a secure way of saving data, this is just a demo, so feel free to use any database of your choice if necessary.
The Workflow for the Bidding System
Before we start building each component, I’ll walk you through the application’s workflow.
Here is how it works:
- The Home page: Users provide only their username, and the application saves this username for identification throughout the application. To keep the tutorial simple, we won’t use any authentication library.
- The Products page: Users can view all the products up for auction, click on each product to bid, and there is a call to action that redirects users to the page where they can add items for auction.
- The Add Products page: This page allows users to add the name and price of the auction item, then redirects them to the Products page to view the recently added item.
- The Bid page: Users can bid for the item they selected from the Products page. This page accepts URL parameters containing the name and the price of the chosen item; then displays a form input that allows users to bid up the product.
- The Nav component: All the pages have the Nav component at the top and display notifications within it. When a user sets a bid or adds a new product, the Nav component notifies every other user.
Without any further ado, create a component folder containing all the pages. Ensure that each page renders an HTML element.
Next, Import all the files within the components folder into the App.js file and create a route for each page using React Router v6.
The code snippet declares the route for each page and passes the Socket.io library into the necessary components.
Navigate into the src/index.css and copy the code below. It contains all the CSS required for styling this project.
Congratulations 💃🏻, we can start coding every part of the project.
Creating the Home page of the application
In this section, we’ll create the home page for the bidding system. The page will accept the username from the user and then save it to the local storage for identification throughout the application.
Update the Home.js file to render a form field that accepts a minimum of six letters as username.
Create the handleSubmit function that stores the username in the local storage, then redirects the user to the Products page after submitting the form.
From the code snippet below, the useNavigate hook enables us to redirect users between pages.
Creating the Products page
In this section, I’ll walk you through creating a simple layout that displays each product and the related information. The product details include the name, price, owner, and the last bidder.A table layout containing each product on every row is the least complicated layout for this data structure.So, let’s code it! 💪
Update the Products.js to display a table containing two products with four columns containing the name, price, last bidder, and the creator.
We’ve been able to display the items available for auction to the users. Next, we need to allow users to add a product and bid on each item. An easy way is to create a hyperlink that links to the Add Products page and an edit button for bidding on items.
Update the Products page to contain the edit button and a call to action for adding products.
Creating the Add Product page
In this section, we’ll create the AddProduct page containing a form with two input fields for the name and price of the product up for auction and a submit button.
From the code above, the handleSubmit button collects the user’s input from the form and logs it to the console before redirecting to the Products page. The username saved to the local storage is also attached to the item as the product owner.
Creating the Bid page
The Bid page is quite similar to the AddProduct page. It contains a form with an input field for the bid price of the selected product and a call to action. After a user places a bid, it redirects them to the Product page.
Creating the Nav component
The Nav component is at the top of every page (according to the App.js file). It represents the app’s notification center – where users view the notifications from Socket.io.
Update the Nav.js file to render a <nav> element as below. The h2 element represents the logo, and the notification container is on the right-hand side of the screen.
Congratulations, we’ve completed the first part of this series. In next week’s article in this series, I’ll walk you through sending messages between the React app and the Node.js server.
You can find the full source code here:https://github.com/novuhq/blog/tree/main/bidding%20system%20using%20socketIO
Thank you for reading! 🥂