Skip to content

Latest commit

 

History

History
19 lines (13 loc) · 2.17 KB

File metadata and controls

19 lines (13 loc) · 2.17 KB

Serverless WebSocket Worker Example

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.

How It Works

  1. Wake up the worker – You must send a request(https://api.runpod.ai/v2/endpointId/run) to wake up the serverless worker.
  2. Retrieve the worker’s public IP and TCP port – Once the worker is awake, handler function will obtain worker's public IP and TCP port from the environment variables.
  3. 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.
  4. Fetch connection details – Call https://api.runpod.ai/v2/endpointId/status/request_id to retrieve the public IP and TCP port.
  5. Establish a WebSocket connection – Use a WebSocket client to connect to the worker using the obtained IP and port.
  6. 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”.
  7. Graceful termination – Upon receiving "shutdown", the WebSocket server will shut down, the handler function will return, and the worker instance will be terminated.

Important Deployment Notes

  • 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.