Skip to content

Commit

Permalink
private example
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteff committed Oct 16, 2021
1 parent 5946531 commit 86c8baf
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 105 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pubsub.unsub('some topic') // no longer interested
pubsub.close() // cleanup
```

[example.js](https://github.com/fsteff/hyperpubsub/blob/main/example.js) is a minimalistic demonstrator that uses the hyperspace [simulator](https://github.com/hypercore-protocol/hyperspace#simulator) for local tests.
[basic.js](./examples/basic.js) is a minimalistic demonstrator that uses the hyperspace [simulator](https://github.com/hypercore-protocol/hyperspace#simulator) for tests.
If started twice you can see it exchanging "hello" messages.

## Private Messges
Expand All @@ -54,6 +54,9 @@ pubsub.subPrivateMsg(publicKey, secretKey, (msg) => {
pubsub2.pubPrivateMsg(publicKey, Buffer.from('hello', 'utf-8'))
```

[private.js](./examples/private.js) is a minimalistic demonstrator for private messages (uses the simulator as well).
Start it once, copy the public key that's printed to the cli and pass it as argument to a second instance.

## TODO
- [ ] PEX is WIP
- [ ] Hyperspace integration
Expand Down
2 changes: 1 addition & 1 deletion example.js → examples/basic.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {PubSub} = require('.').debug() // call debug() if you want debugging messages printed to the cli
const {PubSub} = require('..').debug() // call debug() if you want debugging messages printed to the cli
const simulator = require('hyperspace/simulator')

simulator().then(async ({client}) => {
Expand Down
34 changes: 34 additions & 0 deletions examples/private.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const {PubSub} = require('..')//.debug() // call debug() if you want debugging messages printed to the cli
const simulator = require('hyperspace/simulator')
const sodium = require('sodium-universal')
const os = require('os')

console.log('# HyperPubSub private message example')
console.log('# Usage: start this script on one device, copy the public key to the other and start the script on the other device with this key as argument')

simulator().then(async ({client}) => {
const pubsub = new PubSub(client.network, {application: 'example app', onError: console.error})

const publicKey = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES)
const secretKey = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES)
sodium.crypto_box_keypair(publicKey, secretKey)
console.log('Public Key is: ' + publicKey.toString('hex'))

pubsub.subPrivateMsg(publicKey, secretKey, (msgBuf, app, peer) => {
console.log('received "' + msgBuf.toString('utf-8') + '" from ' + peer.remoteAddress)
}, true)

if(process.argv.length > 2) {
const otherPubKey = Buffer.from(process.argv[2], 'hex')
const msg = Buffer.from('hello from ' + os.userInfo().username + ' @ ' + os.hostname, 'utf-8')
await pubsub.joinPublicKey(otherPubKey)

send()
setInterval(send, 1000)

function send() {
console.log('sending message: "' + msg.toString('utf-8')+'"')
pubsub.pubPrivateMsg(otherPubKey, msg)
}
}
})
Loading

0 comments on commit 86c8baf

Please sign in to comment.