How can I prevent connection idle after subscribe about 1 hour?
Can you provide more information on what you are trying to do?
Iâm trying to get newBlockHeaders using web3js with infura websocket! but itâs only work about 1 hour then the connect end! How can I send pings or reconnect to infura websocket wss://mainnet.infura.io/ws/v3/my-id, thanks
Are you receiving an error or any information (i.e. response) when the websocket connection closes?
var provider = new Web3.providers.WebsocketProvider("wss://mainnet.infura.io/ws/v3/my-id", 30000) var web3 = new Web3(provider) provider.on("connect", ()=>{ web3.eth.subscribe("newBlockHeaders", (err, block) => { console.log(block) }) }) provider.on("end", ()=>{ console.log("provider end") })
thatâs my code!
Provider end log after subscribe newBlockHeaders ilde 1hour.
Can you provide the end log?
The same problem. Sometimes connection stops after one hour sometimes after 4. I try to reconnect when it stops, but finally the connection shuts down.
Also use web3.js library and NodeJs. Connection link: " wss://rinkeby.infura.io/ws"
Hi, iâm using a trick. set socket disconnect per 1hour. Then restart socket again⌠like this
function start(){
web3 = new Web3(âwss://âŚâ)
web3.on(âendâ, function(){
start()
})
setTimeout(function(){
web3.disconnect
}, 60 * 60 * 1000)
}
start()
// something like this⌠hope to help you
Hi I`m using the below code to send ping, but not working.
Connection stops after about one hour.
const websocket = new Web3.providers.WebsocketProvider(wss://mainnet.infura.io/ws/v3/api-key
);
websocket.connection._client.config.keepalive = true;
websocket.connection._client.config.keepaliveInterval = 60000;
const web3Websocket = new Web3(websocket);
Iâm having the same issue. I have tried adding a ping at minute interval, both as a websocket ping frame and as a eth_getBlockByNumber call and both failed to keep the websocket open. I get an âEOFâ error approximately an hour after I start the connection then have to reconnect. The disconnect occurs at almost exactly 1 hour 10 min intervals for me.
Is there a solution or workaround for this? I use it for long running eth_subscribe calls.
Any progress / ideas on the issue? Has anyone got it working for long-lasting ws subscriptions using go-ethereum client?
I have yet to solve the problem using go-ethereum. I reached out to the Infura folks and didnât really get an answer. I did write a test client using the gorilla websockets package with a proper ping frame and it seemed to stay open past an hour. Problem with go-ethereum is that it uses the old x/net/websocket library which doesnât support client pings. so iâve been trying to figure out a workaround that still uses the go-ethereum library since I donât want to rewrite all of that. Or submit a PR into go-ethereum that uses a library that supports ping frames.
Iâm all for any other ideas that people might have to solve this on top of go-ethereum.
I ended up using Parity client - havenât had time to debug / figure out ways to make it work with Infura node. So thatâs that.
We are experimenting the same behaviour, websocket disconnect around an hour
Any idea about the solutions of this annoying issue, really its a big problem!
We have our DAPP in Amazon AWS infrastructure, This will have something to do???
@mike your help or comments on this please!
Can you send more info on your connection, what method youâre using, etc.?
We use this to connect:
const QoriManager = require('./contracts/QoriManager.json');
let provider = new Web3.providers.WebsocketProvider('wss://ropsten.infura.io/ws/v3/XXYYZZ_OUR_ID');
let web3 = new Web3(provider);
web3.eth.net.isListening()
.then(res => console.log('Websocket infura connection:', res))
.catch(err => console.log('No connection', err));
const contract = new web3.eth.Contract(QoriManager.abi, contractAddress);
Our DAPP is in Amazon AWS LightSail server with 2 GB RAM, 1 vCPU, 60 GB SSD.
If you need some other details let me know, thanks for your kindly response.
JJMiranda
The isListening() method returns whether a node is listening for peers, this uses the method, net_listening which is supported by our HTTPS endpoint if you need that information.
Can you try connecting using a different method to test your WSS connection?
Is it not possible to add some kind of a heartbeat so we dont have to reconnect unnecessarily after an hour, which we will just end up doing? It kind of defeats the purpose of disconnecting users after an hour (due to idle sessions), as we will just reconnect.
In general, we donât encourage having idle connections. It takes up resources that are not being used for anything. The idle connection also can be seen as a indication of something going wrong. The backend ethereum clients have stability issues related to subscription and filters where the node is âhealthyâ but subscriptions and filters stop responding. The only thing you can do at that point, is to reconnect. We realize this is a problem for our users and are in the process of upgrading our subscriptions to be less client dependent. The ânewHeadsâ subscription has been upgraded so you shouldnât see a timeout issue with newHeads, and are also in the process of upgrading our log subscription.