Go-Ethereum – Troubleshooting Geth Subscribe Not Working

go-ethereum

I've started up a local node (an avalanche node) and I've successfully connected to it using geth.

 ❯❯❯ geth attach http://127.0.0.1:65130/ext/bc/C/rpc
Welcome to the Geth JavaScript console!

instance: v0.8.4-rc.3
coinbase: 0x0100000000000000000000000000000000000000
at block: 0 (Wed Dec 31 1969 18:00:00 GMT-0600 (CST))
 modules: eth:1.0 net:1.0 rpc:1.0 web3:1.0

I'd like to subscribe to events on this node. The geth docs mention that this should be possible by sending json through the console. When I try to send this json, I get complaints about the json formatting:

> {"id": 1, "method": "eth_subscribe", "params": ["newHeads", {}]}
SyntaxError: SyntaxError: (anonymous): Line 1:6 Unexpected token : (and 2 more errors)

even though I copied this json directly from the docs (and it seems well formed to me).

The docs also mention that I need a minimum version of 1.4 and I have version 1.10:

 ❯❯❯ geth version
Geth
Version: 1.10.16-stable
Architecture: amd64
Go Version: go1.17.6
Operating System: darwin
GOPATH=
GOROOT=go

How can I use geth to subscribe to events?

Best Answer

Figured out how to subscribe using a websocket client, wscat:

 ❮❮❮ wscat -c ws://127.0.0.1:35260/ext/bc/C/ws
Connected (press CTRL+C to quit)
> {"id": 1, "method": "eth_subscribe", "params": ["newPendingTransactions"]}
< {"jsonrpc":"2.0","id":1,"result":"0x94be2b853229f8ea4b60b80b4a45b13e"}

I guess I was using the geth client incorrectly though it's not clear how geth should be used for subscriptions.

Related Topic