Live Reload Server for Auto-Reloading when Code is changed

Like BrowserSync - which handles Client Side code changes in HTML, CSS, JavaScript and Script Mapped files - there's also a server side version that detects changes to PRG/VCX/INI/APP/EXE files and automatically restarts the Web Connection Server.

Works only in the IDE in File Mode

Live Server Reload only works in File Mode and then only when the application is running inside of Visual FoxPro environment as it needs to restart from within itself.

Requires Web Connection 7.05 and Later

This feature was introduced in Web Connection 7.05 and it requires some additional configuration in the AppMain.prg file. The new templates automatically add support but existing applications have to add small bit of additional code to the AppMain.prg file to enable this functionality.

Enabling LiveReloadServer

This option is disabled by default and has to be turned on explicitly via the LiveReloadServer configuration switch in your AppMain.ini file for your application.

Livereloadserver=On

Once enabled any change to a .prg, .vcx, '.app','.exe' or .ini file in the deploy folder structure causes the Web Connection server to be recycled.

Live Reload works best with BrowserSync

Live Reload is useful by itself as it can help with the start and stop server cycle. But it's even better if you also add client side Live Reload via Browser Sync that can reload both changes in client side code and the changes made by Live Reload on the server. Check out the documentation on Browser Sync and how you can easily set up and run Browser Sync with DO Launch with "BROWSERSYNC".

How does this work?

The LiveReloadServer functionality uses a .NET File System Watcher object to monitor file changes on disk in the code folder. Web Connection uses wwDotnetBridge to instantiate the Filewatcher and captures events on in it.

When a file is changed the watcher fires a notification into the event handler, and Web Connection then checks whether the file matches the specified templates for files to watch. If it does it clears the file based READ EVENTS loop and triggers a restart of the application via delayed execution after the application has shut down.

The code for this can be found in LiveReloadServer.prg.

Hooking up in AppMain.prg

If you're creating a new project in Web Connection 7.05 or later the LiveReloadServer handling based on the configuration switch is automatic.

If you have an older version that you are upgrading to 7.05 you can add the following bits of code to your AppMain.prg (ie. your customized version for your app).

After goWcServer has been declared add the following:

*** Load the server - wc3DemoServer class below
goWCServer = CREATEOBJECT("wcDemoServer")

*** Live Reload Server if files source files are changed on disk   
IF goWCServer.oConfig.lLiveReloadServer AND Application.StartMode = 0
  DO LiveReloadServer
ENDIF

This hooks up the file watcher and starts watching files. If the file watcher detects a change to files that are watched, the watcher fires a request to CLEAR EVENTS and sets the goWcServer.lAutoRestart = .T. flag.

Immediately following the READ EVENTS change the code to the following:

READ EVENTS

*** ADD THESE TWO LINES
LOCAL llAutoRestart
llAutoRestart = goWcServer.lAutoRestart

ON ERROR
...

*** CHANGE THIS BLOCK OF CODE
RELEASE ALL EXCEPT llAutoRestart

IF llAutoRestart
   IF VARTYPE("__WWC_FILEWATCHER") = "O"
      __WWC_FILEWATCHER.Destroy()
      __WWC_FILEWATCHER = null
   ENDIF
   CLEAR ALL
   
   *** Restart the application
   ACTIVATE WINDOW COMMAND
   KEYBOARD [CLEAR PROGRAM{ENTER}] + ; 
            [DO ] + STRTRAN(SYS(16,0),".FXP",".PRG") + [{ENTER}]  
ELSE
   *SET STEP ON  
   CLEAR ALL
ENDIF

This code sets a preserved local variable, then clears out the application's memory and state, and then stuffs a restart command into the keyboard buffer to relaunch the application. This is hacky but the only reliable way I've found to be able to completely release and restart the application.

Using an EXE instead of a PRG to Launch

If you'd rather run your compiled project's EXE file than the PRG you can change the KEYBOARD command to include project compilation and then run the EXE instead (using wcDemo as the app name here):

KEYBOARD [CLEAR PROGRAM{ENTER}] + ; 
         [BUILD EXE wcDemo from wcDemo RECOMPILE{ENTER}] + ;
         [DO wcDemo.exe{ENTER}]

This takes a little longer, but it's just as efficient.

See also

Using BrowserSync during Development to Live Reload Changes

© West Wind Technologies, 1996-2019 • Updated: 05/06/19
Comment or report problem with topic