Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't interact with geth over http or ws from node.js express app #3600

Closed
j4ys0n opened this issue Jun 25, 2020 · 3 comments
Closed

Can't interact with geth over http or ws from node.js express app #3600

j4ys0n opened this issue Jun 25, 2020 · 3 comments
Labels
1.x 1.0 related issues

Comments

@j4ys0n
Copy link

j4ys0n commented Jun 25, 2020

Expected behavior

Can interact with node over http or ws from node.js express app

Actual behavior

over http:

(node:70270) UnhandledPromiseRejectionWarning: Error: Invalid JSON RPC response: ""
    at Object.InvalidResponse (/Users/jayson/Code/contract-cloud/ds/node_modules/web3/node_modules/web3-core-helpers/src/errors.js:42:16)
    at XMLHttpRequest.request.onreadystatechange (/Users/jayson/Code/contract-cloud/ds/node_modules/web3/node_modules/web3-providers-http/src/index.js:107:32)
    at XMLHttpRequestEventTarget.dispatchEvent (/Users/jayson/Code/contract-cloud/ds/node_modules/xhr2-cookies/xml-http-request-event-target.ts:44:13)
    at XMLHttpRequest._setReadyState (/Users/jayson/Code/contract-cloud/ds/node_modules/xhr2-cookies/xml-http-request.ts:219:8)

over ws:

(node:69632) UnhandledPromiseRejectionWarning: Error: connection not open on send()
    at Object.ConnectionError (/Users/jayson/Code/contract-cloud/ds/node_modules/web3/node_modules/web3-core-helpers/src/errors.js:69:23)
    at Object.ConnectionNotOpenError (/Users/jayson/Code/contract-cloud/ds/node_modules/web3/node_modules/web3-core-helpers/src/errors.js:48:21)
    at WebsocketProvider.send (/Users/jayson/Code/contract-cloud/ds/node_modules/web3/node_modules/web3-providers-ws/src/index.js:321:33)
    at RequestManager.send (/Users/jayson/Code/contract-cloud/ds/node_modules/web3/node_modules/web3-core-requestmanager/src/index.js:162:66)

Steps to reproduce the behavior

  1. set up express api
  2. use express endpoint/route to trigger web3 connection to geth
  3. try to send anything to geth

Logs

I'm running a geth client with the --debug synced to ropsten and there are no related messages on the console.

Environment

node v12.16.3, web3 v1.2.9 (also tried 1.2.8), macOS 10.15 and Ubuntu 18

i've tried web3.eth.getAccounts() and something like:

    const c = new web3.eth.Contract(contract.abi);
    const from = {from: creator, gas: 3000000}
    const simple = c
      .deploy({data: contract.bytecode, arguments: [...values]})
      .send(from)

if i'm not running the express api in front of the web3 connection, everything works as desired. this only happens when the web3 connection is initiated from the express app.

@cgewecke
Copy link
Collaborator

@j4ys0n

The error messages indicate the connection isn't getting established. How are you instantiating Web3?

There is a simple Web3 Express example here that might be worth looking at. It uses Web3 0.2.x and would need to be updated for Web3 1.x. But it might help you identify some small issue in your code that's making it fail.

if i'm not running the express api in front of the web3 connection, everything works as desired. this only happens when the web3 connection is initiated from the express app.

This suggests it's not a Web3 issue per se, but a problem with the way you're using Express.

@j4ys0n
Copy link
Author

j4ys0n commented Jun 25, 2020

Thanks for the example. Unfortunately, it doesn't really help. My app uses Typescript and ES6 classes. This could be a scoping issue on my part, but I'm not seeing it right now, and I've done similar things in the past. I would think that if I'm following the same patterns, and that was the issue, mongodb and web3 wouldn't work, but mongodb does work.

This is how I'm instantiating web3

    const reconnect = {
      auto: true,
      delay: 1000,
      maxAttempts: 10
    }
    this.web3 = new Web3()
    // const provider = new Web3.providers.WebsocketProvider(`ws://${NODE}`, {reconnect})
    const provider = new Web3.providers.HttpProvider(`http://${NODE}`)
    this.web3.setProvider(provider)

i've been trying ws and http, but only one at a time.

Even if I instantiate Web3 and try to interact with geth all in the same scope/function, i still get the result above.

@j4ys0n
Copy link
Author

j4ys0n commented Jun 25, 2020

ok. dumb mistake. the ip address of my server with the geth node changed (without restarting the server, which is strange). 🤦🏼‍♂️
good to go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.x 1.0 related issues
Projects
None yet
Development

No branches or pull requests

2 participants