mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-25 10:32:14 +00:00
chore: merge 0.30
This commit is contained in:
commit
2e7f49ebbd
@ -445,13 +445,13 @@ const node = await Libp2p.create({
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [SECIO]
|
||||
connEncryption: [NOISE]
|
||||
},
|
||||
config: {
|
||||
relay: { // Circuit Relay options (this config is part of libp2p core configurations)
|
||||
|
@ -43,6 +43,7 @@ const createNode = async () => {
|
||||
})
|
||||
await node1.pubsub.subscribe(topic)
|
||||
|
||||
// Will not receive own published messages by default
|
||||
node2.pubsub.on(topic, (msg) => {
|
||||
console.log(`node2 received: ${uint8ArrayToString(msg.data)}`)
|
||||
})
|
||||
|
@ -44,7 +44,6 @@ const node2 = nodes[1]
|
||||
|
||||
// Add node's 2 data to the PeerStore
|
||||
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs)
|
||||
|
||||
await node1.dial(node2.peerId)
|
||||
|
||||
node1.pubsub.on(topic, (msg) => {
|
||||
@ -52,6 +51,7 @@ node1.pubsub.on(topic, (msg) => {
|
||||
})
|
||||
await node1.pubsub.subscribe(topic)
|
||||
|
||||
// Will not receive own published messages by default
|
||||
node2.pubsub.on(topic, (msg) => {
|
||||
console.log(`node2 received: ${uint8ArrayToString(msg.data)}`)
|
||||
})
|
||||
@ -68,25 +68,34 @@ The output of the program should look like:
|
||||
```
|
||||
> node 1.js
|
||||
connected to QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82
|
||||
node2 received: Bird bird bird, bird is the word!
|
||||
node1 received: Bird bird bird, bird is the word!
|
||||
node2 received: Bird bird bird, bird is the word!
|
||||
node1 received: Bird bird bird, bird is the word!
|
||||
```
|
||||
|
||||
You can change the pubsub `emitSelf` option if you don't want the publishing node to receive its own messages.
|
||||
You can change the pubsub `emitSelf` option if you want the publishing node to receive its own messages.
|
||||
|
||||
```JavaScript
|
||||
const defaults = {
|
||||
config: {
|
||||
pubsub: {
|
||||
enabled: true,
|
||||
emitSelf: false
|
||||
emitSelf: true
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The output of the program should look like:
|
||||
|
||||
```
|
||||
> node 1.js
|
||||
connected to QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82
|
||||
node1 received: Bird bird bird, bird is the word!
|
||||
node2 received: Bird bird bird, bird is the word!
|
||||
node1 received: Bird bird bird, bird is the word!
|
||||
node2 received: Bird bird bird, bird is the word!
|
||||
```
|
||||
|
||||
## 2. Future work
|
||||
|
||||
libp2p/IPFS PubSub is enabling a whole set of Distributed Real Time applications using CRDT (Conflict-Free Replicated Data Types). It is still going through heavy research (and hacking) and we invite you to join the conversation at [research-CRDT](https://github.com/ipfs/research-CRDT). Here is a list of some of the exciting examples:
|
||||
|
@ -44,6 +44,7 @@ const createNode = async () => {
|
||||
|
||||
//subscribe
|
||||
node1.pubsub.on(topic, (msg) => {
|
||||
// Will not receive own published messages by default
|
||||
console.log(`node1 received: ${uint8ArrayToString(msg.data)}`)
|
||||
})
|
||||
await node1.pubsub.subscribe(topic)
|
||||
|
@ -97,15 +97,12 @@ Result
|
||||
```
|
||||
> node 1.js
|
||||
############## fruit banana ##############
|
||||
node1 received: banana
|
||||
node2 received: banana
|
||||
node3 received: banana
|
||||
############## fruit apple ##############
|
||||
node1 received: apple
|
||||
node2 received: apple
|
||||
node3 received: apple
|
||||
############## fruit car ##############
|
||||
node1 received: car
|
||||
############## fruit orange ##############
|
||||
node1 received: orange
|
||||
node2 received: orange
|
||||
|
@ -70,7 +70,7 @@
|
||||
"it-pipe": "^1.1.0",
|
||||
"it-protocol-buffers": "^0.2.0",
|
||||
"libp2p-crypto": "^0.18.0",
|
||||
"libp2p-interfaces": "libp2p/js-libp2p-interfaces#feat/add-types-with-post-install",
|
||||
"libp2p-interfaces": "^0.8.0",
|
||||
"libp2p-utils": "^0.2.2",
|
||||
"mafmt": "^8.0.0",
|
||||
"merge-options": "^2.0.0",
|
||||
|
@ -15,6 +15,9 @@ const { CircuitRelay: CircuitPB } = require('../protocol')
|
||||
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T
|
||||
*/
|
||||
class StreamHandler {
|
||||
/**
|
||||
* Create a stream handler for connection
|
||||
|
44
src/index.js
44
src/index.js
@ -80,11 +80,32 @@ const IDENTIFY_PROTOCOLS = IdentifyService.multicodecs
|
||||
* @property {Libp2pConfig} [config]
|
||||
* @property {PeerId} peerId
|
||||
*
|
||||
* @typedef {Object} CreateOptions
|
||||
* @property {PeerId} peerId
|
||||
*
|
||||
* @extends {EventEmitter}
|
||||
* @fires Libp2p#error Emitted when an error occurs
|
||||
* @fires Libp2p#peer:discovery Emitted when a peer is discovered
|
||||
*/
|
||||
class Libp2p extends EventEmitter {
|
||||
/**
|
||||
* Like `new Libp2p(options)` except it will create a `PeerId`
|
||||
* instance if one is not provided in options.
|
||||
*
|
||||
* @param {Libp2pOptions & CreateOptions} options - Libp2p configuration options
|
||||
* @returns {Promise<Libp2p>}
|
||||
*/
|
||||
static async create (options) {
|
||||
if (options.peerId) {
|
||||
return new Libp2p(options)
|
||||
}
|
||||
|
||||
const peerId = await PeerId.create()
|
||||
|
||||
options.peerId = peerId
|
||||
return new Libp2p(options)
|
||||
}
|
||||
|
||||
/**
|
||||
* Libp2p node.
|
||||
*
|
||||
@ -656,27 +677,4 @@ class Libp2p extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef {Object} CreateOptions
|
||||
* @property {PeerId} peerId
|
||||
*/
|
||||
|
||||
/**
|
||||
* Like `new Libp2p(options)` except it will create a `PeerId`
|
||||
* instance if one is not provided in options.
|
||||
*
|
||||
* @param {Libp2pOptions & CreateOptions} options - Libp2p configuration options
|
||||
* @returns {Promise<Libp2p>}
|
||||
*/
|
||||
Libp2p.create = async function create (options) {
|
||||
if (options.peerId) {
|
||||
return new Libp2p(options)
|
||||
}
|
||||
|
||||
const peerId = await PeerId.create()
|
||||
|
||||
options.peerId = peerId
|
||||
return new Libp2p(options)
|
||||
}
|
||||
|
||||
module.exports = Libp2p
|
||||
|
@ -16,6 +16,7 @@ const passthrough = data => data
|
||||
/**
|
||||
* @template Data, GetData, EventData
|
||||
*/
|
||||
|
||||
class Book {
|
||||
/**
|
||||
* The Book is the skeleton for the PeerStore books.
|
||||
|
@ -80,7 +80,7 @@ export interface CircuitMessageProto extends MessageProto<CircuitMessage> {
|
||||
STOP_DST_MULTIADDR_INVALID: STOP_DST_MULTIADDR_INVALID,
|
||||
STOP_RELAY_REFUSED: STOP_RELAY_REFUSED,
|
||||
MALFORMED_MESSAGE: MALFORMED_MESSAGE
|
||||
}
|
||||
},
|
||||
Type: {
|
||||
HOP: HOP,
|
||||
STOP: STOP,
|
||||
|
Loading…
x
Reference in New Issue
Block a user