WebSSH2 Server API Documentation
February 7, 2026 ยท View on GitHub
Overview
The WebSSH2 server provides a WebSocket interface for establishing SSH connections. This API documentation outlines the events and data structures used for communication between the client and the server.
Currently this implementation requires Socket.IO v2.2.0 due to this instance targeting node 6.9.1 for a legacy project. Future releases will not have this limitation.
Connection
The server uses Socket.IO for real-time communication. Connect to the WebSocket server at the same host and port as the HTTP server, with the path /ssh/socket.io.
Events
Server to Client Events
-
authentication- Emitted to request authentication or provide authentication results.
- Payload:
{ action: string, // "request_auth" or "auth_result" success?: boolean, // Only present for "auth_result" message?: string // Error message if authentication fails }
-
permissions- Emitted after successful authentication to provide allowed actions.
- Payload:
{ autoLog: boolean, allowReplay: boolean, allowReconnect: boolean, allowReauth: boolean }
-
getTerminal- Emitted to request terminal specifications from the client.
- Payload:
true
-
data- Emitted when there's output from the SSH session.
- Payload:
string | Buffer- Shell data is emitted as a raw
Buffer. Socket.IO 4.x automatically sends Buffers as WebSocket binary frames, avoiding the overhead of UTF-8 string conversion and JSON serialization. The client receives this as anArrayBuffer. - Exec data (from the
execchannel) is emitted as astring(UTF-8 encoded), since exec output is already a string from SSH2 and is not a high-throughput path.
- Shell data is emitted as a raw
- Client handling: Convert
ArrayBuffertoUint8Arrayand pass directly toxterm.jsTerminal.write(), which natively acceptsUint8Array. Only decode to string when text is needed (e.g., session logging).
-
ssherror- Emitted when an SSH-related error occurs.
- Payload:
string(Error message)
-
updateUI- Emitted to update specific UI elements.
- Payload:
{ element: string, // UI element identifier value: any // New value for the element }
Client to Server Events
-
authenticate- Emit this event to provide authentication credentials.
- Payload:
{ username: string, password: string, host: string, port: number, term?: string, // Optional terminal type cols?: number, // Optional terminal columns (added for early dimension setup) rows?: number, // Optional terminal rows (added for early dimension setup) forwardAllKeyboardInteractivePrompts?: boolean // When true, all keyboard-interactive prompts are forwarded to client } - The
forwardAllKeyboardInteractivePromptsoption bypasses the automatic password handling for keyboard-interactive auth. When set totrue, all prompts (including password prompts) will be forwarded to the client for user input. This is useful for debugging or when explicit user interaction is required.
-
terminal- Emit this event to provide terminal dimensions.
- Payload:
{ cols: number, rows: number } - Note: Terminal type (
term) is managed server-side based on authentication parameters or server configuration.
-
data- Emit this event to send user input to the SSH session.
- Payload:
string(UTF-8 encoded user input)
-
resize- Emit this event when the terminal size changes.
- Payload:
{ cols: number, rows: number }
-
control- Emit this event for special control commands.
- Payload:
string("replayCredentials" or "reauth")
-
disconnect- Emit this event to close the connection.
- No payload required
Authentication Flow
- The server emits
authenticationwithaction: "request_auth". - The client emits
authenticatewith credentials. - For keyboard-interactive authentication, the server may emit multiple
promptevents:- Each prompt contains fields for user input (password or text fields)
- The client should display these prompts and collect user responses
- The client responds via
prompt-responsewith the user's answers - This loop continues until authentication completes
- The server emits
authenticationwithaction: "auth_result"andsuccess: true/false.
Keyboard-Interactive Behavior
By default:
- First round: If all prompts contain "password", the server auto-responds with the stored password
- Subsequent rounds: All prompts are forwarded to the client (e.g., 2FA verification codes)
This behavior can be overridden:
- Set
forwardAllKeyboardInteractivePrompts: truein the authenticate payload to forward all prompts - Or configure
alwaysSendKeyboardInteractivePrompts: trueon the server
Establishing SSH Session
- After successful authentication, the server emits
getTerminal. - The client emits
terminalwith terminal dimensions (cols/rows only). - The server uses terminal type from authentication parameters or server defaults.
- The server establishes the SSH connection and starts emitting
dataevents with terminal output. - The client can now send
dataevents with user input.
Error Handling
- The client should listen for
ssherrorevents and handle them appropriately (e.g., displaying error messages to the user).
UI Updates
- The client should listen for
updateUIevents and update the corresponding UI elements.
Best Practices
- Handle connection errors and implement reconnection logic.
- Implement proper error handling and user feedback.
- Securely manage authentication credentials.
- Handle terminal resizing appropriately.
- Implement support for special control commands (replay credentials, reauthentication).