So my question: does anyone have an idea on how to set up this communication? Or does anybody can link me to the official documentation that explains how this should be done
Hi Jesper,
Connecting two applications, in this case ALLPLAN with your JS app, would require an HTTP connection in the first place. One of them must be the server, the other one has to be the requester. In this constellation, your app will probably be the requester as it will pull the data from ALLPLAN. So ALLPLAN would be the server listening for the requests from your app.
Your initial approach was right: you will need to start a local HTTP server within ALLPLAN. You can use frameworks like FastAPI with uvicorn for this. And run the server on a separate deamon thread (to make sure, it gets terminated, when ALLPLAN closes):
Initialize the Flask app as a global, like:
APP = FastAPI()
APP.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
Define a function for running the server:
def run_server(app: FastAPI):
config = uvicorn.Config(app, host="127.0.0.1", port=5679, log_level="info", ws_ping_interval=0, ws_max_size=16777216)
server = uvicorn.Server(config)
server.run()And start the server in the __init__ method of your ScriptObject on a separate thread:
self.app = APP
self.thread = Thread(target = run_server, args=(self.app,))
self.thread.daemon = True
self.thread.start()
Remember to stop the host in the on_cancel_function
def on_cancel_function(self) -> OnCancelFunctionResult:
os.kill(os.getpid(), signal.SIGINT)
print("Python Host is stopped")
return OnCancelFunctionResult.CANCEL_INPUT
IMPORTANT: server is running only as long as PythonPart is running. Only in this case, your app may receive the data from ALLPLAN. This is a limitation you have to take into account. I am not aware, that a permanent background connection can be opened between ALLPLAN and another application.
Hopefully, after that you will be able to make a GET request from your app.
If you want to establish a persistent real-time two-way connection between your app and ALLPLAN, you will need to upgrade the HTTP connection to a websocket.
But getting an HTTP server running in ALLPLAN is the first step.
Cheers,
Bart