Terminal API
TypeScript
Remotely run terminals within the model filesystem.
Events
This API has 2 events that can be subscribed to.
To receive events, call the the 'client.terminal.on' function on the API client. This will register a callback to fire when the Decthings server sends events to the client. In order for Decthings to send events, you must be subscribed to each terminal session. This can be done using the subscribeToEvents method. Alternatively, when you start a command using launchTerminalSession, you will by default atomatically be subscribed to events for that terminal.
/**
* Event emitted when a terminal session exits.
*/
on(event: 'exit', handler: (params: {
terminalSessionId: string,
reason: TerminalSessionTerminatedReason
}) => void): this
/**
* Event emitted when output for a terminal session is received.
*/
on(event: 'data', handler: (params: {
terminalSessionId: string,
data: Buffer
}) => void): this
Where "TerminalSessionTerminatedReason" is defined as:
export type TerminalSessionTerminatedReason = {
code: 'terminated_on_request' | 'launcher_terminated' | 'inactive_timeout' | 'unknown'
} | {
code: 'process_exit',
exitCode?: number,
signal?: string,
oom: boolean
}
Subscribe to events example
exit
let client = new DecthingsClient({ apiKey: "<myapikey>" })
async function main() {
// First, subscribe to events so that Decthings will actually send us any
await client.terminal.subscribeToEvents({ terminalSessionId: '<terminalsessionid>' })
// Next, attach a listener which will be called when Decthings sends us events
client.terminal.on('exit', ({ terminalSessionId, reason }) => {
// This function will run when an event is received
console.log(terminalSessionId)
console.log(reason)
})
}
main()