Skip to content

feat: Implement real-time stock updates using WebSockets (#178)#278

Open
devprashant19 wants to merge 1 commit into
niharika-mente:mainfrom
devprashant19:feature/issue-178-websockets
Open

feat: Implement real-time stock updates using WebSockets (#178)#278
devprashant19 wants to merge 1 commit into
niharika-mente:mainfrom
devprashant19:feature/issue-178-websockets

Conversation

@devprashant19

Copy link
Copy Markdown
Contributor

Description

This Pull Request integrates Socket.io to push real-time stock updates from our Express backend to all connected React clients. This resolves the issue where users were forced to manually refresh the page to see if an item went out of stock.

Fixes #178

Changes Made

  • Backend (BACKEND/server.js, BACKEND/controllers/):
    • Installed socket.io and wrapped the Express application in an http.Server to attach the WebSocket server.
    • Added io.emit("stockUpdate") inside the stripeWebhook logic and updateProduct route so that any stock reduction broadcasts globally.
  • Frontend (FRONTEND/src/pages/ProductPage.jsx, FRONTEND/src/components/ui/ProductCard.jsx):
    • Installed socket.io-client and initialized a global socket connection to the backend.
    • Added useEffect hooks in the product view components to listen for incoming stockUpdate events.
    • Successfully synced the UI, dynamically disabling the "Add to Cart" button and changing the stock badges immediately across all active browser sessions when stock drops to 0.

Acceptance Criteria Met

  • Set up Socket.io alongside the Express server.
  • Emit a stockUpdate event containing productId and newStock upon stock updates.
  • Connect to the WebSocket on the ProductPage and ProductCard components.
  • Listen for stockUpdate events and dynamically update the UI in real-time.

How to Test Locally

  1. Checkout this branch and run npm install in both FRONTEND and BACKEND directories.
  2. Start both development servers using npm run dev.
  3. Open the application in two side-by-side browser windows.
  4. From Browser A, trigger a checkout flow or edit the product stock as an admin so that a product's stock is reduced to 0.
  5. Watch Browser B: The product should instantly show "Out of Stock" and the "Add to Cart" button should be disabled without any manual page refresh!

@vercel

vercel Bot commented Jun 16, 2026

Copy link
Copy Markdown

@devprashant19 is attempting to deploy a commit to the niharika-mente's projects Team on Vercel.

A member of the Team first needs to authorize it.

@Aamod-Dev Aamod-Dev added SSoC26 Hard enhancement New feature or request frontend For any changes in the React UI, components, or Vite setup backend For changes related to the Express server, API routes, or MongoDB models labels Jun 18, 2026
@Aamod-Dev

Copy link
Copy Markdown
Collaborator

Review: Real-time stock updates via WebSockets. Check WebSocket server setup, stock update event triggers, client-side WebSocket connection, reconnection handling, and scalability with multiple clients. Document WebSocket endpoint. Hard difficulty appropriate.

@devprashant19

Copy link
Copy Markdown
Contributor Author

@Aamod-Dev please review it now

@Aamod-Dev Aamod-Dev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

package-lock.json has unresolved merge conflict markers (<<<<<<< HEAD / >>>>>>> origin/main). This will break npm install.

Fix: Run npm install in BACKEND/ and FRONTEND/ to regenerate clean lockfiles, then commit.

@devprashant19 devprashant19 force-pushed the feature/issue-178-websockets branch from 09bd4df to d809ea3 Compare June 20, 2026 09:27

@Aamod-Dev Aamod-Dev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Request Changes

  • No backend code emits stockUpdate events. io server is created but no controller or model hook emits stock changes. Frontend listeners will never fire.
  • socket instance created at module level in ProductCard.jsx and ProductPage.jsx. Connects on import for all users, even on pages that do not need it.
  • test-socket.mjs should not be committed, or move to a test directory.
  • Unrelated lint cleanup changes should be in a separate PR.

@Aamod-Dev

Copy link
Copy Markdown
Collaborator

Missing backend WebSocket emission

@Aamod-Dev Aamod-Dev closed this Jun 20, 2026
@Aamod-Dev Aamod-Dev reopened this Jun 20, 2026
@devprashant19 devprashant19 force-pushed the feature/issue-178-websockets branch 2 times, most recently from 69b7e5b to 617fa71 Compare June 20, 2026 19:18

@Aamod-Dev Aamod-Dev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, approved!

@Aamod-Dev Aamod-Dev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs changes: There are merge conflicts. Please resolve them.

@devprashant19 devprashant19 force-pushed the feature/issue-178-websockets branch from 617fa71 to cbcf26e Compare June 22, 2026 10:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend For changes related to the Express server, API routes, or MongoDB models enhancement New feature or request frontend For any changes in the React UI, components, or Vite setup Hard SSoC26

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Real-Time Stock Updates using WebSockets

2 participants