|
| 1 | +# Migrating to the new API |
| 2 | + |
| 3 | +A migration guide for refactoring your application code from libp2p v0.26.x to v0.27.0. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +- [Migrating from callbacks](#migrating-from-callbacks) |
| 8 | +- [Pull Streams to Streaming Iterables](#pull-streams-to-streaming-iterables) |
| 9 | +- [Sample API Migrations](#sample-api-migrations) |
| 10 | + - [Registering Protocol Handlers](#registering-protocol-handlers) |
| 11 | + - [Dialing and Sending Data](#dialing-and-sending-data) |
| 12 | + - [Checking if a peer is connected](#checking-if-a-peer-is-connected) |
| 13 | + - [Pinging another peer](#pinging-another-peer) |
| 14 | + - [Pubsub](#pubsub) |
| 15 | + - [Getting subscribers](#getting-subscribers) |
| 16 | + - [Getting subscribed topics](#getting-subscribed-topics) |
| 17 | + |
| 18 | +## Migrating from callbacks |
| 19 | + |
| 20 | +Callbacks are no longer supported in the libp2p API, as the API has now fully moved to async / await. You can see a full list of the available methods in the [API readme][api] |
| 21 | + |
| 22 | +**Before** |
| 23 | +```js |
| 24 | +libp2p.start((err) => { |
| 25 | + if (err) throw err |
| 26 | + console.log('libp2p started') |
| 27 | +}) |
| 28 | +``` |
| 29 | + |
| 30 | +**After** |
| 31 | +```js |
| 32 | +await libp2p.start() |
| 33 | +console.log('libp2p started') |
| 34 | +``` |
| 35 | + |
| 36 | +## Pull Streams to Streaming Iterables |
| 37 | + |
| 38 | +The libp2p API no longer supports Pull Streams and has migrated to [Streaming Iterables][streaming_iterable]. If you would like to continue using Pull Streams in your application code, or need additional time to migrate your code base, you can leverage the conversion modules [async-iterator-to-pull-stream](https://github.com/alanshaw/async-iterator-to-pull-stream) and [pull-stream-to-async-iterator](https://github.com/alanshaw/pull-stream-to-async-iterator). |
| 39 | + |
| 40 | +For a growing list of async iterator modules, you should follow the [it-awesome repo][it_awesome]. |
| 41 | + |
| 42 | +## Sample API Migrations |
| 43 | + |
| 44 | +### Registering Protocol Handlers |
| 45 | + |
| 46 | +Protocol registration is very similar to how it previously was, however, the handler now takes a single parameter containing the incoming stream and its protocol. Additionally, you can now pass an array of protocols to `.handle`, but a single string is still supported. |
| 47 | + |
| 48 | +**Before** |
| 49 | +```js |
| 50 | +const pull = require('pull-stream') |
| 51 | +libp2p.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn)) |
| 52 | +``` |
| 53 | + |
| 54 | +**After** |
| 55 | +```js |
| 56 | +const pipe = require('it-pipe') |
| 57 | +libp2p.handle(['/echo/1.0.0'], ({ protocol, stream }) => pipe(stream, stream)) |
| 58 | +``` |
| 59 | + |
| 60 | +### Dialing and Sending Data |
| 61 | + |
| 62 | +`dialProtocol` no longer takes a callback, and will now return a [Streaming Iterable][streaming_iterable] and the protocol that was successfully negotiated. The new stream can be used with async iterator modules, see [it-awesome][it_awesome], instead of pull streams. |
| 63 | + |
| 64 | +**Before** |
| 65 | +```js |
| 66 | +const pull = require('pull-stream') |
| 67 | +libp2p.dialProtocol(peerInfo, '/echo/1.0.0', (err, conn) => { |
| 68 | + if (err) { throw err } |
| 69 | + pull( |
| 70 | + pull.values(['hey']), |
| 71 | + conn, |
| 72 | + pull.drain((data) => { |
| 73 | + console.log('received echo:', data.toString()) |
| 74 | + }, (err) => { |
| 75 | + if (err) { throw err } |
| 76 | + }) |
| 77 | + ) |
| 78 | +}) |
| 79 | +``` |
| 80 | + |
| 81 | +**After** |
| 82 | +```js |
| 83 | +const pipe = require('it-pipe') |
| 84 | +const { protocol, stream } = await libp2p.dialProtocol(peerInfo, '/echo/1.0.0') |
| 85 | +await pipe( |
| 86 | + ['hey'], |
| 87 | + stream, |
| 88 | + async function (source) { |
| 89 | + for await (const data of source) { |
| 90 | + console.log('received echo:', data.toString()) |
| 91 | + } |
| 92 | + } |
| 93 | +) |
| 94 | +``` |
| 95 | + |
| 96 | +### Checking if a peer is connected |
| 97 | + |
| 98 | +`peerInfo.isConnected` has been deprecated. libp2p now tracks all connections centrally and will no longer update the state of `peerInfo.isConnected`. Consumers should switch to using `libp2p.registrar.getConnection(peerInfo)`, which will return an open connection to that peer if one exists. |
| 99 | + |
| 100 | +**Before** |
| 101 | +```js |
| 102 | +if (peerInfo.isConnected()) { |
| 103 | + // ...do something if connected |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +**After** |
| 108 | +```js |
| 109 | +const connection = libp2p.registrar.getConnection(peerInfo) |
| 110 | +if (connection) { |
| 111 | + // ...do something if connected |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +### Pinging another peer |
| 116 | + |
| 117 | +`libp2p.ping` will no longer callback with a `Ping` event emitter. The internal logic has been simplified to give more flexibility to the API. `libp2p.ping` will now execute a single ping and return the latency. |
| 118 | + |
| 119 | +**Before** |
| 120 | +```js |
| 121 | +libp2p.ping(peerInfo, (err, ping) => { |
| 122 | + if (err) throw err |
| 123 | + ping.once('ping', (latency) => { |
| 124 | + console.log('Latency is %s ms', latency) |
| 125 | + ping.stop() |
| 126 | + }) |
| 127 | + |
| 128 | + ping.start() |
| 129 | +}) |
| 130 | +``` |
| 131 | + |
| 132 | +**After** |
| 133 | +```js |
| 134 | +const latency = await libp2p.ping(peerInfo) |
| 135 | +console.log('Latency is %s ms', latency) |
| 136 | +``` |
| 137 | + |
| 138 | +### Pubsub |
| 139 | + |
| 140 | +#### Getting subscribers |
| 141 | + |
| 142 | +`libp2p.pubsub.peers()` is now `libp2p.pubsub.getSubscribers()` and is no longer an asynchronous action. |
| 143 | + |
| 144 | +**Before** |
| 145 | +```js |
| 146 | +libp2p.pubsub.peers(topic, (err, subscribers) => { |
| 147 | + if (err) throw err |
| 148 | + console.log('Subscribers:', subscribers) |
| 149 | +}) |
| 150 | +``` |
| 151 | + |
| 152 | +**After** |
| 153 | +```js |
| 154 | +const subscribers = libp2p.pubsub.getSubscribers(topic) |
| 155 | +console.log('Subscribers:', subscribers) |
| 156 | +``` |
| 157 | + |
| 158 | +#### Getting subscribed topics |
| 159 | + |
| 160 | +`libp2p.pubsub.ls()` is now `libp2p.pubsub.getTopics()` and is no longer an asynchronous action. |
| 161 | + |
| 162 | +**Before** |
| 163 | +```js |
| 164 | +libp2p.pubsub.ls((err, topics) => { |
| 165 | + if (err) throw err |
| 166 | + console.log('Topics:', topics) |
| 167 | +}) |
| 168 | +``` |
| 169 | + |
| 170 | +**After** |
| 171 | +```js |
| 172 | +const topics = libp2p.pubsub.getTopics() |
| 173 | +console.log('Topics:', topics) |
| 174 | +``` |
| 175 | + |
| 176 | +[api]: ../API.md |
| 177 | +[it_awesome]: https://github.com/alanshaw/it-awesome |
| 178 | +[streaming_iterable]: ../STREAMING_ITERABLES.md |
0 commit comments