Skip to main content

Hub client

The hub client is the part of every node that connects it to a hub server, the control plane that lets connected nodes find and manage each other. Use this service to sign the node hosting the API in to a hub server, disconnect it, follow its connection state, and read or change the details of nodes.

Get local node id​

Get the ID of the node hosting the API, and whether it is authenticated with a hub server.

GET/v1/hub-client/local-node-id
Example
import { NanopingClient } from "@nanoping/api";

const client = new NanopingClient({ baseUrl: "http://127.0.0.1:10565" });

const response = await client.hubClient.getLocalNodeId();
Response
  • nodeIdstring

    Id of the node hosting the API.

  • isAuthenticatedbool

    True if the node holds a sign-in to a hub server. It may still be disconnected from it at the moment.

Get node​

Get the details of a node. Fails with Unavailable when the node cannot be reached.

GET/v1/nodes/{node_id}
Path parameters
  • node_idstring

    Id of the node to get information about, for example the id returned by getLocalNodeId.

Example
import { NanopingClient } from "@nanoping/api";

const client = new NanopingClient({ baseUrl: "http://127.0.0.1:10565" });

const response = await client.hubClient.getNode({
nodeId: "3f2b1c9e-8d4a-4f6b-9c2e-1a7d5e8b0c4f"
});
Response
  • nodeNode

    Information about the node.

Set node​

Change the details of a node, replacing the current values. Fails with PermissionDenied when the node's configuration comes from a config file.

PUT/v1/nodes/{node_id}
Path parameters
  • node_idstring

    Id of the node to change.

Request body
  • namestring

    New name of the node. Example: edge-node-1

  • metadataNodeMetadata

    New metadata of the node. Replaces all existing metadata, so leaving it empty removes the metadata.

Example
import { NanopingClient } from "@nanoping/api";

const client = new NanopingClient({ baseUrl: "http://127.0.0.1:10565" });

const response = await client.hubClient.setNode({
nodeId: "3f2b1c9e-8d4a-4f6b-9c2e-1a7d5e8b0c4f",
name: "edge-node-1",
metadata: {
items: {
key: {
bytesValue: ""
}
}
}
});
Response
  • nodeNode

    The node information after the change.

Authenticate by request​

Ask a hub server to authenticate the node hosting the API, and wait for the hub server to accept or reject the request. On success the node stays authenticated, also after a restart. Fails with AlreadyExists if the node is already authenticated, Unauthenticated if the request was rejected, DeadlineExceeded if the hub server did not answer in time, Unavailable if the hub server could not be reached, and NotFound if the hub server does not accept authentication by request.

Joining a hub takes two sides. Call this on the node that should join. The call waits while the request is shown on the hub server, where it is approved or rejected: in the dashboard, with np authentication-requests, or with authentication by requests. When the call returns without an error, the node is part of the hub.

POST/v1/hub-client/authenticate-by-request
Request body
  • hubServerAddressstring

    HTTP or HTTPS address of the hub server. Example: http://127.0.0.1:8769

Example
import { ApiError, Code, NanopingClient, joinHub } from "@nanoping/api";

const client = new NanopingClient({ baseUrl: "http://127.0.0.1:10565" });

try {
// Waits until the request is approved or rejected on the hub server.
await joinHub(client, "http://127.0.0.1:8769");
} catch (error) {
if (error instanceof ApiError && error.code === Code.Unauthenticated) {
throw new Error("The hub server rejected the node");
}
throw error;
}

// The node is part of the hub now, and stays so after a restart.
Response

No fields.

Disconnect​

Disconnect the node hosting the API from its hub server. The node always leaves, even if the hub server could not remove it from its list of nodes.

POST/v1/hub-client/disconnect
Request body

No fields.

Example
import { NanopingClient } from "@nanoping/api";

const client = new NanopingClient({ baseUrl: "http://127.0.0.1:10565" });

await client.hubClient.disconnect();
Response

No fields.

Stream connection state​

Stream the connection state between the node hosting the API and its hub server. The current state is sent first, then a new message every time the state changes. The stream stays open until the caller closes it.

GET/v1/streams/hub-client/connection-state

Server-sent events. A : ready comment is sent when the stream is ready, then each message is a data: line holding the response below. A failure after that is an error event.

Example
import { NanopingClient } from "@nanoping/api";

const client = new NanopingClient({ baseUrl: "http://127.0.0.1:10565" });

for await (const response of client.hubClient.streamConnectionState()) {
}
Response messages
  • stateConnectionState

    The current connection state.