Documentation / @hybrid-compute/remote / RemoteCompute
Class: RemoteCompute
Defined in: packages/remote/src/index.ts:205
RemoteCompute is a backend that sends tasks to a remote service, over HTTP with fetch or over a WebSocket.
Each task is one JSON request and one JSON reply; see RemoteTaskRequestInterface and RemoteTaskResponseInterface for the message format. Every failure (an error in the reply, an unusable response, a timeout, an unreachable server) rejects with a RemoteTaskError.
The WebSocket transport connects on the first task, not when it's created, reuses the connection, and connects again on the next task after it closes. Call RemoteCompute.close when you're done with it.
Examples
const remote = createRemoteCompute({
transport: 'fetch',
endpoint: 'https://api.example.com/compute',
canRunTasks: ['translateText']
});
const result = await remote.runTask('translateText', { text: 'hello' });const remote = createRemoteCompute({
transport: 'websocket',
endpoint: 'wss://api.example.com/ws',
canRunTasks: ['analyzeSentiment']
});
const result = await remote.runTask('analyzeSentiment', { text: 'It works!' });
remote.close();See
- https://developer.mozilla.org/en-US/docs/Web/API/fetch
- https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
Implements
Constructors
Constructor
new RemoteCompute(options: RemoteComputeOptionsInterface): RemoteCompute;Defined in: packages/remote/src/index.ts:224
Creates the backend. It checks the options, and does no network I/O until the first task.
Parameters
options
The transport, endpoint, allowed tasks and, optionally, a timeout and fetch headers.
Returns
RemoteCompute
Throws
For a missing or wrongly typed option.
Throws
For an unknown transport, an endpoint with the wrong URL scheme, or a negative timeout.
Methods
canRun()
canRun(taskName: string): boolean;Defined in: packages/remote/src/index.ts:235
Determines if this backend may run the given task.
Parameters
taskName
string
Name of the task.
Returns
boolean
true if the task is in canRunTasks, or if canRunTasks is '*'.
Implementation of
ComputeBackendInterface.canRun
close()
close(): void;Defined in: packages/remote/src/index.ts:276
Closes the WebSocket, if one is open or opening. Tasks still waiting for a reply reject with a RemoteTaskError. The next task opens a new connection. Does nothing for the fetch transport.
Returns
void
runTask()
runTask<Input, Output>(
taskName: string,
input: Input,
options?: RunTaskOptionsInterface): Promise<Output>;Defined in: packages/remote/src/index.ts:255
Sends a task to the remote service and waits for its reply.
Type Parameters
Input
Input
The input data structure expected by the task.
Output
Output
The output structure returned by the task.
Parameters
taskName
string
Name of the remote task.
input
Input
Input data to send. It must be serialisable as JSON.
options?
Options for this call, such as an AbortSignal.
Returns
Promise<Output>
A Promise resolving to the reply's result. It rejects with a RemoteTaskError if the task fails, the reply is unusable, the timeout passes or the server can't be reached, and with the signal's reason if the call is cancelled.