1

I'm trying to connect local node running on my pc using polkadot.js library in react.js(browser). I'm pasting my code in below snippet and error that I'm facing in chrome console

Code

// ************************Polkadot****************************
import {WsProvider, ApiPromise} from '@polkadot/api';
import { ContractPromise } from '@polkadot/api-contract';
import { Keyring } from '@polkadot/keyring';
// ************************************************************
import bridgePeer from './MetaData/DevErc20Metadata.json';
import bridgeTxt from './MetaData/PrivErc20Metadata.json';


//Keyring for finding the private and public key pair
const keyring = new Keyring({ type: 'ed25519' });

// Contract address and its host
const txtNode = new WsProvider('ws://127.0.0.1:9944');
 const PeerNode = new WsProvider('ws://127.0.0.1:9946');

const ContractAddressGxt = '5GDxmhbSFqoBbsdWC4qd7LLHMZNmW3xX4rLKA7Ucdgb6un4z';
const ContractAddressPeer = '5D6z8T8rWZW1Uy7RwPburZv4wo5tjhCtG8CbfR5pbgxSEY37';
let api1, api2;

(async () => {

  api1 = await ApiPromise.create({ provider:txtNode});
  api2 = await ApiPromise.create({ provider: PeerNode});

})();

Console

index.js:465 WebSocket connection to 'ws://127.0.0.1:9946/' failed: 
connect @ index.js:465
connectWithRetry @ index.js:499
WsProvider @ index.js:411
./src/Common/WasmWasmHelper.js @ WasmWasmHelper.js:15
options.factory @ react refresh:6
_webpack_require_ @ bootstrap:24
fn @ hot module replacement:62
./src/Componets/Swap/Swap.js @ loader.js:10
options.factory @ react refresh:6
_webpack_require_ @ bootstrap:24
fn @ hot module replacement:62
./src/App.js @ classPrivateFieldLooseKey.js:4
options.factory @ react refresh:6
_webpack_require_ @ bootstrap:24
fn @ hot module replacement:62
./src/index.js @ Swap.js:191
options.factory @ react refresh:6
_webpack_require_ @ bootstrap:24
(anonymous) @ startup:7
(anonymous) @ startup:7
logger.js:62 2022-06-10 18:36:55          API-WS: disconnected from ws://127.0.0.1:9946: 1006:: Abnormal Closure

Can anyone tell me why I'm facing this error any suggestion most welcome.

6
  • Are you running two nodes or why is the second port different? How did you start the nodes? Jun 10, 2022 at 15:30
  • I am running both nodes on same system, that is why i need to have different ports. Jun 13, 2022 at 4:41
  • And how did you start the nodes? Please provide the console commands, otherwise we cannot investigate this. Jun 13, 2022 at 10:12
  • 2
    You'll definitely get this error if you happen to not have a substrate node running at all. Jul 3, 2022 at 2:46
  • I've been getting this error when I continuously create new ApiPromise objects in polkadot-js in React. await ApiPromise.create({provider: ...}); Is this bad practice and should I instead be sharing the ApiPromise object between my classes?
    – forgetso
    Sep 16, 2022 at 13:25

2 Answers 2

0

1006 can be an indication that no connection was made at all.

to debug whether or not your node is even listening on the port you expect to find it, tail the node logs to check for log messages like:

Accepting new connection 1/100

Accepting new connection 2/100

then attempt new connections from something that isn't previously untested code like websocat. eg:

echo '{
  "id":1,
  "jsonrpc": "2.0",
  "method": "rpc_methods",
  "params":[]
}' | websocat -n1 -B 99999999 ws://127.0.0.1:9944

if you don't get a response to your websocat request, and you don't see new connections being made in the node logs, your problem is in the network layer. you may have a firewall or selinux setting blocking access to the port, the node may be listening on a non-default port (check your --ws-port node startup args) or ahy number of other things.

0

It could be that the node you are connecting to is out of connections.

This can be changed using --ws-max-connections nnn where nnn is the number of connections when starting the node.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.