How to use Frappe context (frappe.get_doc) inside a WebSocket listener from an external service?
I am working with Frappe Framework and I need to consume a WebSocket from an external service (not Frappe’s built-in socket.io).
My goal is to process incoming WebSocket messages and interact with Frappe objects, such as:
frappe.get_doc(…)
frappe.db.insert(…)
frappe.db.commit()
I tried to run the WebSocket listener in a separate thread / background loop, but when a message is received, I get an error like: ‘frappe’ is not defined or Frappe context not initialized.
It seems that the WebSocket callback does not have access to the Frappe request/app context. I would like to ask:
-
Is it possible to use Frappe context (frappe.get_doc, frappe.db, etc.) inside a WebSocket listener that runs outside the normal request lifecycle?
-
What is the recommended architecture for integrating an external WebSocket service with Frappe?
Has anyone faced a similar issue when integrating external WebSocket services with Frappe?