Skip to content

Commit bb59b51

Browse files
chore: complement 0.29 migration for pubsub subscribe (#755)
* chore: complement 0.29 migration for pubsub subscribe * chore: update doc/migrations/v0.28-v0.29.md Co-authored-by: Jacob Heun <[email protected]>
1 parent fb4b273 commit bb59b51

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

doc/migrations/v0.28-v0.29.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,36 @@ libp2p.pubsub.on(topic, handler)
9090
libp2p.pubsub.subscribe(topic)
9191
```
9292

93+
In the latest release, 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.
94+
95+
**Before**
96+
97+
```js
98+
const topics = ['a', 'b']
99+
const handler = (msg) => {
100+
// msg.data - pubsub data received
101+
const data = msg.data.toString()
102+
}
103+
libp2p.pubsub.subscribe(topics, handler)
104+
```
105+
106+
**After**
107+
108+
```js
109+
const uint8ArrayToString = require('uint8arrays/to-string')
110+
111+
const topics = ['a', 'b']
112+
const handler = (msg) => {
113+
// msg.data - pubsub data received
114+
const data = uint8ArrayToString(msg.data)
115+
}
116+
117+
topics.forEach((topic) => {
118+
libp2p.pubsub.on(topic, handler)
119+
libp2p.pubsub.subscribe(topic)
120+
})
121+
```
122+
93123
#### Unsubscribe
94124

95125
Handlers should not be directly bound to the subscription anymore.

0 commit comments

Comments
 (0)