KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Live Progress Between 4D Processes with Websocket
PRODUCT: 4D | VERSION: 21 | PLATFORM: Mac & Win
Published On: November 18, 2026
A long job usually reports its progress by being asked again and again. A WebSocket lets 4D push it instead: each message goes to every connected client the moment it is sent, with no polling. The client is 4D code too, 4D.WebSocket : another process, another application, or a form that would pass each message to its progress bar.

The 4D WebSocket server shares the host and port of the 4D web server, which must be running, and it must live in a worker. Progress_Push hops into the worker named Progress when it is called from anywhere else, and the first push creates the server inside a singleton class whose life is that of its process, so no process variable is needed. The same class is the handler: onConnection accepts every client, and onMessage has nothing to do because clients only listen.

On the listening side, ProgressListener is the connection handler given to 4D.WebSocket.new in a worker; its callbacks run in that worker, and dataType "object" delivers each message as the object that was sent. Nothing has to keep a reference to the 4D.WebSocket object: in the test the connection stayed open without one, so the listener closes it itself at 100%. In the test, a second worker listens while the example runs a ten second job, then reports what it received: twenty messages, half a second apart, as the job ran and not all at the end.

ProgressHandler (class)

property server : 4D.WebSocketServer

singleton Class constructor()
    This.server:=4D.WebSocketServer.new(This; {path: "/progress"; dataType: "object"})

Function onConnection($wss : Object; $event : Object) : Object
    return This

Function onMessage($ws : 4D.WebSocketConnection; $event : Object)

Function push($message : Object)
    var $connection : 4D.WebSocketConnection
    
    For each ($connection; This.server.connections)
        $connection.send($message)
    End for each


Progress_Push

#DECLARE($percent : Integer; $label : Text)

If (Current process name#"Progress")
    CALL WORKER("Progress"; Current method name; $percent; $label)
Else
    cs.ProgressHandler.me.push({percent: $percent; label: $label})
End if


Test example: ProgressListener (class)

property dataType : Text
property received : Collection
property ready : 4D.Signal
property finished : 4D.Signal

Class constructor($received : Collection; $ready : 4D.Signal; $finished : 4D.Signal)
    This.dataType:="object"
    This.received:=$received
    This.ready:=$ready
    This.finished:=$finished

Function onOpen($ws : 4D.WebSocket; $event : Object)
    This.ready.trigger()

Function onMessage($ws : 4D.WebSocket; $event : Object)
    Use (This.received)
        This.received.push(New shared object("percent"; $event.data.percent; "label"; $event.data.label; "at"; Milliseconds))
    End use
    If ($event.data.percent=100)
        This.finished.trigger()
        $ws.terminate()
    End if



Test example: Example_Progress

#DECLARE() : Object

var $received : Collection:=New shared collection
var $ready : 4D.Signal:=New signal
var $finished : 4D.Signal:=New signal
var $step : Integer

If (Not(WEB Server.isRunning))
    WEB Server.start({HTTPPort: 8099})
End if
Progress_Push(0; "Waiting for the job")
DELAY PROCESS(Current process; 30)

CALL WORKER("Listener"; Formula(4D.WebSocket.new("ws://127.0.0.1:8099/progress"; cs.ProgressListener.new($received; $ready; $finished))))
$ready.wait(5)

For ($step; 1; 20)
    DELAY PROCESS(Current process; 30)
    Progress_Push($step*5; "Step "+String($step)+" of 20")
End for
$finished.wait(5)

return {count: $received.length; first: $received.first().label; last: $received.last().label; msBetween: Round(($received.max("at")-$received.min("at"))/($received.length-1); 0)}


Result

{
    "count": 20,
    "first": "Step 1 of 20",
    "last": "Step 20 of 20",
    "msBetween": 510
}