I've tried to listen to events using websockets.
import asyncio, json
from websockets import connect
wss = 'wss://wsapi.fantom.network'
params = json.dumps({"id": 1, "method": "eth_subscribe", "params": ["logs", {"address": ['address'], "topics": ['topics']}]})
async def get_event():
async with connect(wss) as ws:
await ws.send(params)
await ws.recv()
while True:
try:
event = await asyncio.wait_for(ws.recv(), timeout=None)
except:
pass
else:
/ Decode event to get data /
if __name__ == "__main__":
loop = asyncio.get_event_loop()
while True:
loop.run_until_complete(get_event())
However, the time at which the code received the event is always ~3s later from the timestamp of the event (timestamp of the block at which the event was emitted). 3s delay means that by the time I receive the event, 2-3 blocks have already been mined, and that is not real-time update.
How to reduce the latency? Any suggestions would be appreciated.