mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-25 10:32:14 +00:00
chore: complement 0.29 migration for pubsub subscribe
This commit is contained in:
parent
fb4b2734d3
commit
7c8120caca
@ -90,6 +90,36 @@ libp2p.pubsub.on(topic, handler)
|
||||
libp2p.pubsub.subscribe(topic)
|
||||
```
|
||||
|
||||
In the latest releases, despite not being documented in `libp2p` the underlying pubsub routers supported subscribing to multiple topics at the same time. We removed that code complexity, since this is easily achieved in the application layer if needed.
|
||||
|
||||
**Before**
|
||||
|
||||
```js
|
||||
const topics = ['a', 'b']
|
||||
const handler = (msg) => {
|
||||
// msg.data - pubsub data received
|
||||
const data = msg.data.toString()
|
||||
}
|
||||
libp2p.pubsub.subscribe(topics, handler)
|
||||
```
|
||||
|
||||
**After**
|
||||
|
||||
```js
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
const topics = ['a', 'b']
|
||||
const handler = (msg) => {
|
||||
// msg.data - pubsub data received
|
||||
const data = uint8ArrayToString(msg.data)
|
||||
}
|
||||
|
||||
topics.forEach((topic) => {
|
||||
libp2p.pubsub.on(topic, handler)
|
||||
libp2p.pubsub.subscribe(topic)
|
||||
})
|
||||
```
|
||||
|
||||
#### Unsubscribe
|
||||
|
||||
Handlers should not be directly bound to the subscription anymore.
|
||||
|
Loading…
x
Reference in New Issue
Block a user