Websocket: close 1006 (abnormal closure): unexpected EOF

here is my code

cli, err := ethclient.Dial(“wss://mainnet.infura.io/ws/v3/xxxxxx”)
defer cli.Close()
contractAddress := common.HexToAddress(“0x7Be8076f4EA4A4AD08075C2508e481d6C946D12b”)
query := ethereum.FilterQuery{
FromBlock: big.NewInt(1),
ToBlock: big.NewInt(10000),
Addresses: []common.Address{contractAddress},
}
logs := make(chan types.Log)
sub, err := cli.SubscribeFilterLogs(context.Background(), query, logs)
if err != nil {
log.Fatal(err)
}
for {
select {
case err := <-sub.Err():
log.Fatal(err)
case vLog := <-logs:
fmt.Println(vLog) // pointer to event log
}
}

it always report error websocket: close 1006 (abnormal closure): unexpected EOF why?

Hi :wave: @986950908 and welcome ot the Infura community!

To be honest, I’m not too familiar with Go, but these are some suggestions I found:

Please check out the article below:
https://stackoverflow.com/questions/61108552/go-websocket-error-close-1006-abnormal-closure-unexpected-eof

Make sure that the connection wasn’t closed by a firewall, VPN, or antivirus software.

If you are using any load balancer, make sure that there are no default sittings that might trigger timeout which also counts for WebSocket connections.

How long does it take until getting the error ? I assume that we’re closing an idle connection since there’s nothing coming over.
If I test the underlying filters it looks like there’s no events within that block range:

wscat -c wss://mainnet.infura.io/ws/v3/…
Connected (press CTRL+C to quit)

{“jsonrpc”:“2.0”,“method”:“eth_newFilter”,“params”: [{“fromBlock”:“0x1”, “toBlock”:“0x2710”, “address”: [“0x7Be8076f4EA4A4AD08075C2508e481d6C946D12b”]}],“id”:1}
< {“jsonrpc”:“2.0”,“id”:1,“result”:“0x10ff0f0c2d2be5e2d973e10d37875b7d738b69cb4454”}
{“jsonrpc”:“2.0”,“method”:“eth_getFilterLogs”,“params”:[“0x10ff0f0c2d2be5e2d973e10d37875b7d738b69cb4454”],“id”:1}
< {“jsonrpc”:“2.0”,“id”:1,“result”:}

Btw, I suggest you update that Infura projectID since it was publicly exposed.

thanks for help!i love your community

thanks! it takes about seconds until getting error. Is there any way I can keep the connection alive?I want to keep listening to transactions

Yup, you can include a subscription for new blocks within the same wss connection, that will keep it alive: Subscribing to New Blocks · Ethereum Development with Go

I had a closer look at your code, I’m not sure that FromBlock and ToBlock work in SubscribeFilterLogs since this method via wss mainly implies getting new data not querying old blocks.
On the other hand, it’s weird that you get that session disconnected in a few seconds. Can you try to remove the FromBlock and ToBlock and subscribe to a relatively active contract to test if you do get any data over, something like this:

contractAddress := common.HexToAddress(“0x57f1887a8BF19b14fC0dF6Fd9B2acc9Af147eA85”)
query := ethereum.FilterQuery{
Addresses: []common.Address{contractAddress},
}

Thanks !

Thank you very much! It helps me a lot.
I tried to delete From and To,it can works normally