DEV Community

Cover image for Emulate User Activity with Bots
Yoram Kornatzky
Yoram Kornatzky

Posted on

2

Emulate User Activity with Bots

In our sales events platform, Auctibles, sellers have the exciting opportunity to create various events. These range from a sale event at a fixed price to live auctions, where buyers bid the price up, providing a dynamic and engaging experience.

The Technology

We use the Laravel framework in the context of the TALL stack: Tailwind, Alpine.js, Laravel, and Livewire.

Why Emulation?

For a demo environment, it's crucial that we accurately emulate buyers' activity for a seller. This includes sending buy orders and chat messages. The purpose of the emulation is to allow the seller to get a feeling of how the event with multiple buyers will be observed from the event control screens. This includes changing the quantities of items still available, dynamic price changes, and chat messages from buyers.

Ordinary Buyers

The buy screen is a Livewire component. Clicking the BUY button sends a broadcast event: LiveBuy, LiveBid,...

Similarly, buyers send chat messages via a broadcast event ChatMessage.

Bots for Emulation

We emulate buyers' activity via a bot. The bot is a ReactPHP server that periodically sends the same broadcast events that ordinary buyers send.

The server receives a set_timer GET call at the event's start and a stop_timer GET call at its end.

$loop = Factory::create();
$server = new Server(function (ServerRequestInterface $request) use($loop) {

        $path = $request->getUri()->getPath();
        $method = $request->getMethod();

        if ($path == '/set_timer' && $method === 'GET') {

          $params = $request->getQueryParams();
                $event_id = $params['event_id'] ?? null;

                // start emulation

                return Response::plaintext('timer set')->withStatus(Response::STATUS_OK);   

            } else if ($path == '/stop_timer' && $method === 'GET') {

            $params = $request->getQueryParams();
                $event_id = $params['event_id'] ?? null;   

                // stop emulation

                return Response::plaintext('timer stopped')->withStatus(Response::STATUS_OK);               
                    }

});

$socket = new SocketServer('0.0.0.0:' . config('app.BOT_SERVER_PORT'));
$server->listen($socket);

$loop->run();
Enter fullscreen mode Exit fullscreen mode

Laravel to ReactPHP

These calls are received from the component where the seller controls the event, using Http:

  $url = config('app.BOT_SERVER_URL') . ':' . config('app.BOT_SERVER_PORT') . '/set_timer';
  $data = [
  'event_id' => $this->event->id,
  ];

$response = Http::withQueryParameters($data)->get($url);
Enter fullscreen mode Exit fullscreen mode

Starting the Bot

To start the emulation, we set a periodic timer,

$timer = $loop->addPeriodicTimer($this->period_in_seconds, function () use ($event_id) {

   $this->send_bid($event_id);
   $this->send_message($event_id);

});
Enter fullscreen mode Exit fullscreen mode

We store the timer in an array:

$this->timers[$event_id] = $timer;
Enter fullscreen mode Exit fullscreen mode

Stopping the Bot

$loop->cancelTimer($this->timers[$event_id]);
unset($this->timers[$event_id]);
Enter fullscreen mode Exit fullscreen mode

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay