|
4 | 4 | * [version](#version)
|
5 | 5 | * [dns](#dns)
|
6 | 6 | * [stop](#stop)
|
| 7 | +* [ping](#ping) |
| 8 | +* [pingPullStream](#pingpullstream) |
| 9 | +* [pingReadableStream](#pingreadablestream) |
7 | 10 |
|
8 | 11 | #### `id`
|
9 | 12 |
|
@@ -105,4 +108,140 @@ ipfs.stop((err) => {
|
105 | 108 |
|
106 | 109 | A great source of [examples][] can be found in the tests for this API.
|
107 | 110 |
|
| 111 | +#### `ping` |
| 112 | + |
| 113 | +> Send echo request packets to IPFS hosts |
| 114 | +
|
| 115 | +##### `Go` **WIP** |
| 116 | + |
| 117 | +##### `JavaScript` - ipfs.ping(peerId, [options], [callback]) |
| 118 | + |
| 119 | +Where: |
| 120 | + |
| 121 | +- `peerId` (string) ID of the peer to be pinged. |
| 122 | +- `options` is an optional object argument that might include the following properties: |
| 123 | + - `count` (integer, default 10): the number of ping messages to send |
| 124 | +- `callback` must follow `function (err, responses) {}` signature, where `err` is an error if the operation was not successful. `responses` is an Array of ping response objects of the form: |
| 125 | + |
| 126 | + ```js |
| 127 | + { |
| 128 | + success: true, |
| 129 | + time: 1234, |
| 130 | + text: '' |
| 131 | + } |
| 132 | + ``` |
| 133 | + |
| 134 | + Note that not all ping response objects are "pongs". A "pong" message can be identified by a truthy `success` property and an empty `text` property. Other ping responses are failures or status updates. |
| 135 | + |
| 136 | + If no `callback` is passed, a promise is returned. |
| 137 | + |
| 138 | +**Example:** |
| 139 | + |
| 140 | +```JavaScript |
| 141 | +ipfs.ping('Qmhash', function (err, responses) { |
| 142 | + if (err) { |
| 143 | + throw err |
| 144 | + } |
| 145 | +
|
| 146 | + responses.forEach((res) => { |
| 147 | + if (res.time) { |
| 148 | + console.log(`Pong received: time=${res.time} ms`) |
| 149 | + } else { |
| 150 | + console.log(res.text) |
| 151 | + } |
| 152 | + }) |
| 153 | +}) |
| 154 | +``` |
| 155 | + |
| 156 | +A great source of [examples][] can be found in the tests for this API. |
| 157 | + |
| 158 | +#### `pingPullStream` |
| 159 | + |
| 160 | +> Stream echo request packets to IPFS hosts |
| 161 | + |
| 162 | +##### `Go` **WIP** |
| 163 | + |
| 164 | +##### `JavaScript` - ipfs.pingPullStream(peerId, [options], [callback]) |
| 165 | + |
| 166 | +Where: |
| 167 | + |
| 168 | +- `peerId` (string) ID of the peer to be pinged. |
| 169 | +- `options` is an optional object argument that might include the following properties: |
| 170 | + - `count` (integer, default 10): the number of ping messages to send |
| 171 | + |
| 172 | +Returns a [`PullStream`][ps] of ping response objects of the form: |
| 173 | + |
| 174 | +```js |
| 175 | +{ |
| 176 | + success: true, |
| 177 | + time: 1234, |
| 178 | + text: '' |
| 179 | +} |
| 180 | +``` |
| 181 | + |
| 182 | +Note that not all ping response objects are "pongs". A "pong" message can be identified by a truthy `success` property and an empty `text` property. Other ping responses are failures or status updates. |
| 183 | + |
| 184 | +**Example:** |
| 185 | + |
| 186 | +```JavaScript |
| 187 | +const pull = require('pull-stream') |
| 188 | +
|
| 189 | +pull( |
| 190 | + ipfs.pingPullStream('Qmhash'), |
| 191 | + pull.drain((res) => { |
| 192 | + if (res.time) { |
| 193 | + console.log(`Pong received: time=${res.time} ms`) |
| 194 | + } else { |
| 195 | + console.log(res.text) |
| 196 | + } |
| 197 | + }) |
| 198 | +) |
| 199 | +``` |
| 200 | + |
| 201 | +A great source of [examples][] can be found in the tests for this API. |
| 202 | + |
| 203 | +#### `pingReadableStream` |
| 204 | + |
| 205 | +> Stream echo request packets to IPFS hosts |
| 206 | + |
| 207 | +##### `Go` **WIP** |
| 208 | + |
| 209 | +##### `JavaScript` - ipfs.pingReadableStream(peerId, [options], [callback]) |
| 210 | + |
| 211 | +Where: |
| 212 | + |
| 213 | +- `peerId` (string) ID of the peer to be pinged. |
| 214 | +- `options` is an optional object argument that might include the following properties: |
| 215 | + - `count` (integer, default 10): the number of ping messages to send |
| 216 | + |
| 217 | +Returns a [`ReadableStream`][rs] of ping response objects of the form: |
| 218 | + |
| 219 | +```js |
| 220 | +{ |
| 221 | + success: true, |
| 222 | + time: 1234, |
| 223 | + text: '' |
| 224 | +} |
| 225 | +``` |
| 226 | + |
| 227 | +Note that not all ping response objects are "pongs". A "pong" message can be identified by a truthy `success` property and an empty `text` property. Other ping responses are failures or status updates. |
| 228 | + |
| 229 | +**Example:** |
| 230 | + |
| 231 | +```JavaScript |
| 232 | +const stream = ipfs.pingReadableStream('Qmhash') |
| 233 | +
|
| 234 | +stream.on('data', (res) => { |
| 235 | + if (res.time) { |
| 236 | + console.log(`Pong received: time=${res.time} ms`) |
| 237 | + } else { |
| 238 | + console.log(res.text) |
| 239 | + } |
| 240 | +}) |
| 241 | +``` |
| 242 | + |
| 243 | +A great source of [examples][] can be found in the tests for this API. |
| 244 | + |
108 | 245 | [examples]: https://github.com/ipfs/interface-ipfs-core/blob/master/js/src/miscellaneous.js
|
| 246 | +[rs]: https://www.npmjs.com/package/readable-stream |
| 247 | +[ps]: https://www.npmjs.com/package/pull-stream |
0 commit comments