[Ethereum] the websocket URL in Ethereum on Geth

eventsgo-ethereumtransactionswebsocket

I've lunched an Ethereum server based on Geth. Now, I need to be notified about all incoming transactions on my generated accounts.
In my previous Bitcoin Core server, I had an option in its config file like this:

walletnotify=php -f /path/notify.php %s

Through this code, Bitcoin server sends all events to the specified file, and then, I process other things.
But I don't know what is the exact mechanism to do that in Geth and Ethereum. The official documents, only said use --wsaddr, --wsport, --wsapi flags. But, where do I have to listen to notifications?

Best Answer

Websockets

I'd recommend this page for an overview of the different flags you can use with Geth on launch. It mentions there that the default address for the websocket (if turned on with --ws) is localhost:8546.

Logging all transactions

I don't think this will output all new txs out-of-the-box, though I am a bit out of my water here. You could use a JavaScript script to accomplish this without websockets or ipc (loading a script with the --js flag followed by the path to the script), or you could use subscriptions. The way your question is phrased implies your looking for the latter, so I'll try to focus on that. The page in the docs is the one right after the one you linked: https://geth.ethereum.org/docs/rpc/pubsub .

You're going to need to make a call to the exposed websocket (or rpc, if you prefer - I'm sticking with websockets since you mentioned them in the question). You could use Postman for this if you like. (If someone has a cleaner way of doing this, please suggest it.) In order to subscribe to receive new blocks as they're added to the chain, and then to include all txs in the block, send:

{"id": 1, "method": "eth_subscribe", "params": ["newHeads", {"includeTransactions": true}]}

If you're using the default websocket settings, for instance, that object should be sent to localhost:8546.

You should receive an object of data in return, something like:

{"jsonrpc":"2.0","id":1,"result":"0xcd0c3e8af590364c09d0fa6a1210faf5"}

The result is your subscription id. You should now get a stream of data over websockets. Take note of your subscription id, you'll need it if you want to unsubscribe.

Related Topic