Advertisement
semsem_elazazy

Connect to shelf socket

Jun 29th, 2025
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { io } from "socket.io-client";
  2.  
  3. // Connect to the shelf socket namespace
  4. const socket = io("https://your-server-url", {
  5.   // Add auth headers or namespace config if needed
  6. });
  7.  
  8. // Connection established
  9. socket.on("connect", () => {
  10.   console.log("🔗 Connected to shelf socket:", socket.id);
  11. });
  12.  
  13. //  Shelf state update event (specific product)
  14. socket.on("shelf-state-update", (response) => {
  15.   if (response.success) {
  16.     const { _id, shelfState, weight } = response.product;
  17.     console.log("Shelf product updated:", {
  18.       productId: _id,
  19.       state: shelfState,
  20.       weight: weight,
  21.     });
  22.     // Update UI with the new shelf product state
  23.   } else {
  24.     console.error("Shelf update failed:", response);
  25.   }
  26. });
  27.  
  28. //  All products state counts update (new event)
  29. socket.on("product-states-update", (response) => {
  30.   if (response.success) {
  31.     console.log("Updated product state counts:", response.stateCounts);
  32.     // response.stateCounts example:
  33.     // { available: 4, outOfStock: 2, lowStock: 3 }
  34.     // Update your dashboard/statistics panel accordingly
  35.   } else {
  36.     console.error("Failed to receive product state counts:", response);
  37.   }
  38. });
  39.  
  40. // Handle socket errors
  41. socket.on("error", (error) => {
  42.   console.error("Socket error:", error.message || error);
  43. });
  44.  
  45. //  Handle disconnect
  46. socket.on("disconnect", () => {
  47.   console.log("Disconnected from shelf socket");
  48. });
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement