How to user external socket in frappe backend

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:

  1. Is it possible to use Frappe context (frappe.get_doc, frappe.db, etc.) inside a WebSocket listener that runs outside the normal request lifecycle?

  2. 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?

Maybe you can have the external Websocket integrate the realtime infrastructure Frappe provides and take it from there, e.g. have your own doctype react to the incoming socketio channels.

This might need some changes to the compose file.

You could also add your own socketio listener to the compose zoo, more or less copying what the websocket container does.