API docs -> WS (#141)
* API docs -> WS * API docs update * HttpProvider -> WsProvider * sub<something> -> subscribing * Update dependencies * Test message checks update
This commit is contained in:
+38
-2
@@ -26,9 +26,9 @@ Initialisation -
|
||||
|
||||
```js
|
||||
import createApi from '@polkadot/api';
|
||||
import HttpProvider from '@polkadot/api-provider/http';
|
||||
import WsProvider from '@polkadot/api-provider/ws';
|
||||
|
||||
const provider = new HttpProvider('http://127.0.0.1:9933');
|
||||
const provider = new WsProvider('http://127.0.0.1:9944');
|
||||
const api = createApi(provider);
|
||||
```
|
||||
|
||||
@@ -40,3 +40,39 @@ api.chain
|
||||
.then((header) => console.log(header))
|
||||
.catch((error) => console.error(error));
|
||||
```
|
||||
|
||||
Retrieving the best block (once-off) -
|
||||
|
||||
```js
|
||||
api.chain
|
||||
.getHead()
|
||||
.then((headerHash) => {
|
||||
return api.chain.getHeader(headerHash);
|
||||
})
|
||||
.then((header) => {
|
||||
console.log(`best #${header.number.toString()}`);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('error:', error);
|
||||
});
|
||||
```
|
||||
|
||||
Retrieving best header via subscription -
|
||||
|
||||
```js
|
||||
api.chain
|
||||
.newHead((error, header) => {
|
||||
if (error) {
|
||||
console.error('error:', error);
|
||||
}
|
||||
|
||||
console.log(`best #${header.number.toString()}`);
|
||||
})
|
||||
.then((subscriptionId) => {
|
||||
// id for the subscription, can unsubscribe via
|
||||
// api.chain.newHead.unsubscribe(subscriptionId)
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('error subscribing:', error);
|
||||
});
|
||||
```
|
||||
|
||||
@@ -34,6 +34,6 @@
|
||||
"@polkadot/api-provider": "^0.28.22",
|
||||
"@polkadot/jsonrpc": "^0.28.22",
|
||||
"@polkadot/params": "^0.28.22",
|
||||
"@polkadot/util": "^0.28.7"
|
||||
"@polkadot/util": "^0.28.14"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user