Skip to content

Event Handling

kkMihai edited this page Jan 21, 2024 · 2 revisions

Event Handling in VirtualizorClient

Overview

The VirtualizorClient class extends the EventEmitter class in Node.js, allowing you to handle various events that occur during the interaction with the Virtualizor API.

Methods

on(event, callback)

  • Description: Attaches an event listener to the VirtualizorClient instance.
  • Parameters:
    • event - The event to listen to.
    • callback - The callback function to execute when the event is triggered.
Syntax
const Client = new VirtualizorClient(options)

Events

vpsCreated

  • Description: Triggered when a new virtual server is successfully created.
  • Example:
Client.on('vpsCreated', (response) => {
  console.log(`Virtual Server Created! Details:`, response);
});

vpsStarted

  • Description: Triggered when a virtual server is successfully started.
  • Example:
Client.on('vpsStarted', (response) => {
  console.log(`Virtual Server Started! Details:`, response);
});

vpsStopped

  • Description: Triggered when a virtual server is successfully stopped.
  • Example:
Client.on('vpsStopped', (response) => {
  console.log(`Virtual Server Stopped! Details:`, response);
});

vpsRestarted

  • Description: Triggered when a virtual server is successfully restarted.
  • Example:
Client.on('vpsRestarted', (response) => {
  console.log(`Virtual Server Restarted! Details:`, response);
});

Usage

  1. Import the VirtualizorClient class.
  2. Create an instance named Client with the necessary configuration (host, port, API key, API pass).
  3. If you need to perform specific actions, destructure the required methods from the Client instance.
  4. Attach event listeners to the Client instance using the on method (Client.on('event', callback)).