Preparing the Applications

After lots of testing in our local development environment, prepare the applications that will be deployed to the production server.

First application to modify to support WebSockets is FMSoft Customer Portal: https://prime.fmsoft.net/userv/mportal.dll

clip0307

Currently the main form shows a date/time on the status bar that is updated once per 30 seconds using a UniTimer:

clip0308

Remove the timer event handler and change the timer interval to tick every 2 minutes only (to keep the session alive).

clip0309

Goal: update the date/time using WebSocket messages. Use a ThreadTimer in ServerModule to broadcast the date/time every second. The client will process that message and update the date/time from client-side code.

clip0310

Above: ThreadTimer will broadcast date/time once per second.

clip0311

Below is the server-side event handler that broadcasts date/time to all clients. Note: the boClientOnly option is used so the event does not fire Ajax requests; events are processed on the client side.

Add a client-side event handler to the main form to process the incoming message and update the status bar:

Enable the thread timer in code only when this Node is "Node Zero" of the cluster. Use the OnClusterNodeIdChange event (fired when NodeId or ServerNodeId changes and also called initially when the module is created). Because it can be called multiple times, assign directly to the Enabled property of the timer.

circle-info

The ThreadTimer should be enabled only for the cluster node that is Node Zero.

Example enabling code with optional debug logging:

When checking the log file of Node 0 you should see entries similar to:

mportal.exe: 00001AA0: 22:44:25 [TUniServerModule]:Node Id = 0

mportal.exe: 00001AA0: 22:44:25 [UniThreadTimer1]:Timer Enabled for Node: 0 -1

Testing on a local PC shows the timer is working properly and messages are processed by the client-side event handler.

clip0313
1

1. Remove/adjust the local UniTimer

  • Remove the UniTimer event that updated the status bar every 30 seconds.

  • Set that timer to tick every 2 minutes (to keep sessions alive).

2

2. Add a ThreadTimer to the ServerModule

  • Add a ThreadTimer that fires once per second.

  • In its OnTimer event, call BroadcastMessage with the 'timer' message and the formatted Now value.

3

3. Add client-side handler on MainForm

  • Implement form.socketmessage to process the 'timer' message and set the status bar text from params.now.

4

4. Enable the ThreadTimer only on Node Zero

  • In UniGUIServerModuleClusterNodeIdChange, set UniThreadTimer1.Enabled := ClusterNodeZero.

  • Optionally log a message for debugging to confirm the timer is enabled for the correct node.

5

5. Verify

  • Check Node 0 logs for the “Timer Enabled” entry.

  • Test locally to ensure the client receives messages and the status bar updates every second.