KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Terminate WebSocket client when form is closed
PRODUCT: 4D | VERSION: 20 | PLATFORM: Mac & Win
Published On: January 29, 2024

After opening a WebSocket client connection in a form using "4D.WebSocket.new( )", remember to close the connection when the form is closed. To do this, use the ".terminate( )" function:

: (Form event code=On Unload)
   Form.webSocket.terminate()

This ensures that no more messages go through the onMessage function of the WebSocket connection handler. Otherwise, any additional messages will trigger the onMessage function, which could cause errors for any Form variables that become undefined in onMessage after the form ceases to exist.