From 7c8120cacab07241a168df1eee7ae9d9a7868d69 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Wed, 16 Sep 2020 09:58:10 +0200 Subject: [PATCH] chore: complement 0.29 migration for pubsub subscribe --- doc/migrations/v0.28-v0.29.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/doc/migrations/v0.28-v0.29.md b/doc/migrations/v0.28-v0.29.md index 9eb2d6d7..fea46669 100644 --- a/doc/migrations/v0.28-v0.29.md +++ b/doc/migrations/v0.28-v0.29.md @@ -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.