KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: How to instantiate WebSocketServer object properly
PRODUCT: 4D | VERSION: 20 | PLATFORM: Mac & Win
Published On: July 27, 2023

The new WebSocketServer class allows developers to create a WebSocket server out of the box by calling 4D.WebSocketServer.new(). However, there are a couple additional things to do for it to work correctly.

1) An instance of a “ServerHandler” user class must be passed as the first parameter. This allows the WebSocket server to know how to handle specific server events such as when the server is started or when a new client connects.

2) The WebSocketServer object should be instantiated as a process variable in a new worker process. Otherwise, the WebSocket server can be destroyed prematurely if the current process ends.

Example:

$svrHandler:=cs.WSServerHandler.new()
CALL WORKER("WebSocketServer";Formula(WS_Server:=4D.WebSocketServer.new($svrHandler)))