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 open a websocket connection to my ethereum node #2

Open
cavernec opened this issue Oct 17, 2021 · 13 comments
Open

Can't open a websocket connection to my ethereum node #2

cavernec opened this issue Oct 17, 2021 · 13 comments

Comments

@cavernec
Copy link

Hi,

The following piece of code is failing

const { ethers } = require("ethers"); const AWSWebsocketProvider = require('@aws/web3-ws-provider'); const endpoint = process.env.AMB_WS_ENDPOINT; const baseProvider = new AWSWebsocketProvider(endpoint); const provider = new ethers.providers.WebSocketProvider(baseProvider)

with error

node:events:368
throw er; // Unhandled 'error' event
^
Error: Unexpected server response: 403
at ClientRequest. (/home/ubuntu/node_modules/ws/lib/websocket.js:604:7)
at ClientRequest.emit (node:events:390:28)
at ClientRequest.emit (node:domain:475:12)
at HTTPParser.parserOnIncomingClient [as onIncoming] (node:_http_client:623:27)
at HTTPParser.parserOnHeadersComplete (node:_http_common:128:17)
at TLSSocket.socketOnData (node:_http_client:487:22)
at TLSSocket.emit (node:events:390:28)
at TLSSocket.emit (node:domain:475:12)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
Emitted 'error' event on WebSocket instance at:
at abortHandshake (/home/ubuntu/node_modules/ws/lib/websocket.js:731:15)
at ClientRequest. (/home/ubuntu/node_modules/ws/lib/websocket.js:604:7)
[... lines matching original stack trace ...]
at addChunk (node:internal/streams/readable:315:12)

I have the right credentials exported in my ~.bashrc file because the following piece of code is working fine 👍

const { ethers } = require("ethers"); const AWSHttpProvider = require('@aws/web3-http-provider'); const endpoint = process.env.AMB_HTTP_ENDPOINT; const baseProvider = new AWSHttpProvider(endpoint); const provider = new ethers.providers.Web3Provider(baseProvider);

Would you please have any idea what's going on ?

Thanks !

@cayblood
Copy link

It's possible that AMB_WS_ENDPOINT has been set in your environment but not AMB_HTTP_ENDPOINT. Have you double-checked that the value of that env var is set properly?

@uireactdev
Copy link

uireactdev commented Oct 19, 2021

I do see same issue working for HttpProvider and not working for Websocket provider with ethers, where as with web3 no issues for websocket connection. With ethers if we use Web3Provider instead of WebSocketProvider is working fine something like

const baseProvider = new AWSWebsocketProvider(endpoint, options);
let provider = new ethers.providers.Web3Provider(baseProvider);

@cavernec
Copy link
Author

I do see same issue working for HttpProvider and not working for Websocket provider with ethers, where as with web3 no issues for websocket connection. With ethers if we use Web3Provider instead of WebSocketProvider is working fine something like

const baseProvider = new AWSWebsocketProvider(endpoint, options);
let provider = new ethers.providers.Web3Provider(baseProvider);

I don't think this is actually working, I mean the code is not breaking anywhere but if you do something like

provider.on("pending", (tx) => { console.log(tx); });

then you will see that the provider is not working as intended

@cavernec
Copy link
Author

It's possible that AMB_WS_ENDPOINT has been set in your environment but not AMB_HTTP_ENDPOINT. Have you double-checked that the value of that env var is set properly?

yes both variables are correctly set up

@uireactdev
Copy link

I do see same issue working for HttpProvider and not working for Websocket provider with ethers, where as with web3 no issues for websocket connection. With ethers if we use Web3Provider instead of WebSocketProvider is working fine something like

const baseProvider = new AWSWebsocketProvider(endpoint, options);
let provider = new ethers.providers.Web3Provider(baseProvider);

I don't think this is actually working, I mean the code is not breaking anywhere but if you do something like

provider.on("pending", (tx) => { console.log(tx); });

then you will see that the provider is not working as intended

I tried with poll instead of pending which is working.

provider.on("poll", async () => {
        provider.getBlockNumber()
          .then(function (block) {
             console.log(block);
          });;
      })

@heyitaki
Copy link

heyitaki commented Oct 24, 2021

+1 to this issue, websocket and http connections are working with web3, but websocket is failing with ethers

@cavernec
Copy link
Author

@heyitaki Unfortunately no... @cayblood any idea ?

Does anyone know if we can get pending transactions from Amazon Managed Blockchain ? It looks like provider.on("poll") works well on my end but I can't make provider.on("pending") work...

@evertonfraga
Copy link

Hi @heyitaki, @cavernec, @uireactdev, sorry about the delay catching up here.

Let me investigate that for you. Amazon Managed Blockchain definitely supports pending transaction at the RPC level. Some questions:

  1. Are you running your code from Node.js or the browser?
  2. Ethers' Websocket provider does not forward headers to the WS module.

Let me debug that and come up with a working code.

@cavernec
Copy link
Author

hi @evertonfraga, thanks for looking into this ! I am indeed running the code from Node.js, not sure about others people though

@heyitaki
Copy link

Working around this for now by using web3 wrapper instead of ethers

@molfar
Copy link

molfar commented Jan 26, 2022

I have the same error with nodejs and ethers.

@RayVR
Copy link

RayVR commented May 16, 2022

Running all my code in node.js locally.

I'd like to suggest the ethers example be removed from the docs until this is resolved as it is missing basic functionality.

Unfortunately, this wasted a couple hours for me. I usually check the issues on open source projects but since this came from Amazon I guess my expectations were a little higher.

I can't use ethers + WS to monitor pending transactions or monitor anything from contracts. One Example: set up an event listener for '*' on a Uniswap pool contract (here's a fairly active one 0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640)

You'll get nothing from the listener.

@hansiang93
Copy link

const endpoint = 'wss://nd-NODEID.wss.ethereum.managedblockchain.REGION.amazonaws.com';
const baseProvider = new AWSWebsocketProvider(endpoint, { clientConfig: { credentials: credentials }}));
let provider = new ethers.providers.WebSocketProvider(baseProvider); # <---- this is wrong

do this instead

let provider = new ethers.providers.WebSocketProvider(baseProvider.connection);

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

No branches or pull requests

8 participants