lightstreams-network/lightchain

Websockets + Web3: `Error: Connection not open`

Closed this issue · 3 comments

System information

Lightchain version: Version: 1.1.0-beta MainNet (but running off fork https://github.com/0x13a/lightchain/tree/fix/upgrade-geth)
OS & version: OSX Mojave 10.14.4 / Kernel Version 18.5.0
Go version: go version go1.12.5 darwin/amd64
Branch: https://github.com/0x13a/lightchain/tree/fix/upgrade-geth
Commit hash: cb8e32e6209e8aaf302ac21d67cabe014b667b3e

Expected behaviour

Lightchain works fine with the Web3 HTTP provider, but with websockets, I cannot seem to open a connection.

I noticed there is no CLI option for --wsapi, is this on purpose?

web3.eth.getAccounts().then(console.log).catch(console.error)

Should return

[ '0xc916Cfe5c83dD4FC3c3B0Bf2ec2d4e401782875e' ]

Actual behaviour

Error: connection not open
    at WebsocketProvider.send (/Users/emizzle/temp/lightstreams-js-sdk/node_modules/web3-providers-ws/src/index.js:276:18)
    at RequestManager.send (/Users/emizzle/temp/lightstreams-js-sdk/node_modules/web3-core-requestmanager/src/index.js:132:66)
    at sendRequest (/Users/emizzle/temp/lightstreams-js-sdk/node_modules/web3-core-method/src/index.js:560:42)
    at Eth.send [as getBalance] (/Users/emizzle/temp/lightstreams-js-sdk/node_modules/web3-core-method/src/index.js:581:13)
    at repl:1:10
    at Script.runInThisContext (vm.js:91:20)
    at REPLServer.defaultEval (repl.js:317:29)
    at bound (domain.js:396:14)
    at REPLServer.runBound [as eval] (domain.js:409:12)
    at REPLServer.onLine (repl.js:615:10)
    at REPLServer.emit (events.js:187:15)
    at REPLServer.EventEmitter.emit (domain.js:442:20)
    at REPLServer.Interface._onLine (readline.js:290:10)
    at REPLServer.Interface._line (readline.js:638:8)
    at REPLServer.Interface._ttyWrite (readline.js:919:14)
    at REPLServer.self._ttyWrite (repl.js:688:7)
    at ReadStream.onkeypress (readline.js:168:10)
    at ReadStream.emit (events.js:182:13)
    at ReadStream.EventEmitter.emit (domain.js:442:20)
    at emitKeys (internal/readline.js:422:14)
    at emitKeys.next (<anonymous>)
    at ReadStream.onData (readline.js:1022:36)

Steps to reproduce the behaviour

  1. lightchain init --force --standalone
  2. lightchain run --rpc --rpcport=8555 --rpcaddr=localhost --rpcapi=eth,web3,net,debug,personal,shh --ws --wsport=8556 --wsaddr=localhost
    In a new term:
  3. Install web3 beta 37: npm install web3@1.0.0-beta.37
  4. Start a REPL: node
  5. let web3 = new Web3(new Web3.providers.WebsocketProvider("ws://localhost:8556"))
    NOTE: I have also tried let web3 = new Web3(new Web3.providers.WebsocketProvider("ws://127.0.0.1:8556")) because the run routine output shows the websocket endpoint as ws://127.0.0.1:8556:
INFO [05-16|11:43:22.533] IPC endpoint opened                      url=/Users/emizzle/.lightchain/database/geth.ipc
INFO [05-16|11:43:22.682] HTTP endpoint opened                     url=http://localhost:8555                        cors= vhosts=localhost
INFO [05-16|11:43:22.686] WebSocket endpoint opened                url=ws://127.0.0.1:8556
  1. web.eth.getAccounts().then(console.log).catch(console.error)
  2. Error: connection not open (see stack above)

This works with HTTP:

  1. lightchain init --force --standalone
  2. lightchain run --rpc --rpcport=8555 --rpcaddr=localhost --rpcapi=eth,web3,net,debug,personal,shh --ws --wsport=8556 --wsaddr=localhost
    In a new term:
  3. Install web3 beta 37: npm install web3@1.0.0-beta.37
  4. Start a REPL: node
  5. let web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8555"))
  6. web.eth.getAccounts().then(console.log).catch(console.error)
  7. [ '0xc916Cfe5c83dD4FC3c3B0Bf2ec2d4e401782875e' ]

As you metioned, there is no currently support for --wsapi and --wsorigins and that is the reason of that issue.

We just did some quick work to support them. I reproduced the same scenario you describe above with web3 and it works after the changes applied to this PR

Along the today we are planning to merge this into master and publish a new version.

Thanks for pointing this out! :)

PR was approved and merged.

This is great, thank you @ggarri! I can confirm this works now!