Communication between two nodes using asymmetric encryption
import { SwarmClient } from'@erebos/swarm-node'const run = async () => {
// Create PSS clients over WebSocketconst alice = new SwarmClient({ pss: 'ws://localhost:8501' })
const bob = new SwarmClient({ pss: 'ws://localhost:8502' })
// Retrieve Alice's public key and create the topicconst [key, topic] = awaitPromise.all([
alice.pss.getPublicKey(),
alice.pss.stringToTopic('PSS rocks'),
])
// Make Alice subscribe to the created topic and Bob add her public keyconst [subscription] = awaitPromise.all([
alice.pss.subscribeTopic(topic),
bob.pss.setPeerPublicKey(key, topic),
])
// Actually subscribe to the messages stream
alice.pss.createSubscription(subscription).subscribe(payload => {
console.log(
`received message from ${payload.key}: ${payload.msg.toString()}`,
)
})
// Send message to Alice
bob.pss.sendAsym(key, topic, 'hello world')
}
run().catch(console.error)
Communication between two nodes without encryption
import { SwarmClient } from'@erebos/swarm-node'const run = async () => {
// Create PSS clients over WebSocketconst alice = new SwarmClient({ pss: 'ws://localhost:8501' })
const bob = new SwarmClient({ pss: 'ws://localhost:8502' })
// Retrieve Alice's node address and create the topicconst [address, topic] = awaitPromise.all([
alice.pss.baseAddr(),
alice.pss.stringToTopic('PSS rocks'),
])
// Make Alice subscribe to the created topicconst subscription = await alice.pss.subscribeTopic(topic, true)
// Actually subscribe to the messages stream
alice.pss.createSubscription(subscription).subscribe(payload => {
console.log(`received message: ${payload.msg.toString()}`)
})
// Send message to Alice
bob.pss.sendRaw(address, topic, 'hello world')
}
run().catch(console.error)