trufflesuite / ganache-cli-archive

Fast Ethereum RPC client for testing and development. See https://github.com/trufflesuite/ganache for current development.
https://www.trufflesuite.com/ganache
MIT License
3.36k stars 695 forks source link

Web3 timing out when connecting over websocket #364

Closed munawarb closed 6 years ago

munawarb commented 7 years ago
When I connect over websocket (because I'm trying to consume events,) the websocket connection times out. ## Expected Behavior

Starting a web3 connection like this: var web3 = new Web3(new Web3.providers.WebsocketProvider("ws://localhost:8545")); should open a websocket connection so I can consume events from the smart contract. I have testjs-rpc running like this: testrpc -m "hi"

Current Behavior

I get this error: /media/iot/nvme/ethereum/webap/node_modules/web3/packages/web3-providers-ws/node_modules/websocket/lib/W3CWebSocket.js:111 throw new Error('cannot call send() while not connected'); ^

Error: cannot call send() while not connected at W3CWebSocket.send (/media/iot/nvme/ethereum/webap/node_modules/web3/packages/web3-providers-ws/node_modules/websocket/lib/W3CWebSocket.js:111:15) at WebsocketProvider.send (/media/iot/nvme/ethereum/webap/node_modules/web3/packages/web3-providers-ws/src/index.js:209:21) at Timeout._onTimeout (/media/iot/nvme/ethereum/webap/node_modules/web3/packages/web3-providers-ws/src/index.js:200:19) at ontimeout (timers.js:469:11) at tryOnTimeout (timers.js:304:5) at Timer.listOnTimeout (timers.js:264:5)

Possible Solution

Steps to Reproduce (for bugs)

1. 2. 3. 4.

Context

I can't use HttpProvider because it's been deprecated and doesn't support event listening. So I'm trying to get a websocket thing going, but I'm unclear whether or not testjs-rpc supports it.

Your Environment

onbjerg commented 7 years ago

testrpc does not support WebSockets, but it definitely should, since it doesn't support IPC either, and the HTTP provider doesn't support subscriptions anymore (since web3 1.0.0).

leopoldjoy commented 7 years ago

@onbjerg Do you know if there are any plans in the near future to support WebSockets?

onbjerg commented 7 years ago

@leopoldjoy I would assume yes, but there's no official word, so practically I don't know.

I opened up an issue about it in trufflesuite/ganache-core#11, as that is what they are using internally in testrpc.

17Damon commented 6 years ago

$geth --datadir data --networkid 31415926 --ws --wsport 8546 --wsorigins "*" --rpc --rpcport 8545 --rpccorsdomain "*" --nodiscover console It works!!!

onbjerg commented 6 years ago

I'm not sure what you mean, that's geth, not testrpc.

pannous commented 6 years ago

why do they keep breaking the API?

onbjerg commented 6 years ago

This is a fast moving space. Everything should be expected to break. In this particular case, I actually think it's a pretty good idea not having long-lived connections over HTTP. 😊

The issue is that the ecosystem is pretty small, so keeping up with breaking changes is a bit of a hassle 😄

benjamincburns commented 6 years ago

@munawarb Thanks for submitting this issue.

TestRPC does not as of yet support WebSocket connections, so I'm sad to say that this is expected behaviour. This is fairly high on my list of priorities for new features, but at the moment I'm focused on fixing up some defects. Once I get things a bit more stabilized, I'll dust off the old WebSockets PR and see what it'll take to get it merged.

benjamincburns commented 6 years ago

@pannous I'm not sure what you were referring to when you mentioned breaking the API. In this case, there wasn't anything to break, as WebSockets are a feature which we have never supported (though per my last comment, I expect we'll have support for them before the end of the year). If we've inadvertently broken something, please do raise another issue.

ojones commented 6 years ago

Giveth relies on websockets so we use forked version of testrpc created by perissology: "ethereumjs-testrpc": "git://github.com/perissology/testrpc.git#81216dbc" This allows us to use websockets as expected (note changed port in our configs): "blockchain": { "nodeUrl": "ws://localhost:8546", "startingBlock": 0 }, You can see more about how we use it at https://github.com/Giveth/feathers-giveth