Live-shopping app Whatnot is in talks to nearly double its valuation to $20bn
Back to Tutorials
techTutorialintermediate

Live-shopping app Whatnot is in talks to nearly double its valuation to $20bn

July 29, 202642 views4 min read

Learn to build a real-time live-shopping platform backend using Node.js and WebSockets, mimicking the core functionality of platforms like Whatnot.

Introduction

In the rapidly evolving world of e-commerce, live-shopping platforms like Whatnot are revolutionizing how consumers discover and purchase products. This tutorial will guide you through building a basic live-shopping platform backend using Node.js and WebSockets, mimicking the core functionality of live commerce platforms. You'll learn how to handle real-time product listings, audience engagement, and streaming data - essential components for modern live-shopping experiences.

Prerequisites

  • Basic understanding of JavaScript and Node.js
  • Node.js installed (version 14 or higher)
  • npm (Node Package Manager) installed
  • Basic knowledge of WebSockets and real-time communication
  • Text editor or IDE (VS Code recommended)

Step-by-Step Instructions

1. Initialize Your Project

First, create a new directory for your live-shopping platform and initialize the Node.js project:

mkdir live-shopping-platform
 cd live-shopping-platform
 npm init -y

This creates the basic project structure and package.json file that will manage your dependencies.

2. Install Required Dependencies

Install the core packages needed for your live-shopping platform:

npm install express socket.io

We're using Express for the web server and Socket.IO for real-time bidirectional communication between the server and clients.

3. Create the Main Server File

Create a file named server.js and add the basic server setup:

const express = require('express');
const http = require('http');
const socketIo = require('socket.io');

const app = express();
const server = http.createServer(app);
const io = socketIo(server);

// Serve static files
app.use(express.static('public'));

// Store active products and viewers
let activeProducts = [];
let viewers = [];

// Handle socket connections
io.on('connection', (socket) => {
  console.log('User connected:', socket.id);
  
  // Handle product listing
  socket.on('listProduct', (product) => {
    product.id = Date.now();
    product.viewers = 0;
    activeProducts.push(product);
    io.emit('productListed', product);
  });
  
  // Handle viewer joining
  socket.on('joinStream', (data) => {
    const viewer = { id: socket.id, name: data.name, product: data.productId };
    viewers.push(viewer);
    
    // Update product viewer count
    const product = activeProducts.find(p => p.id === data.productId);
    if (product) {
      product.viewers++;
      io.emit('productUpdate', product);
    }
    
    socket.emit('joinedStream', { product, viewer });
  });
  
  socket.on('disconnect', () => {
    console.log('User disconnected:', socket.id);
    // Remove viewer from list
    viewers = viewers.filter(v => v.id !== socket.id);
  });
});

const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});

This setup creates a foundation for real-time communication, allowing users to list products and join streams while tracking viewer engagement.

4. Create the Frontend Interface

Create a public directory and add an index.html file:

<!DOCTYPE html>
<html>
<head>
  <title>Live Shopping Platform</title>
  <script src='/socket.io/socket.io.js'></script>
</head>
<body>
  <h1>Live Shopping Platform</h1>
  
  <div id='productForm'>
    <h2>List a Product</h2>
    <input type='text' id='productName' placeholder='Product Name'>
    <input type='number' id='productPrice' placeholder='Price'>
    <input type='text' id='productDescription' placeholder='Description'>
    <button onclick='listProduct()'>List Product</button>
  </div>
  
  <div id='streamArea'>
    <h2>Live Streams</h2>
    <div id='products'></div>
  </div>
  
  <script>
    const socket = io();
    
    function listProduct() {
      const name = document.getElementById('productName').value;
      const price = document.getElementById('productPrice').value;
      const description = document.getElementById('productDescription').value;
      
      socket.emit('listProduct', { name, price, description });
      
      document.getElementById('productName').value = '';
      document.getElementById('productPrice').value = '';
      document.getElementById('productDescription').value = '';
    }
    
    socket.on('productListed', (product) => {
      const productsDiv = document.getElementById('products');
      productsDiv.innerHTML += `
        <div class='product' data-id='${product.id}'>
          <h3>${product.name}</h3>
          <p>Price: $${product.price}</p>
          <p>Description: ${product.description}</p>
          <p>Viewers: ${product.viewers}</p>
          <button onclick='joinStream(${product.id})'>Join Stream</button>
        </div>
      `;
    });
    
    function joinStream(productId) {
      const name = prompt('Enter your name:');
      if (name) {
        socket.emit('joinStream', { name, productId });
      }
    }
  </script>
</body>
</html>

This HTML interface allows users to list products and join live streams, demonstrating the real-time interaction that makes live-shopping engaging.

5. Add Product Update Handling

Enhance the server to handle product updates when viewers join:

// Add this to your existing server.js file
socket.on('joinStream', (data) => {
  const viewer = { id: socket.id, name: data.name, product: data.productId };
  viewers.push(viewer);
  
  // Update product viewer count
  const product = activeProducts.find(p => p.id === data.productId);
  if (product) {
    product.viewers++;
    io.emit('productUpdate', product);
  }
  
  socket.emit('joinedStream', { product, viewer });
});

// Listen for product updates
io.on('connection', (socket) => {
  // ... existing code
  
  socket.on('productUpdate', (product) => {
    io.emit('productUpdate', product);
  });
});

This ensures that as more viewers join a stream, the product listing updates in real-time to show the current viewer count, mimicking the engagement dynamics of platforms like Whatnot.

6. Run Your Application

Start your server by running:

node server.js

Visit http://localhost:3000 in your browser to test the live-shopping platform. You can list products and simulate viewers joining streams to see real-time updates.

Summary

This tutorial demonstrated how to build a foundational live-shopping platform backend using Node.js and Socket.IO. You've learned to handle real-time product listings, manage viewer engagement, and implement bidirectional communication between clients and server. While this is a simplified version, it captures the core technologies used in platforms like Whatnot that are driving the live commerce revolution in Western markets. The architecture you've built can be extended with features like real-time bidding, product recommendations, and payment processing to create a complete live-shopping experience.

Source: TNW Neural

Related Articles