[Ethereum] Subscribe to events usign geth websocket

eventsgo-ethereumjson-rpcwebsocket

I'm trying to subscribe to geth using the websocket api. First step was to start geth:

geth --ws --wsport=8456 --wsorigins="*" --fast --cache=512 --rpcport=8455 --rpc --rpcapi="personal,eth,network"

Then I tried to connect to it using websockets from python:

from websockets import connect
import asyncio

async def get_event():
    async with connect("ws://localhost:8456") as ws:
        await ws.send(json.dumps({"id": 1, "method": "eth_subscribe", "params": ["newPendingTransactions"]}))
        subscription_response = await ws.recv()
        while True:
            try:
                message = await asyncio.wait_for(ws.recv(), timeout=60)
                pass
            except asyncio.TimeoutError:
                # No data in 20 seconds, check the connection.
                try:
                    pong_waiter = await ws.ping()
                    await asyncio.wait_for(pong_waiter, timeout=10)
                except asyncio.TimeoutError:
                    # No response to ping in 10 seconds, disconnect.
                    logger.critical("socket timeout")
                    break
            else:
                pprint(message)
if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    while True:
        loop.run_until_complete(get_event())

I'm getting subscription response:
'{"jsonrpc":"2.0","id":1,"result":"0xde19c48fe52cd12207f807f330630e2b"}'

But not receiving any messages after that and running in the TimeoutError. Any idea what is going wrong?

Best Answer

At first glance it appears you are not enabling any of the available --wsapi methods (personal, admin, eth, web3). You are enabling them on --rpcapi. That could be what is causing the time out. Does geth time out, or your application?

Related Topic