Live server implementation

Live server is an inbuilt server that runs on javascript language and helps to enable live php development, an environment in which the PHP is rendered on a live state. In order to keep live server efficient, the live server runs at the top of the application before any content is rendered when it is pre-enabled in top level core configuration file or when it is manually called called in routes. However, in template files, the live server script is imported at the point of call through the use of a live server template directive that will be discussed later.

Helper classes, functions and features have been restructured and integerated into this framework to support live development state within a specified range of support. For example, an error notification system was attached to php error handlers to convert php errors into an error pop-up notification message to reduce what is known as a "KILL EFFECT" which occurs when live server terminates and may not be able to continue unless the page is refreshed or the error is fixed before refreshing the page.

Certain PHP errors prevents the live server from running because they shut down the entire application. In order to reduce the effect of these errors, the liveserver will continue to run with a relative error pop-up notification message until the page is manually refreshed. Once the page is refreshed while such errors are active, the live server will not be able to restart until such errors are fixed and the page is refreshed to reboot the liveserver. In the cases where soft errors (e.g warning and notice errors) occurs, the liveserver will still try to reboot to keep the application on a live state and any error detected will be converted to a pop-up notification message.

Although, the live server may prove helpful, due to the comprehensive structure which php uses to handle its errors, spoova live server debug system is still known to have some minor issues. The live server implements different resource management approach to reduce its effect on cpu performance. One of these is the internal behavioral pattern of pausing watch mode for web pages that are not in view. Another feature and improvement added over time is the introduction of live server options that gives more controls over how the live server is expected to be used by the developer, giving the developer more controls over resource management. Although, the Live server was built to support majorly offline development, it is disabled by default and can be initialized or turned off through different ways which are further explained below:

Autoloading From Resource Importation System
This is a system which allows live server script to be loaded into the web page automatically when the page is loaded with its default resources that are managed and controlled by the Resource class. Once the basic core static files are loaded, the live server script will be loaded immediately so that developer can start to work on a live state mode. This mode requires activation through the command-line by running the command below :
  > php mi watch online
                            
The command above will turn the live server on for both online and offline environments. In order to keep it strictly for offline only, we can run the command below:
  > php mi watch offline
                            
We can also disable the default autoloading of the live server through the command below
  > php mi watch disable
                            
Should the command fail for any reason, this can be manually configured through icore/init file by setting "RESOURCE_WATCH" key to a default value of "1" or "2" which stands for offline or online respectively while a default of "0" disables the live server. These values should be defined without quotes. Although, setting up live server this way may sound easy, it is not the most efficient way to start the live server unless a template file cannot be reached. Template files are preferred and recommended for starting the live server.


Embedded Resource Loading
This is a system which allows the Resource class to load the live server through an embedded format which is added directly to the header of the page. There are two ways in which the live server can be imported in this format. The first method is shown below:
  <?= Res::import("::watch") ?>
                            
We can also do this using the alias of the method above. This is shown below
  <?= Res::live() ?>
                            
Should the command fail for any reason, this can be manually configured through icore/init file by setting "RESOURCE_WATCH" key to a default value of "1" or "2" which stands for offline or online respectively while a default of "2" disables the live server. These values should be defined without quotes. Although, setting up live server this way may sound easy, it is not the most efficient way to start the live server unless a template file cannot be reached. Template files are preferred and recommended for starting the live server.


Embedded Template Directive
This is by far the easiest way to load the live server. It involve the autoloading of live server into the webpage by the use of specified template directives. The template directive is added into the header section of the template file which is then rendered along with the page. Any of the template directives below can be used to import the live server.
  @live
                            
  @live()
                            
  @Res('::watch')
                            
Any of the template directives above can be added to a template file to import the live server script.


Route Loading Live server
This method is useful when working within Route files and we don't have access to the template files. The monitor() function can help to quickly load the live server script from any route file once the relative webpage is visited even if no template is loaded. An example is shown below:
  <?php 

  class Home extend Window {

    function __construct() {

        self::call($this, 'home');

    }

    function home() {

        monitor(); 

    }

  }
                            
                            
In the sample above, when the relative home webpage is visited, the live server will be loaded. This is useful for perfoming a quick code test.


Liveserver Inheritance, Control and Management
The inheritance behavior of the live server is one in which automatically makes a child template file to inherit a the live server mode from its parent template. Whenever the live server is started in a parent template file, any child of that parent will automatically inherit the live state mode. The following code sample demonstrates this relationship.

Parent Template (main.rex.php)
  <!DOCTYPE html>
  <html lang="en">
  <head>
      @live
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
  </head>
  <body>
      @yield()
  </body>
  </html>
                        

Child Template (index.rex.php)
  @template('main')

    //child html here...

  @template;
                        

In the sample above, the child template index will inherit the live server from its parent template main. The flexibility of the live server also makes it easy to turn off the live server for any child template even if it was turned on from a parent template. This behaviour makes it easier to manage the live server from a single parent file which every other children template will inherit from without any form of difficulty. The sample below is how we can turn off the live server for the child template file.
Child Template (index.rex.php)
  @template('main:off')

    //child html here...

  @template;
                        
Live server can also be switched off in routes with Res::off(). An example of this is shown below:
  <?php
  
  use Routes;
  
  class Home extends Routes {
     
     function __construct(){
 
      Res::off(); //switch off live server
      
      self::load('template', fn() => compile());      
     
     } 
     
  } 
                           

Liveserver Control and Management 2.6+
Starting from version 2.6 of the framework, the live server introduces the use of options and some safe practices to reduce the effect of polling requests. This means that developers can now control the maximum range of time which request are allowed to poll when in an an inactive state or perhaps employ other much safer practices that entirely eliminates the polling of requests through the use of live server options. Other added features includes the pausing of polled requests. Learn more about these new improvements from here