cosmjs
cosmjs copied to clipboard
WebsocketClient seems to disconnect/timeout after a few hours.
I am currently running a WebsocketClient to get NewBlock events.
The WebSocket fine works when the script is just ran. But after several hours, it seems like the WebSocket gets disconnected/timeout and no more updates are sent to the listener. The strange thing is that the error and complete callback does not get triggered.
const client = new WebsocketClient(rpcEndpoint);
const readable = client.listen({
"jsonrpc": "2.0",
"method": "subscribe",
"id": "1",
"params": {
"query": "tm.event='NewBlock'"
}
});
var subscription: Subscription;
subscription = readable.subscribe({
next: i => NewEvent(i),
error: err => console.error(err),
complete: () => console.log('completed'),
});
Is there something wrong?
I can confirm this issue. No errors or warnings, program just keeps running without any updates from server (next() is not triggering) I also noticed that client on some public rpcs is dying for several minutes, but on the others it working for several hours
const wsClient = new WebsocketClient(
wsEndpoint,
(err) => console.log("ws client error " + JSON.stringify(err)));
let stream = wsClient.listen({
jsonrpc: "2.0",
method: "subscribe",
id: 0,
params: {
query: "tm.event='NewBlockHeader'"
}
});
stream.addListener({
complete: () => {
console.log("complete");
},
error: (err) => {
console.log("error: " + JSON.stringify(err))
},
next: (newtx) => {
try {
let newHeight = newtx?.data?.value?.header?.height;
processNewHeight(network, newHeight);
} catch (err) {
console.log(JSON.stringify(err));
}
}
});
This is happening to me as well. Sometimes it'll stay connected for days, but it'll eventually stop and not fix itself until restarted. There aren't any error message, or warnings we could act on to re-connect. I am using javascript ws.
Can you share full implementation for this ? I need to listen for events or transactions from smart contracts.