In my Angular application I need to make a http call in regular intervals of time (say every y mins) as long as the page is active and render the response on the screen.
Is it good to use NodeJS.Timer object to implement setTimeout()
or use Observable.timer().
Observable.timer() is a lot more useful for Angular
for example:
const tick3$ = Observable.timer(100, 60000);
If you just want to execute a regular function every 10 minutes, you can just use plain Javascript for that:
var timer = setInterval(function() {
// put your function code here
}, 10 * 60 * 60 * 1000);
Or, if the function is already separately defined:
var timer = setInterval(myFunction, 10 * 60 * 60 * 1000);
Is it good to use node js timer object to implement setTimeout()
It's really not clear what you mean by this question. A nodejs timer object would be running things on the nodejs server, not on the client. Since you said you want to run something in the browser web page, I presume you don't want a timer on nodejs to do that, but rather a timer in the web page as my example above shows.
Angular also has a wrapper around setInterval() called $interval() and it adds a few more features including a count and some angular specific functionality related to updating the view if the data changes. If you want those angular specific features, you can use $interval(). If not, then you can use either.
Related
I've implemented a web scraper with Nodejs, cheerio and request-promise that scrapes an endpoint (basic html page) and return certain information. The content of the page I'm crawling differs based on a parameter at the end of the url (http://some-url.com?value=12345 where 12345 is my dynamic value).
I need this crawler to work every x minutes and crawl multiple pages, and to do that I've set a cronjob using Google Cloud Scheduler. (I'm fetching the dynamic values I need from Firebase).
There could be more than 50 different values for which I'd need to crawl the specific page, but I would like to ease the load with which I'm sending the requests so the server doesn't choke. To accomplish this, I've tried to add a delay
1) using setTimeout
2) using setInterval
3) using a custom sleep implementation:
const sleep = require('util').promisify(setTimeout);
All 3 of these methods work locally; all of the requests are made with y seconds delay as intended.
But when tried with Firebase Cloud Functions and Google Cloud Scheduler
1) not all of the requests are sent
2) the delay is NOT consistent (some requests fire with the proper delay, then there are no requests made for a while and other requests are sent with a major delay)
I've tried many things but I wasn't able to solve this problem.
I was wondering if anyone could suggest a different theoretical approach or a certain library etc. I can take for this scenario, since the one I have now doesn't seem to work as I intended. I'm adding one of the approaches that locally work below.
Cheers!
courseDataRefArray.forEach(async (dataRefObject: CourseDataRef, index: number) => {
console.log(`Foreach index = ${index} -- Hello StackOverflow`);
setTimeout(async () => {
console.log(`Index in setTimeout = ${index} -- Hello StackOverflow`);
await CourseUtil.initiateJobForCourse(dataRefObject.ref, dataRefObject.data);
}, 2000 * index);
});
(Note: I can provide more code samples if necessary; but it's mostly following a loop & async/await & setTimeout pattern, and since it works locally I'm assuming that's not the main problem.)
I have a list of URLs in DB. I want to periodically check if those URLs are alive or not.
So I create a long running service which run infinite loop, each iteration:
Query database to get list of urls
For each URL, make request to check if it is alive or not
Please guide me how to implement that service.
I looked at Bull and Kue, but they seem not support infinite loop service?
You can use something very simple like setInterval() to have your task repeat x amount of times.
var testUrls = function(){
//do your magic of connecting to the DB and checking urls.
}
setInterval(testUrls, 60000);
The above code snippet will call your function testUrls every minute.
Or if you need more control over the scheduling you can use a npm package like cron.
You can use node-schedule
Its very simple
var schedule = require('node-schedule');
var j = schedule.scheduleJob('42 * * * *', function(){
console.log('The answer to life, the universe, and everything!');
});
Is it possible to use RedisMqServer as simple task scheduler? For example to publish a message and execute it in feature (at specific time)?
RedisMQ doesn't provide any explicit support for delayed messages itself, but you could use simulate it with a Timer, e.g:
var sendInMs = 30 * 1000;
new System.Threading.Timer(msg => mqClient.Publish(msg),
msg,
sendInMs,
Timeout.Infinite);
I would like to know, how to use javascript to achieve my use case,
My app receives a post request, then it incr memcache key, then it publish the increased value straightaway to users(mobile app) using third party API.
Eg. first requst value become 1, publish 1.
second request value become 2, publish 2 ...
It works fine with requests less than 2k within 30 secs.
If the requests number goes up to 10k, users(mobile app) may receive too many messages from publisher(battery consuming)
So I have to the throttle publishing calls, instead of publishing per request, I want to publish the value every second. In 1 second, the value can be 1, then publish 1. In 2 second then value can be 100, then publish 100. So that I saved 99 publish calls.
When requests are not coming anymore, I don't want a worker keep running every second.
Each time it increments, cache the new value to a global variable and post it to clients using setInterval. Here is a simple example:
var key = 0;
// Update the cache to the present
// value on application start
memcache.get('key', updateKey);
// Handle increment request and
// save the new value
app.post('/post', function(req, res){
memcache.incr('key', updateKey);
});
// Update the cached key
function updateKey(err, val){
key = val;
}
// Publish to clients once
// a second
function publish(){
clients.emit(key);
}
setInterval(publish, 1000);
Starting and stopping this routine is a little more involved and may depend on how you're serving requests / incrementing the value.
Take a look at node-rate-limiter
You can implement it in a number of ways to solve your problem...
I am developing an simple Chat Application, I can view the updated data when I click on the REFRESH Button, BUT can I refresh the data at a regular interval from the Server (as my Chat is getting Stored in the Database remotely)
Thanks in advance.
Use DelayedTask class of Sencha Touch:
//create the delayed task instance with our callback
var task = Ext.create('Ext.util.DelayedTask', function() {
//load the list's store here. The list will be automatically updated
listComp.getStore().load(); // Assuming your list component is "listComp"
listComp.refresh();
// The task will be called after each 10000 ms
task.delay(10000);
}, this);
//The function will start after 0 milliseconds
//so we want to start instantly at first
task.delay(0);
//to stop the task, just call the cancel method
//task.cancel();
This should work for your case.
You just need to call your refresh() function at a regular right ?
So you just need to add a setTimeout("refresh()", 1000); at the end of your refresh() function. Then you just need to call it when your app startup.
Hope this helps
Maybe you'll be interested in socket server connection in your chat application. Server will notify your clients whenever server data changes. Take a look at http://socket.io/
please look at this.
It will call the function for every (time you set).
http://www.sencha.com/forum/showthread.php?194202-Autorefresh-List-with-DelayedTask