By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,255 Members | 2,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,255 IT Pros & Developers. It's quick & easy.

Regularly poll server for messages

P: 6
Hi there,

I'm writing a PHP web app that sends and recieves sms text messages. I need to regularly poll the server for new incoming messages. I say regularly but it would be all the time really, every second or two, 24/7. Clearly this is outside of PHP's normal usage. Do you have any suggestions about the best language to use for this kind of action?

Thanks in advance,
Paul
May 16 '07 #1
Share this Question
Share on Google+
9 Replies


100+
P: 136
Hi,

U can do this using php. U need to do a script that will poll the server for new incoming message. Now u need to call the script in cron job in Apache server. Ur server must support the cron job and ur code should be highly optimised that it shouldn't take more than 1 sec. Then u can set the cron job to run every 1 or 2 sec.

Another way of doing this job is to use a shell script, though I am not very much familiar with shell script.

Hi there,

I'm writing a PHP web app that sends and recieves sms text messages. I need to regularly poll the server for new incoming messages. I say regularly but it would be all the time really, every second or two, 24/7. Clearly this is outside of PHP's normal usage. Do you have any suggestions about the best language to use for this kind of action?

Thanks in advance,
Paul
susen
May 16 '07 #2

Motoma
Expert 2.5K+
P: 3,235
devsusan is spot on about setting up a cron job to perform this. Is there a reason why you would like to do this in PHP as opposed to some other technology?
May 16 '07 #3

P: 6
Thanks devsusan and Motoma,

I've no problem with contacting the server with PHP but it is getting this script to run automatically every second or whatever that I wasn't sure about.

The reason for me wanting to use PHP is simply that other than vb and a bit of javascript, it's the only language that I'm that confident in. I'm just a hobbyist really. I have to admit that I'm not sure what a cron job is yet! but I'll soon find out.

I'd put together an alternative solution using javascript before I visited the forum. All it does is, every few seconds, refreshes the php page that polls the server. It seems to work well but I'm pretty sure that the server would get sick of it and deny service after a while.

Thanks for your help, any other ideas are certainly appreciated.

Yours,
Paul.
May 16 '07 #4

Motoma
Expert 2.5K+
P: 3,235
cron is a service which runs on most *nix servers. If you are using a Windows server, the tool you would be looking for is AT. These are both utilities which provide scheduling for processes.

In order to run your PHP update script, you will call your PHP script from the command line: php -e /path/to/my/script.php

However, it sounds like it will be slightly more work from the way you make things sound.

This method will only work if you are keeping track of the data somewhere on the server. My intention (and devsusan's too, I am sure) was that your php script would gather information and then load that into a database. When your users go to view their messages, they would visit a different link that would then retrieve the information from the database.

If this is not the situation you are trying to obtain then I think we have had a miscommunication.
May 16 '07 #5

P: 6
Hi Motoma,

I think we are talking about the same thing. I only need something to schedule and repeat the execution of my php script which, as you say, would collect incoming information and write it to the database. Users would then have an up to date record of their messages read back out from the database by various other php pages whenever they log on to the application.

The only concern that I have is that I usually host web apps on commercial shared webspace. I'm not sure that I wil have the authority to run a cron job on it so I will probably have to set up my own webserver. I'm not concerned about setting up a machine with Linux (probably Ubuntu) and Apache but I've never tried to make my webserver available any more widely that my LAN. I think I might have a lot of reading to do!

Yours,
Paul.
May 16 '07 #6

P: 6
Great news! my hosting company say that I have privileges to run cron jobs! Woo hoo!
May 16 '07 #7

Motoma
Expert 2.5K+
P: 3,235
Glad to hear it! Come back any time if you need help.

Great news! my hosting company say that I have privileges to run cron jobs! Woo hoo!
May 16 '07 #8

100+
P: 136
thats a gr8 news. But c1pkw please take care of your script so that it should take less time to get executed.

I have experienced some problem in running cron jobs in frequency less than a minute. Once your code takes more time to execute then what happen before completing one cron thread Apache starts another cron thread. This way if the number of running cron threads increases then the site start taking more server resources. This is a problematic situation for a site if its hosted in a shared server. Many third party hosting company setup their Apache in such a way that if a site starts consuming more resources then Apache will stop that specific service for that site.

Motoma if have such experience please share with us, cause I am also interested to know if this sort of situation can be avoided or not.

susen
May 22 '07 #9

Motoma
Expert 2.5K+
P: 3,235
thats a gr8 news. But c1pkw please take care of your script so that it should take less time to get executed.

I have experienced some problem in running cron jobs in frequency less than a minute. Once your code takes more time to execute then what happen before completing one cron thread Apache starts another cron thread. This way if the number of running cron threads increases then the site start taking more server resources. This is a problematic situation for a site if its hosted in a shared server. Many third party hosting company setup their Apache in such a way that if a site starts consuming more resources then Apache will stop that specific service for that site.

Motoma if have such experience please share with us, cause I am also interested to know if this sort of situation can be avoided or not.

susen
To clarify, cron is run by the cron daemon (the crond process), not Apache.
Yes, Susen, this definitely can become a problem. Typically, the solution to a problem such as this is an object called a semaphore. In the simple case you would design a script that tests to see if a pid file exists, and if it does it will quit immediately. If it doesn't, the program will create it, run, and then delete the file. This will prevent your script from having more than one instance running at once.
May 22 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.