-3

Following is a pseudocode of what I am doing

Javascript

1) Make XMLHttpRequest GET Request to get data from a server

2) Parse the response received and and make a string of the desired format

3) Make XMLHttpRequest POST Request to send this new string to a PHP script

PHP

4) Parse this received string

5) Send this string to a mqtt broker

For now, my Javascript code is written in a HTML file and I run this entire process in a webpage. In order to run this continuously, I have to keep this webpage open in my browser all the time(I cannot shut my PC at all). But I do not want to keep my system up and running all the time. So I am thinking of running this entire process on a server using crontab. But these are the following issues I am encountering.

1) To run my JS on the server, I used NodeJS to run it. But NodeJS has its own XMLHttpRequest alternative, which I tried, but cannot even get it to do my first step.

My doubt is, even if I get NodeJS to successfully do http requests, can NodeJS run my the entire process since it also involves PHP?

2) I thought of changing my entire JS code to a PHP code, because crontab cam run PHP scripts.

My doubt is, can I do the XMLHttpRequest in PHP like I am doing in the JS?

Learner
  • 3
  • 6

2 Answers2

0

You can cron PhantomJS to load up your web page every hour.

Ross
  • 14,266
  • 12
  • 60
  • 91
  • I used `setInterval` in JS to run this entire process every 30 min. My problem is, using this, I had to keep the web page open in the browser all the time, because of which I cannot shut my system down at any time. – Learner Nov 11 '16 at 15:37
  • You can use PhantomJS to load your webpage then quit. Then you can crontab to schedule running PhantomJS every 30 mins. Architecturally, your approach is flawed. But PhantomJS is the way to load and HTML page an execute any JavaScript on it without opening a browser windowif that's what you are looking to do. – Ross Nov 11 '16 at 15:42
  • Can you tell me where all to do I need to improve my flaws? The JS and HTML page was a quick fix, and that's why I am trying to improve now – Learner Nov 11 '16 at 15:47
  • Your not flawed ;-) But it sounds like you are using to many different things. You likely should only using only PHP. But hey, if it works and you are learning, doesn't matter – Ross Nov 11 '16 at 16:11
  • I know I was using many things. I had to get it done in like 5 days, so didn't think too much when I got it running(And yes, I worked as I wanted :P) But now I want to correct it.. – Learner Nov 11 '16 at 16:22
0

To go with option 1) you can use the https://www.npmjs.com/package/xhr2 package, which implements the W3C XMLHttpRequest specification on top of the node.js APIs.

Crontab is also able to run any script, so you might trigger the node.js script with crontab as well.

DevDig
  • 1,018
  • 13
  • 19
  • So, I should just implement my `XMLHttpRequest` using the one you mentioned, and then can run this JS file on crontab like this - 30 * * * * /path/to/my .js file ? – Learner Nov 11 '16 at 15:44
  • yes, using `30 * * * * /usr/local/bin/node /path/to/script.js` – DevDig Nov 11 '16 at 15:49