Skip to main content

Stack

Stack is facade that provides provides IPFS and Relay communications for components such as Storage, PubSub and etc...

Create

import { Stack } from '@dstack-js/lib';

const stack = await Stack.create({
  namespace: 'namespace',
});

console.log('My Peer ID is:', stack.id);

Identity

Providing private key

import { Stack } from '@dstack-js/lib';

const privateKey = '...';

const stack = await Stack.create({
  namespace: 'namespace',
  privateKey,
});

console.log('My Peer ID is:', stack.id);

You can generate a private key using peer-id module.

Peer object

{
  id: "peerId",
  address: "/some/address"
}

Get my identity

const peer = await stack.id();
console.log(peer.id, peer.address);

Connectivity

No need to connect peers manually, DStack will handle it by itself

if you want to ensure that some peers is connected

Connect to peer manually

await stack.connect('/some/addr');

Get connected Peers

const peers = await stack.peers();

Events

onPeerConnect

stack.onPeerConnect((peer) => console.log(peer.id, peer.address));

onPeerDisconnected

stack.onPeerDisconnected((peer) => console.log(peer.id, peer.address));

Ping

Get round trip time to the connected peer

const [peer] = await stack.peers();
const ms = await stack.ping(peer);
console.log(ms, 'ms'); // Example output: `8.1 ms`

Specify timeout:

const [peer] = await stack.peers();
// timeout in ms
const ms = await stack.ping(peer, { timeout: 10000 });
console.log(ms, 'ms');

Specify cycles count:

const [peer] = await stack.peers();
const ms = await stack.ping(peer, { count: 1 });
console.log(ms, 'ms');

Gracefully stop

await stack.stop();