Networking Packages
These are packages supporting both the inter-node networking and the Xnode overlay network.
Package p2p
This is the library implemented P2P networking based on libp2p.
Instance Initialisation
// Construct and start a p2p instance
ins, err := p2p.NewInstance(cancelContext).SetP2PHost(existingHost).Build()
err = ins.Start()
Gossip Publish-and-Subscribe (pub-sub)
// Join the specific topic
err := ins.Join("topic")
// Publisher sample usage
err = ins.Publish("topic", []byte("message"))
// Subscriber sample usage
msg, err := s.Subscribe(topic)
go func() {
for {
select {
case <-cancelContext.Done():
return
case m := <-msg:
// Handle message......
}
}
}()
DHT (Distributed Hash Table)
// Add a key to DHT
err := ins.DHT.PutValue(timeoutCtx, "key", []byte("value"))
// Get a value from DHT
value, err := ins.DHT.GetValue(timeoutCtx, "key")