2017-07-20 19:42:32 -07:00
# Publish Subscribe
2020-05-27 14:31:36 -05:00
Publish Subscribe is also included on the stack. Currently, we have two PubSub implementation available [libp2p-floodsub ](https://github.com/libp2p/js-libp2p-floodsub ) and [libp2p-gossipsub ](https://github.com/ChainSafe/js-libp2p-gossipsub ), with many more being researched at [research-pubsub ](https://github.com/libp2p/research-pubsub ).
2017-07-20 19:42:32 -07:00
We've seen many interesting use cases appear with this, here are some highlights:
- [Collaborative Text Editing ](https://www.youtube.com/watch?v=-kdx8rJd8rQ )
- [IPFS PubSub (using libp2p-floodsub) for IoT ](https://www.youtube.com/watch?v=qLpM5pBDGiE ).
- [Real Time distributed Applications ](https://www.youtube.com/watch?v=vQrbxyDPSXg )
2021-01-20 10:34:47 +01:00
## 0. Set up the example
Before moving into the examples, you should run `npm install` on the top level `js-libp2p` folder, in order to install all the dependencies needed for this example. In addition, you will need to install the example related dependencies by doing `cd examples && npm install` . Once the install finishes, you should move into the example folder with `cd pubsub` .
2017-07-20 19:42:32 -07:00
## 1. Setting up a simple PubSub network on top of libp2p
2018-02-15 19:49:41 +01:00
For this example, we will use MulticastDNS for automatic Peer Discovery. This example is based the previous examples found in [Discovery Mechanisms ](../discovery-mechanisms ). You can find the complete version at [1.js ](./1.js ).
2017-07-20 19:42:32 -07:00
2019-08-19 17:06:08 +02:00
Using PubSub is super simple, you only need to provide the implementation of your choice and you are ready to go. No need for extra configuration.
2017-07-20 19:42:32 -07:00
2019-12-16 22:28:35 +00:00
First, let's update our libp2p configuration with a pubsub implementation.
```JavaScript
feat: convert to typescript (#1172)
Converts this module to typescript.
- Ecosystem modules renamed from (e.g.) `libp2p-tcp` to `@libp2p/tcp`
- Ecosystem module now have named exports
- Configuration has been updated, now pass instances of modules instead of classes:
- Some configuration keys have been renamed to make them more descriptive. `transport` -> `transports`, `connEncryption` -> `connectionEncryption`. In general where we pass multiple things, the key is now plural, e.g. `streamMuxer` -> `streamMuxers`, `contentRouting` -> `contentRouters`, etc. Where we are configuring a singleton the config key is singular, e.g. `connProtector` -> `connectionProtector` etc.
- Properties of the `modules` config key have been moved to the root
- Properties of the `config` config key have been moved to the root
```js
// before
import Libp2p from 'libp2p'
import TCP from 'libp2p-tcp'
await Libp2p.create({
modules: {
transport: [
TCP
],
}
config: {
transport: {
[TCP.tag]: {
foo: 'bar'
}
},
relay: {
enabled: true,
hop: {
enabled: true,
active: true
}
}
}
})
```
```js
// after
import { createLibp2p } from 'libp2p'
import { TCP } from '@libp2p/tcp'
await createLibp2p({
transports: [
new TCP({ foo: 'bar' })
],
relay: {
enabled: true,
hop: {
enabled: true,
active: true
}
}
})
```
- Use of `enabled` flag has been reduced - previously you could pass a module but disable it with config. Now if you don't want a feature, just don't pass an implementation. Eg:
```js
// before
await Libp2p.create({
modules: {
transport: [
TCP
],
pubsub: Gossipsub
},
config: {
pubsub: {
enabled: false
}
}
})
```
```js
// after
await createLibp2p({
transports: [
new TCP()
]
})
```
- `.multiaddrs` renamed to `.getMultiaddrs()` because it's not a property accessor, work is done by that method to calculate announce addresses, observed addresses, etc
- `/p2p/${peerId}` is now appended to all addresses returned by `.getMultiaddrs()` so they can be used opaquely (every consumer has to append the peer ID to the address to actually use it otherwise). If you need low-level unadulterated addresses, call methods on the address manager.
BREAKING CHANGE: types are no longer hand crafted, this module is now ESM only
2022-03-28 14:30:27 +01:00
import { createLibp2p } from 'libp2p'
import { Gossipsub } from 'libp2p-gossipsub'
2019-12-16 22:28:35 +00:00
feat: convert to typescript (#1172)
Converts this module to typescript.
- Ecosystem modules renamed from (e.g.) `libp2p-tcp` to `@libp2p/tcp`
- Ecosystem module now have named exports
- Configuration has been updated, now pass instances of modules instead of classes:
- Some configuration keys have been renamed to make them more descriptive. `transport` -> `transports`, `connEncryption` -> `connectionEncryption`. In general where we pass multiple things, the key is now plural, e.g. `streamMuxer` -> `streamMuxers`, `contentRouting` -> `contentRouters`, etc. Where we are configuring a singleton the config key is singular, e.g. `connProtector` -> `connectionProtector` etc.
- Properties of the `modules` config key have been moved to the root
- Properties of the `config` config key have been moved to the root
```js
// before
import Libp2p from 'libp2p'
import TCP from 'libp2p-tcp'
await Libp2p.create({
modules: {
transport: [
TCP
],
}
config: {
transport: {
[TCP.tag]: {
foo: 'bar'
}
},
relay: {
enabled: true,
hop: {
enabled: true,
active: true
}
}
}
})
```
```js
// after
import { createLibp2p } from 'libp2p'
import { TCP } from '@libp2p/tcp'
await createLibp2p({
transports: [
new TCP({ foo: 'bar' })
],
relay: {
enabled: true,
hop: {
enabled: true,
active: true
}
}
})
```
- Use of `enabled` flag has been reduced - previously you could pass a module but disable it with config. Now if you don't want a feature, just don't pass an implementation. Eg:
```js
// before
await Libp2p.create({
modules: {
transport: [
TCP
],
pubsub: Gossipsub
},
config: {
pubsub: {
enabled: false
}
}
})
```
```js
// after
await createLibp2p({
transports: [
new TCP()
]
})
```
- `.multiaddrs` renamed to `.getMultiaddrs()` because it's not a property accessor, work is done by that method to calculate announce addresses, observed addresses, etc
- `/p2p/${peerId}` is now appended to all addresses returned by `.getMultiaddrs()` so they can be used opaquely (every consumer has to append the peer ID to the address to actually use it otherwise). If you need low-level unadulterated addresses, call methods on the address manager.
BREAKING CHANGE: types are no longer hand crafted, this module is now ESM only
2022-03-28 14:30:27 +01:00
const node = await createLibp2p({
2020-05-15 15:37:47 +02:00
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0']
},
feat: convert to typescript (#1172)
Converts this module to typescript.
- Ecosystem modules renamed from (e.g.) `libp2p-tcp` to `@libp2p/tcp`
- Ecosystem module now have named exports
- Configuration has been updated, now pass instances of modules instead of classes:
- Some configuration keys have been renamed to make them more descriptive. `transport` -> `transports`, `connEncryption` -> `connectionEncryption`. In general where we pass multiple things, the key is now plural, e.g. `streamMuxer` -> `streamMuxers`, `contentRouting` -> `contentRouters`, etc. Where we are configuring a singleton the config key is singular, e.g. `connProtector` -> `connectionProtector` etc.
- Properties of the `modules` config key have been moved to the root
- Properties of the `config` config key have been moved to the root
```js
// before
import Libp2p from 'libp2p'
import TCP from 'libp2p-tcp'
await Libp2p.create({
modules: {
transport: [
TCP
],
}
config: {
transport: {
[TCP.tag]: {
foo: 'bar'
}
},
relay: {
enabled: true,
hop: {
enabled: true,
active: true
}
}
}
})
```
```js
// after
import { createLibp2p } from 'libp2p'
import { TCP } from '@libp2p/tcp'
await createLibp2p({
transports: [
new TCP({ foo: 'bar' })
],
relay: {
enabled: true,
hop: {
enabled: true,
active: true
}
}
})
```
- Use of `enabled` flag has been reduced - previously you could pass a module but disable it with config. Now if you don't want a feature, just don't pass an implementation. Eg:
```js
// before
await Libp2p.create({
modules: {
transport: [
TCP
],
pubsub: Gossipsub
},
config: {
pubsub: {
enabled: false
}
}
})
```
```js
// after
await createLibp2p({
transports: [
new TCP()
]
})
```
- `.multiaddrs` renamed to `.getMultiaddrs()` because it's not a property accessor, work is done by that method to calculate announce addresses, observed addresses, etc
- `/p2p/${peerId}` is now appended to all addresses returned by `.getMultiaddrs()` so they can be used opaquely (every consumer has to append the peer ID to the address to actually use it otherwise). If you need low-level unadulterated addresses, call methods on the address manager.
BREAKING CHANGE: types are no longer hand crafted, this module is now ESM only
2022-03-28 14:30:27 +01:00
transports: [
new TCP()
],
streamMuxers: [
new Mplex()
],
connectionEncryption: [
new Noise()
],
// we add the Pubsub module we want
pubsub: new Gossipsub()
2019-12-16 22:28:35 +00:00
})
```
Once that is done, we only need to create a few libp2p nodes, connect them and everything is ready to start using pubsub.
2017-07-20 19:42:32 -07:00
```JavaScript
feat: convert to typescript (#1172)
Converts this module to typescript.
- Ecosystem modules renamed from (e.g.) `libp2p-tcp` to `@libp2p/tcp`
- Ecosystem module now have named exports
- Configuration has been updated, now pass instances of modules instead of classes:
- Some configuration keys have been renamed to make them more descriptive. `transport` -> `transports`, `connEncryption` -> `connectionEncryption`. In general where we pass multiple things, the key is now plural, e.g. `streamMuxer` -> `streamMuxers`, `contentRouting` -> `contentRouters`, etc. Where we are configuring a singleton the config key is singular, e.g. `connProtector` -> `connectionProtector` etc.
- Properties of the `modules` config key have been moved to the root
- Properties of the `config` config key have been moved to the root
```js
// before
import Libp2p from 'libp2p'
import TCP from 'libp2p-tcp'
await Libp2p.create({
modules: {
transport: [
TCP
],
}
config: {
transport: {
[TCP.tag]: {
foo: 'bar'
}
},
relay: {
enabled: true,
hop: {
enabled: true,
active: true
}
}
}
})
```
```js
// after
import { createLibp2p } from 'libp2p'
import { TCP } from '@libp2p/tcp'
await createLibp2p({
transports: [
new TCP({ foo: 'bar' })
],
relay: {
enabled: true,
hop: {
enabled: true,
active: true
}
}
})
```
- Use of `enabled` flag has been reduced - previously you could pass a module but disable it with config. Now if you don't want a feature, just don't pass an implementation. Eg:
```js
// before
await Libp2p.create({
modules: {
transport: [
TCP
],
pubsub: Gossipsub
},
config: {
pubsub: {
enabled: false
}
}
})
```
```js
// after
await createLibp2p({
transports: [
new TCP()
]
})
```
- `.multiaddrs` renamed to `.getMultiaddrs()` because it's not a property accessor, work is done by that method to calculate announce addresses, observed addresses, etc
- `/p2p/${peerId}` is now appended to all addresses returned by `.getMultiaddrs()` so they can be used opaquely (every consumer has to append the peer ID to the address to actually use it otherwise). If you need low-level unadulterated addresses, call methods on the address manager.
BREAKING CHANGE: types are no longer hand crafted, this module is now ESM only
2022-03-28 14:30:27 +01:00
const { fromString } from 'uint8arrays/from-string')
const { toString } from 'uint8arrays/to-string')
2019-12-16 22:28:35 +00:00
const topic = 'news'
const node1 = nodes[0]
const node2 = nodes[1]
2020-05-15 15:37:47 +02:00
// Add node's 2 data to the PeerStore
2022-01-20 12:03:35 +00:00
await node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs)
2020-05-15 15:37:47 +02:00
await node1.dial(node2.peerId)
2019-12-16 22:28:35 +00:00
2020-08-25 16:48:04 +02:00
node1.pubsub.on(topic, (msg) => {
2022-01-14 16:33:17 +01:00
console.log(`node1 received: ${toString(msg.data)}` )
2017-07-20 19:42:32 -07:00
})
2020-08-25 16:48:04 +02:00
await node1.pubsub.subscribe(topic)
2019-12-16 22:28:35 +00:00
2020-12-04 14:53:05 +01:00
// Will not receive own published messages by default
2020-08-25 16:48:04 +02:00
node2.pubsub.on(topic, (msg) => {
2022-01-14 16:33:17 +01:00
console.log(`node2 received: ${toString(msg.data)}` )
2019-12-16 22:28:35 +00:00
})
2020-08-25 16:48:04 +02:00
await node2.pubsub.subscribe(topic)
2019-12-16 22:28:35 +00:00
// node2 publishes "news" every second
setInterval(() => {
2022-04-22 20:49:35 +01:00
node2.pubsub.publish(topic, fromString('Bird bird bird, bird is the word!')).catch(err => {
console.error(err)
})
2019-12-16 22:28:35 +00:00
}, 1000)
2017-07-20 19:42:32 -07:00
```
The output of the program should look like:
```
> node 1.js
2019-04-11 15:52:04 +02:00
connected to QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82
2019-08-19 17:06:08 +02:00
node1 received: Bird bird bird, bird is the word!
node1 received: Bird bird bird, bird is the word!
```
2020-12-04 14:53:05 +01:00
You can change the pubsub `emitSelf` option if you want the publishing node to receive its own messages.
2019-08-19 17:06:08 +02:00
```JavaScript
const defaults = {
config: {
pubsub: {
enabled: true,
2020-12-04 14:53:05 +01:00
emitSelf: true
2019-08-19 17:06:08 +02:00
}
}
}
2017-07-20 19:42:32 -07:00
```
2020-12-04 14:53:05 +01:00
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!
```
2017-07-20 19:42:32 -07:00
## 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:
- [PubSub Room ](https://github.com/ipfs-labs/ipfs-pubsub-room )
- [Live DB - A always in Sync DB using CRDT ](https://github.com/ipfs-labs/ipfs-live-db )
- [IIIF Annotations over IPFS, CRDT and libp2p ](https://www.youtube.com/watch?v=hmAniA6g9D0&feature=youtu.be&t=10m40s )
- [orbit.chat - p2p chat application, fully running in the browser with js-ipfs, js-libp2p and orbit-db ](http://orbit.chat/ )