Create a new daemon client, using a unix socket.
Name
Type
Description
socketPath
String
unix socket path
Client instance
const Client = require ( 'libp2p-daemon-client' )
const defaultSock = '/tmp/p2pd.sock'
const client = new Client ( defaultSock )
// client.{}
Closes the socket.
Type
Description
Promise
Promise resolves when socket is closed
const Client = require ( 'libp2p-daemon-client' )
const defaultSock = '/tmp/p2pd.sock'
const client = new Client ( defaultSock )
// close the socket
await client . close ( )
Requests a connection to a known peer on a given set of addresses.
client.connect(peerId, addrs)
Name
Type
Description
peerId
PeerId
peer ID to connect
options
Object
set of addresses to connect
const client = new Client ( defaultSock )
try {
await client . connect ( peerId , addrs )
} catch ( err ) {
//
}
Query the daemon for its peer ID and listen addresses.
Type
Description
Object
Identify response
Object.peerId
Peer id of the daemon
Object.addrs
Addresses of the daemon
const client = new Client ( defaultSock )
let identify
try {
identify = await client . identify ( )
} catch ( err ) {
//
}
Get a list of IDs of peers the node is connected to.
Type
Description
Array
array of peer id's
Array.<PeerId>
Peer id of a node
const client = new Client ( defaultSock )
let identify
try {
identify = await client . identify ( )
} catch ( err ) {
//
}
Initiate an outbound stream to a peer on one of a set of protocols.
client.openStream(peerId, protocol)
Name
Type
Description
peerId
PeerId
peer ID to connect
protocol
string
protocol to use
Type
Description
Socket
socket to write data
const protocol = '/protocol/1.0.0'
const client = new Client ( defaultSock )
let socket
try {
socket = await client . openStream ( peerId , protocol )
} catch ( err ) {
//
}
socket . write ( uint8ArrayFromString ( 'data' ) )
Register a handler for inbound streams on a given protocol.
client.registerStreamHandler(path, protocol)
Name
Type
Description
path
string
socket path
protocol
string
protocol to use
const protocol = '/protocol/1.0.0'
const client = new Client ( defaultSock )
await client . registerStreamHandler ( path , protocol )
Write a value to a key in the DHT.
client.dht.put(key, value)
Name
Type
Description
key
Uint8Array
key to add to the dht
value
Uint8Array
value to add to the dht
const client = new Client ( defaultSock )
const key = '/key'
const value = uint8ArrayFromString ( 'oh hello there' )
try {
await client . dht . put ( key , value )
} catch ( err ) {
//
}
Query the DHT for a value stored through a key in the DHT.
Name
Type
Description
key
Uint8Array
key to get from the dht
Type
Description
Uint8Array
Value obtained from the DHT
const client = new Client ( defaultSock )
const key = '/key'
let value
try {
value = await client . dht . get ( key , value )
} catch ( err ) {
//
}
Query the DHT for a given peer's known addresses.
client.dht.findPeer(peerId)
Name
Type
Description
peerId
PeerId
ID of the peer to find
Type
Description
PeerInfo
Peer info of a known peer
const client = new Client ( defaultSock )
let peerInfo
try {
peerInfo = await client . dht . findPeer ( peerId )
} catch ( err ) {
//
}
Announce that have data addressed by a given CID.
Name
Type
Description
cid
CID
cid to provide
const client = new Client ( defaultSock )
try {
await client . dht . provide ( cid )
} catch ( err ) {
//
}
Query the DHT for peers that have a piece of content, identified by a CID.
client.dht.findProviders(cid, [count])
Name
Type
Description
cid
CID
cid to find
count
number
number or results aimed
Type
Description
Array
array of peer info
Array.<PeerInfo>
Peer info of a node
const client = new Client ( defaultSock )
let peerInfos
try {
peerInfos = await client . dht . findProviders ( cid )
} catch ( err ) {
//
}
Query the DHT routing table for peers that are closest to a provided key.
client.dht.getClosestPeers(key)
Name
Type
Description
key
Uint8Array
key to get from the dht
Type
Description
Array
array of peer info
Array.<PeerInfo>
Peer info of a node
const client = new Client ( defaultSock )
let peerInfos
try {
peerInfos = await client . dht . getClosestPeers ( key )
} catch ( err ) {
//
}
Query the DHT routing table for a given peer's public key.
client.dht.getPublicKey(peerId)
Name
Type
Description
peerId
PeerId
ID of the peer to find
Type
Description
PublicKey
public key of the peer
const client = new Client ( defaultSock )
let publicKey
try {
publicKey = await client . dht . getPublicKey ( peerId )
} catch ( err ) {
//
}
client.pubsub.getTopics()
Type
Description
Array<String>
topics the node is subscribed to
const client = new Client ( defaultSock )
let topics
try {
topics = await client . pubsub . getTopics ( )
} catch ( err ) {
//
}
Name
Type
Description
topic
string
topic to publish
data
Uint8Array
data to publish
Type
Description
Promise
publish success
const topic = 'topic'
const data = uint8ArrayFromString ( 'data' )
const client = new Client ( defaultSock )
try {
await client . pubsub . publish ( topic , data )
} catch ( err ) {
//
}
client.pubsub.subscribe()
Name
Type
Description
topic
string
topic to subscribe
Type
Description
AsyncIterator
data published
const topic = 'topic'
const client = new Client ( defaultSock )
for await ( const msg of client . pubsub . subscribe ( topic ) ) {
// msg.data - pubsub data received
}