Skip to main content
This guide explains how to connect to the OpenHands WebSocket API to receive real-time events and send actions to the agent.

Overview

OpenHands uses Socket.IO for WebSocket communication between the client and server. The WebSocket connection allows you to:
  1. Receive real-time events from the agent
  2. Send user actions to the agent
  3. Maintain a persistent connection for ongoing conversations

Connecting to the WebSocket

Connection Parameters

When connecting to the WebSocket, you need to provide the following query parameters:
  • conversation_id: The ID of the conversation you want to join
  • latest_event_id: The ID of the latest event you’ve received (use -1 for a new connection)
  • providers_set: (Optional) A comma-separated list of provider types

Connection Example

Here’s a basic example of connecting to the WebSocket using JavaScript:

Sending Actions to the Agent

To send an action to the agent, use the oh_user_action event:

Receiving Events from the Agent

The server emits events using the oh_event event type. Here are some common event types you might receive:
  • User messages (source: "user", type: "message")
  • Agent messages (source: "agent", type: "message")
  • File edits (action: "edit")
  • File writes (action: "write")
  • Command executions (action: "run")
Example event handler:

Using Websocat for Testing

Websocat is a command-line tool for interacting with WebSockets. It’s useful for testing your WebSocket connection without writing a full client application.

Installation

Connecting to the WebSocket

Sending a Message

Complete Example with Websocat

Here’s a complete example of connecting to the WebSocket, sending a message, and receiving events:

Event Structure

Events sent and received through the WebSocket follow a specific structure:

Best Practices

  1. Handle Reconnection: Implement reconnection logic in your client to handle network interruptions.
  2. Track Event IDs: Store the latest event ID you’ve received and use it when reconnecting to avoid duplicate events.
  3. Error Handling: Implement proper error handling for connection errors and failed actions.
  4. Rate Limiting: Avoid sending too many actions in a short period to prevent overloading the server.

Troubleshooting

Connection Issues

  • Verify that the OpenHands server is running and accessible
  • Check that you’re providing the correct conversation ID
  • Ensure your WebSocket URL is correctly formatted

Authentication Issues

  • Make sure you have the necessary authentication cookies if required
  • Verify that you have permission to access the specified conversation

Event Handling Issues

  • Check that you’re correctly parsing the event data
  • Verify that your event handlers are properly registered