Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
I have a simulated real time app. I send a csv file to rabbitmq. And rabbitmq sends back the queues. In my client app, ag grid cannot keep with the speed of the received messages. I am using websocket.
I wonder how can I control the speed of those messages, either in rabbitmq, node js, websocket in the server or client? I want to control the speed of each response, e.g. To send/receive every response every second, minute, etc.
Thanks in advance
The solution was to control the flow of the queues. Use prefetch to indicate how many queues you want to consume at each time, and add a set time to indicate how often
channel.prefetch(1);
channel.consume(
queue,
msg => {
if (msg !== null) {
if(msg) {
setTimeout(() => {
console.log(msg.content.toString());
sendQueuesToWebSocket(msg.content.toString());
channel.ack(msg);
}, 50)
}
} else {
console.log('Consumer cancelled by server');
}
},
{
noAck: false,
}
);
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last month.
Improve this question
I have websocket connection between client(browser) & server.
Problem is, sometimes I request a data through websocket.
But since websocket works like an event, I cannot manage my code efficiently.
I can send websocket message in a function but then I have to listen and handle it on the event listener.
Below is example.
const ws = new WebSocket("ws://whatever");
function handleClickBtn() {
ws.send('request something');
// cannot get response here
}
ws.onmessage = function (event) {
console.log(`response comes here: ${event.data}`);
}
It seems harder to maintain my code since you have to jump between request and response.
If I've used REST API, it would been easier with async/await syntax.
What kind of technique can be used here?
You'll need to think in async terms and indeed handle many potential in-flight requests at the same time. To be able to do this, you will need to be able to relate the incoming responses to requests you sent previously. For example, you could add random ids to your requests, and the response to it will contain the same id. E.g.:
Request:
[82512903521, "fetch-data", "param1", "param2"]
Response:
[82512903521, {"some": "returned", "data": ...}]
Then internally you keep a list of request ids you sent, together with an appropriate callback function which you'll trigger when the corresponding response returns. E.g. something like:
const requests = {};
function request(callback, ...params) {
const id = randomId();
ws.send([id, ...params])
requests[id] = callback;
}
ws.onmessage = function (event) {
const [id, data] = event.data;
requests[id](data);
}
There are existing protocols and libraries that implement this kind of this, for example WAMP.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have another discord.js query which involves custom statuses. I want my bot's custom status to be "Being Used In # Guilds/Servers" where # is the number of guilds the bot will be in.
Note that by "custom status" I don't mean a Playing status:
I mean a custom status (without "Playing"):
(Yes, that is OG Clyde, because I had my account since the Christmas Vacation 2020.)
So some answers say that Discord.js V12 only has Playing Statuses, not Custom Statuses. Now I had a conversation with #jabaa (in the comments) which said that I should share my own research and code, or else people will downvote. But I cannot share, because I don't know which code to use. I know one to display memberCount in the status, but not guild numbers.
Alright, I am using this in my bot too: here is the code
//...
client.on('ready', () => {
//...
client.user.setActivity('othertext' + client.guilds.cache.size, {type : 'PLAYING'})
}
client.on('guildCreate', guild => {
//same code as 'ready'
})
client.on('guildDelete', guild => {
//also the same code as 'ready'
})
Now this was from my human memory but this is a start, just modify it with any errors you may have, hopefully there are none.
NOTE: If you are just putting the number of guilds, for some reason, make sure the add the '' after the client.guilds.cache.size, otherwise you will get an error saying it got a number but expected string
DJS does not currently have support for Custom Statuses the same way users do, so this is not possible at this time.
As you can see here,
Bots cannot set a CUSTOM_STATUS, it is only for custom statuses
received from users
ClientUser.setStatus() takes a PresenceData type as an argument, which in turn has an object activity and its property type that can be either PLAYING, STREAMING, LISTENING, WATCHING or COMPETING
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm basically calling data from an API and handling the data loading with a loader in my html so when my data array.length is not undefined, the data is shown.
But when there is no data it loads indefinitely...
What I want to do is set a timeout if there is no data, something like 10 seconds in order to display a message like "No data found"
Here is my Vue.js template:
If you search how to print a message after 10 seconds, there is this code:
function stateChange(newState) {
setTimeout('', 10000); //10sec * 1000
if(newState == -1) {
alert('NO DATA');
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am currently using an API to create a React Redux application. My problem is I am confused about how to efficiently use the data as the API has more than 14470 "pages" to navigate around. My main goal is to display certain items in the API based on key value pair. For example I would say I want to display a category in the API based on a certain key value pair such as "highestRating" and want to map through the API to find out the five items with the highestRating, how would I be able to do this efficiently?
What I have tried so far is looping to get the entire API available to me but then I get stuck with my current task at hand.
export const fetchHighestRating = () => async dispatch => {
let data = [];
let morePagesAvailable = true;
let currentPage = 0;
while (morePagesAvailable) {
currentPage++;
const response = await api.get(
`/api?page%5Blimit%5D=10&page%5Boffset%5D=${currentPage}`
);
data = [...data , response];
morePagesAvailable = currentPage < 17471;
}
dispatch({ type: FETCH_HIGHEST, payload: data });
};
This is not a javascript problem, it is database. You should run query directly in database to test response speed and decide what to do next, including:
Sharding
Indexing
Optimize config of DB
....
Any above required research in type of DB you are using, the current situation of DB so there is no exactly answer now!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I will need to pull data from many datasources - this is a time-consuming process.
If user sends the form the program pulls data from 7 tables ( using ajax )
I would like to provide an information what is actually going on ,for example "Pulling data from table1" , "Pulling data from table2" and so on.
but have no idea how to do this - Is there a good standard for this kind of task ?
I am using python/django as a backend
Thanks in advance
You can use the XHR onprogress function to stream data in JavaScript. The function gets called periodically whenever data is flushed from the server. So use sys.stdout.flush() in Python to force a flush whenever you want to display the current status.
test.py
import sys
import time
for x in range(5):
print(x)
sys.stdout.flush()
time.sleep(1)
test.html
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<button id="btn">Begin Stream</button>
<div id="app"></div>
<script>
$(function() {
$("#btn").on("click", () => {
$.ajax('test.py', {
xhrFields: {
onprogress: (e) => {
$("#app").html(e.currentTarget.response);
}
}
})
.done((data) => {
$("#app").html(`DONE: ${data}`);
})
.fail((data) => {
$("#app").html(`ERROR: ${data}`);
});
});
});
</script>