[Ethereum] “Connection not open” error when subscribing to address with websocket

go-ethereumweb3jswebsocket

I'm trying to subscribe to transactions sent to or from a particular ethereum address. I also want to test this in a local private network, which is why I'm not using etherscan or something like that.

I start geth with this command: geth --ws --mine --minerthreads=1 --datadir ./test/testDataDir/ --networkid 15

I connect to the geth console with this command: geth attach ipc:./test/testDataDir/geth.ipc

Then I unlock the account and submit a transaction, like this:

web3.eth.sendTransaction({from:eth.coinbase, to:"0x3e1127Bf1A673D378a8570f7a79cEA4F10E20489", value: 100})
"0xe122a9fa56068f9c10802bf607b572d57de83022f6b46238b6a96629c5a0478e"

This is the output of the miner:


INFO [07-03|11:18:04] Successfully sealed new block number=1846 hash=fe4bcc…c4eea9
INFO [07-03|11:18:04] 🔗 block reached canonical chain number=1841 hash=a45268…7aee1b
INFO [07-03|11:18:04] 🔨 mined potential block number=1846 hash=fe4bcc…c4eea9
INFO [07-03|11:18:04] Commit new mining work number=1847 txs=0 uncles=0 elapsed=160.998µs
INFO [07-03|11:18:05] Submitted transaction fullhash=0xe122a9fa56068f9c10802bf607b572d57de83022f6b46238b6a96629c5a0478e recipient=0x3e1127Bf1A673D378a8570f7a79cEA4F10E20489
INFO [07-03|11:18:14] Successfully sealed new block number=1847 hash=76f3ee…169d0f
INFO [07-03|11:18:14] 🔗 block reached canonical chain number=1842 hash=d170fb…84b87e
INFO [07-03|11:18:14] 🔨 mined potential block number=1847 hash=76f3ee…169d0f
INFO [07-03|11:18:14] Commit new mining work number=1848 txs=1 uncles=0 elapsed=233.923µs

Here's how I connect to web3 from my code:


const Web3 = require('web3'); //1.0.0-beta.34
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
}
else {
web3 = new Web3('ws://127.0.0.1:8546');
}

And here's where I subscribe to the logs. I want to have a callback whenever a transaction is sent to a particular address.


var options = {
fromBlock: '0x0',
address: '0x3e1127Bf1A673D378a8570f7a79cEA4F10E20489'
};
var subscription = web3.eth.subscribe('logs', options, function(error, result){
if(error || result == null){
console.log('Error when watching incoming transactions: ', error.message);
return;
}
console.log('Got something back: ', result);
// code continues...
}
subscription.on('data', function(log){
console.log(log);
});

But this is what gets printed on the console from my code:


connection not open on send()
Error when watching incoming transactions: connection not open
connection not open on send()
Error when watching incoming transactions: connection not open
connection not open on send()
Error when watching incoming transactions: connection not open
connection not open on send()
Error when watching incoming transactions: connection not open

Anyone have any idea what could be causing this? Perhaps I'm not connecting to the websocket correctly.

Best Answer

Try this :

geth --ws  --wsaddr "0.0.0.0" --wsapi "eth,net,web3,admin,shh" --wsorigins "*"  --mine --minerthreads=1 --datadir ./test/testDataDir/ --networkid 15
Related Topic