Exercise: Build an Auction System

Create a dynamic auction system using Node.js's EventEmitter to simulate an interactive bidding process.

Task

Imagine you're building an online auction platform where participants can join, place bids, and leave as the auction progresses. This exercise will challenge you to use event-driven programming to simulate the auction, implementing features such as registering listeners for each participant, dynamically removing listeners when participants leave, and tracking active bidders in real time.

You’ll program the auctioneer to announce the start of the auction, manage bids, and clean up listeners when the auction ends. As participants interact, their actions will trigger events, making this a dynamic, interactive system.

Requirements

The following requirements outline the key features and functionality you need to implement for a fully event-driven auction system.

  • Announce the start of the auction:

    • Use the once method to ensure the auctioneer announces the start of the auction only once.

    • Include the auction item's name in the announcement.

  • Participants join the auction:

    • Write a createBidder function to dynamically register each participant.

    • Each participant should have a separate listener for the bid event, registered through the createBidder function.

    • The createBidder function should also provide:

      • A placeBid function for the participant to place bids.

      • A withdraw function to remove the participant from the auction.

    • Ensure participants hear and respond to bids from others but not their own.

  • Participants place bids:

    • Each bid must include the bidder's name and the bid amount.

    • Broadcast the bid to all other participants.

    • After each bid, display the total number of active participants using the listenerCount method.

  • Participants withdraw:

    • Use the removeListener method to unregister the listener for a withdrawing participant.

    • After withdrawal, update and display the total number of active participants.

  • End the auction:

    • Use the .once() method to announce the end of the auction.

    • Use the .removeAllListeners() method to clean up all listeners when the auction ends.

Simulated auction flow

The following flow demonstrates how the auction system should function, showcasing the sequence of events and participant interactions.