Establish a connect stream

api

The VoltAPI.Connect API enables clients to establish a Volt connect stream. Note that clients will not need to call this API directly as it is encapsulated in the various ‘Connection’ classes in the client libraries.

cli

The CLI automatically uses connect streams to implement certain commands, in particular the wire publish and subscribe commands. This means that when you subscribe to a wire using the CLI, the subscription will automatically be restarted if the connection to the Volt is lost and subsequently re-established.

fusebox

The fusebox always establishes a connect stream to the target Volt, and this is used to reflect the connection status to the user and to receive notification of changes to resources that are in view.

javascript [grpc]

Use the VoltClient.connect method to establish a Volt connect stream, and register to receive connected events for notifications.

import grpc from "@grpc/grpc-js";
import { VoltClient } from "@tdxvolt/volt-client-grpc";
const client = new VoltClient(grpc);
const config = "./volt.config.json";
// Register to receive connection events.
client.on("connected", (connected) => {
if (connected) {
console.log("connected");
} else {
console.log("disconnected");
}
});
// Register to receive error events.
client.on("error", (err) => {
console.log("error: %s", err.message);
});
client
.initialise("connect-example", config)
.then(() => {
return client.connect();
})
.then(() => {
console.log("client waiting for events");
})
.catch((err) => {
console.log("failure in client [%s]", err.message);
process.exit(1);
});

Javascript [web]

This is a work in progress.

import { VoltClient } from "@tdxvolt/volt-client-web";
import { config } from "./client-configuration.js";
let client;
try {
client = new VoltClient(WebSocket, config);
await client.initialise();
console.log("client initialised");
const connectStream = client.connect(
{
hello: {},
},
(err, resp) => {
if (err) {
console.log("received error on connect: " + err.message);
} else {
console.log(JSON.stringify(resp));
}
}
);
await connectStream.responsePromise;
} catch (err) {
console.log("failure in connect example [%s]", err.message);
} finally {
console.log("finished");
client.close();
}

C++

#include <volt_client/volt_api_client.h>
...
tdx::volt_client::VoltConnectCallbacks voltCallbacks;
voltCallbacks.onConnection = [](bool connected) {
// Perform connect/disconnect logic here.
std::cout << "connected: " << connected << std::endl;
};
// Create and initialise the Volt client connection with the configuration
// details and the key passphrase.
auto volt = std::make_unique<tdx::volt_client::VoltConnection>();
if ((result = volt->initialise(config, passphrase)) != tdx::error_code::ok) {
std::cout << "failure initialising connection: " << result << std::endl;
return result;
}
// Wait for the Volt connection to initialise successfully (this
// may fail if the Volt is offline or unreachable).
result = tdx::error_code::notInitialised;
while (!result.empty()) {
if ((result = volt->connect(voltCallbacks)) != tdx::error_code::ok) {
std::cout << "failure connecting to Volt: " << result << std::endl;
std::cout << "retrying in 10 seconds......" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(10000));
}
}
// Once the initial connection is successfully established, the `onConnection`
// callback will be called, including for subsequent disconnection/connection
// events.