Auction Bidding Platform Examples with WebSockets

Explore practical examples of auction bidding platforms using WebSockets for real-time interaction.
By Jamie

Auction Bidding Platform Examples with WebSockets

WebSockets provide a powerful way to enable real-time communication between clients and servers, particularly useful in auction bidding platforms. These platforms require instant updates to ensure that all bidders have the most current information about bids and auction statuses. Here are three diverse examples illustrating how WebSockets can be implemented in auction systems.

Example 1: Live Bid Updates

Context

In a live auction environment, it’s crucial for all participants to see updates in real-time, especially when new bids are placed. This example shows how WebSockets can facilitate live updates for bidders.

// Server-side WebSocket implementation using Node.js
const WebSocket = require('ws');
const server = new WebSocket.Server({ port: 8080 });

let currentBid = 100; // Starting bid

server.on('connection', (socket) => {
    // Send current bid to the newly connected client
    socket.send(JSON.stringify({ type: 'currentBid', bid: currentBid }));

    // Listen for incoming bids
    socket.on('message', (message) => {
        const data = JSON.parse(message);
        if (data.type === 'placeBid' && data.bid > currentBid) {
            currentBid = data.bid;
            // Notify all clients about the new bid
            server.clients.forEach((client) => {
                if (client.readyState === WebSocket.OPEN) {
                    client.send(JSON.stringify({ type: 'newBid', bid: currentBid }));
                }
            });
        }
    });
});

Notes

  • This implementation allows any client to place a bid that is greater than the current highest bid.
  • All connected clients receive the updated bid immediately, ensuring transparency in the auction process.

Example 2: Auction Countdown Timer

Context

An auction platform often features a countdown timer, which adds urgency for bidders. This example demonstrates how to use WebSockets to synchronize the countdown timer across all clients.

// Server-side countdown timer implementation
const WebSocket = require('ws');
const server = new WebSocket.Server({ port: 8080 });

let auctionEndTime = Date.now() + 300000; // 5 minutes from now

server.on('connection', (socket) => {
    // Notify clients of the auction end time
    socket.send(JSON.stringify({ type: 'auctionEndTime', endTime: auctionEndTime }));

    const interval = setInterval(() => {
        const timeLeft = auctionEndTime - Date.now();
        if (timeLeft <= 0) {
            clearInterval(interval);
            // Notify clients that the auction has ended
            server.clients.forEach((client) => {
                if (client.readyState === WebSocket.OPEN) {
                    client.send(JSON.stringify({ type: 'auctionEnded' }));
                }
            });
        } else {
            // Update clients with time left
            server.clients.forEach((client) => {
                if (client.readyState === WebSocket.OPEN) {
                    client.send(JSON.stringify({ type: 'timeLeft', time: timeLeft }));
                }
            });
        }
    }, 1000);
});

Notes

  • The countdown timer runs on the server, ensuring that all clients have an accurate view of the time left.
  • Clients receive updates every second, providing a real-time experience.

Example 3: Bidder Notifications

Context

In addition to live bid updates, bidders may want to receive notifications when they are outbid. This example illustrates how WebSockets can be leveraged to send notifications to individual clients.

// Server-side implementation for notifications
const WebSocket = require('ws');
const server = new WebSocket.Server({ port: 8080 });

let highestBid = { amount: 0, bidder: null };

server.on('connection', (socket) => {
    socket.on('message', (message) => {
        const data = JSON.parse(message);
        if (data.type === 'placeBid') {
            if (data.bid > highestBid.amount) {
                // Notify the previous highest bidder
                if (highestBid.bidder) {
                    highestBid.bidder.send(JSON.stringify({ type: 'outbid', bid: data.bid }));
                }
                // Update highest bid
                highestBid = { amount: data.bid, bidder: socket };
            } else {
                // Notify the current bidder that they were outbid
                socket.send(JSON.stringify({ type: 'outbid', bid: highestBid.amount }));
            }
        }
    });
});

Notes

  • This example ensures that bidders are informed when they have been outbid, allowing them to react quickly.
  • The server maintains the highest bid and the associated socket for notifications.

These examples illustrate how WebSockets can significantly enhance the functionality of an auction bidding platform, providing real-time interaction and updates that are essential for a competitive bidding environment.