Overview
The Crypto Shop Backend uses Socket.io to push real-time notifications to clients when:- TRX payment transactions are confirmed on the blockchain
- Order status changes from “pending” to “completed”
- Product stock is updated
Socket.io Server Configuration
Fromsrc/config/socket.js:5, the server is configured with CORS support:
Connection Flow
Client Setup
Installation
Install the Socket.io client library:Basic Connection
Transaction Confirmation Event
Fromsrc/config/socket.js:37, when a transaction is confirmed:
Event Data Structure
Complete Integration Example
When Notifications are Sent
Fromsrc/services/transactionListener.js:72, notifications are sent when:
- A pending transaction receives 21 confirmations on the blockchain
- The transaction type is “purchase”
- The order status is “pending”
Room-based Messaging
The system uses room-based messaging to ensure users only receive their own notifications:- Each user joins a private room:
user:{userId} - Notifications are emitted only to the specific user’s room
- Multiple clients can connect with the same user ID and all will receive notifications
Error Handling
Browser Notifications
Request permission and show browser notifications:Testing Notifications
To test the notification system:Create an Order
Use the Creating Orders guide to create an order.
Pay for Order
Use the Payment Processing guide to pay.
Socket.io Events Reference
- Client → Server
- Server → Client
- Built-in Events
join-userJoin a user-specific room to receive notifications.Parameters:
userId(string): The user’s MongoDB ObjectId
Production Considerations
Next Steps
Payment Processing
Understand how payments trigger notifications
Creating Orders
Learn how to create orders that can be paid