api: Initializing connection - Slow - 7 Seconds+

Connecting to any network with polkadot.js is slow and usually takes 7/8 seconds or more regardless of the network speed.

  • Polkadot, hosted by parity - 7.64s stalled
  • Polkadot, hosted by web3 - 6.64s stalled
  • Kusama, hosted by parity - 5.49s stalled image

This behavior is consistent across networks and can be noticed also in https://polkadot.js.org/apps/

image

This issue pertains even in subsequent connections Is there any way we can bring it to 1/2 secs? client-side caching, preload registry etc?

Can be tested locally with following

import { ApiRx, WsProvider } from '@polkadot/api';
const provider = new WsProvider('wss://kusama-rpc.polkadot.io/')
const api$ = new ApiRx({ provider }).isReady; 

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 17 (8 by maintainers)

Most upvoted comments


const axios = require('axios');
import { ApiRx, WsProvider, ApiPromise } from '@polkadot/api';


const testKusama = (async () => {
    var genesisHashQuery = JSON.stringify({ "id": "1", "jsonrpc": "2.0", "method": "chain_getBlockHash", "params": [0] });
    var runtimeQuery = JSON.stringify({ "id": "1", "jsonrpc": "2.0", "method": "state_getRuntimeVersion", "params": [] });
    var metadataQuery = JSON.stringify({ "id": "1", "jsonrpc": "2.0", "method": "state_getMetadata", "params": [] });
    var config = {
        method: 'post',
        url: 'https://kusama-rpc.polkadot.io',
        headers: {
            'Content-Type': 'application/json',
        }
    };

    const genesisHashdata = await axios({ ...config, ...{ data: genesisHashQuery } });
    const runtimedata = await axios({ ...config, ...{ data: runtimeQuery } });
    const metadatadata = await axios({ ...config, ...{ data: metadataQuery } });

    const metadataKey = `${genesisHashdata.data.result}-${runtimedata.data.result.specVersion}`;
    const metaValue = metadatadata.data.result;
    let metadata = {};
    metadata[metadataKey] = metaValue;
    const ENDPOINT = 'wss://kusama-rpc.polkadot.io';

    console.time('kusamaWithMetaReady');
    await ApiPromise.create({ provider: new WsProvider(ENDPOINT), metadata })
    console.timeEnd('kusamaWithMetaReady');
    console.time('kusamaWithoutMetaReady');
    await ApiPromise.create({ provider: new WsProvider(ENDPOINT) });
    console.timeEnd('kusamaWithoutMetaReady')

})(); 

Result kusamaWithMetaReady: 3675.406982421875 ms kusamaWithoutMetaReady: 5543.93603515625 ms

Definite improvement 👍

This helps.

I can improvise loading time for subsequent new WebSocket instances by caching metadata of the first WS Instance for Websites and Electron using persistent data indexdb or AsyncStorage for React Native. And preload it for serverless nodeJs WS instances

Tx

It is updated on upgrades. (Hence being keyed by genesisHash and specVersion in the options)

It is the actual hex metadata as retrieved from the chain, e.g locally is is via curl -H "Content-Type: application/json" -d '{"id":"1", "jsonrpc":"2.0", "method": "state_getMetadata", "params":[]}' http://localhost:9933