Web client
There are two means of accessing the tdx Volt from web applications, via the HTTP interface or using the websocket API.
The websocket API is recommended because it maps well to the grpc streaming modes.
HTTP interface
The HTTP interface is a work in progress. The tdx Volt API is accessible using pure REST requests, but a javascript library that packages this up is not fully implemented yet.
Websocket API
The websocket API is currently more polished than the HTTP interface.
Begin by installing the @tdxvolt/volt-client-web
package:
Configuration
In order to be able to initialise a connection to a Volt, you will need to obtain a client configuration.
How your web application obtains or stores the client configuration is not discussed here, needless to say you need to be careful not to expose keys or other sensitive data. For the purposes of this document, we assume the configuration is obtained via some out-of-band means and stored in the browser `localStorage`. It holds the key in encrypted form. The web application will then prompt the user to enter the key passphrase in order to unlock it.
Example client connection object, obtained from the fusebox identity dialog:
Initialisation
Create a tdx Volt instance to enable Bob to connect to the Volt:
Now attempt to initialise the tdx Volt connection:
If initialisation fails, an error will be thrown.
Once initialised successfully, you can issue any API call.
Refer to the API documentation for details of the method names and corresponding request and response messages. Each API method expects a JSON object representing the request(s) as input, and returns JSON objects as indicated by the response message type. The JSON representation of the protobuf message format is intuitive.
Unary calls
All unary calls return a promise that will either resolve with a JSON response object matching that defined by the API protobuf, or reject if there is a problem with the grpc transport or an error status on the response object.
In the example below, we retrieve the resource associated with Bob’s home folder:
Streaming calls
The promise model does not fit well with streaming calls since they are long-lived.
For this reason, all streaming calls (client streaming, server streaming, and bi-directional streaming) return a call
object that is used to manage the call.
The call
object emits events as interactions with the underlying websocket occur, and there are 3 events of interest:
- “error” emitted when an error occurs on the call
- “data” emitted when a response is received from the Volt
- “end” emitted when the call ends
An example of a streaming call is shown below, where we subscribe to the @wire
wire: