'ActionCable stops working with no output - Javascript
I am using the following code in order to get the updates from a server:
const { ActionCable } = require('@sorare/actioncable');
const cable = new ActionCable({
headers: {
// 'Authorization': `Bearer <YourJWTorOAuthToken>`,
// 'APIKEY': '<YourOptionalAPIKey>'
}
});
var query = "aCardWasUpdated(rarities: [limited, rare, super_rare, unique]) " +
"{slug, id, onSale, rarity, liveSingleSaleOffer{price, blockchainId, endDate, open}, player{displayName}}";
cable.subscribe(query, {
connected() {
console.log(new Date().toLocaleString());
console.log("connected");
},
disconnected(error) {
console.log(new Date().toLocaleString());
console.log("disconnected", error);
},
rejected(error) {
console.log(new Date().toLocaleString());
console.log("rejected", error);
},
received(data) {
console.log(new Date().toLocaleString());
if (data?.result?.errors?.length > 0) {
console.log('error', data?.result?.errors);
return;
}
const aCardWasUpdated = data?.result?.data?.aCardWasUpdated;
if (!aCardWasUpdated) {
console.log("A card was not updated");
return;
}
console.log("card");
}
});
That code works fine for some minutes, but after a random amount of time, it stops printing anything.
I am running the code in a screen
terminal using node code.js
(an then detaching the terminal with ctrl+a+d); but I get the same output using a "normal" terminal.
What could be the issue? Is there a case inside the subscribe
function that I am not considering?
Solution 1:[1]
First of all ,i recommend using Socket.io
for working with sockets rather than sorare
.
Your code is correct and if there are no logs related to 'disconnect'
or 'rejected'
events ,there is no problem with your connection either.
maybe there is problem with your target server's event emitting and your server is not receiving any events after a while.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Moein moeinnia |