This WebSocket worker example allows direct communication with a serverless worker without using RunPod’s default REST API. It enables real-time, bidirectional communication between a websocket client and the worker.
- Wake up the worker – You must send a request(
https://api.runpod.ai/v2/endpointId/run
) to wake up the serverless worker. - Retrieve the worker’s
public IP
andTCP port
– Once the worker is awake,handler
function will obtain worker's public IP and TCP port from the environment variables. - Share connection details – Use progress_update to share the public IP and TCP port with any backend application that plans to communicate with the worker.
- Fetch connection details – Call
https://api.runpod.ai/v2/endpointId/status/request_id
to retrieve the public IP and TCP port. - Establish a WebSocket connection – Use a WebSocket client to connect to the worker using the obtained IP and port.
- Complete communication and shut down – Once the client or backend application is done, send a shutdown signal, in this example, we are transmitting the string “shutdown”.
- Graceful termination – Upon receiving "shutdown", the WebSocket server will shut down, the handler function will return, and the worker instance will be terminated.
- After deploying this code, remember to go to Serverless Settings → Docker Configuration → Expose TCP Ports and expose port 8765, or whichever port your WebSocket server is running on.
- If you choose a different port, ensure that you update the environment variable name accordingly, as the exposed port number is appended to the variable name.