Regularly poll server for messages

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • c1pkw
    New Member
    • May 2007
    • 6

    Regularly poll server for messages

    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
  • devsusen
    New Member
    • Feb 2007
    • 136

    #2
    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.

    Originally posted by c1pkw
    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

    Comment

    • Motoma
      Recognized Expert Specialist
      • Jan 2007
      • 3236

      #3
      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?

      Comment

      • c1pkw
        New Member
        • May 2007
        • 6

        #4
        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.

        Comment

        • Motoma
          Recognized Expert Specialist
          • Jan 2007
          • 3236

          #5
          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 miscommunicatio n.

          Comment

          • c1pkw
            New Member
            • May 2007
            • 6

            #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.

            Comment

            • c1pkw
              New Member
              • May 2007
              • 6

              #7
              Great news! my hosting company say that I have privileges to run cron jobs! Woo hoo!

              Comment

              • Motoma
                Recognized Expert Specialist
                • Jan 2007
                • 3236

                #8
                Glad to hear it! Come back any time if you need help.

                Originally posted by c1pkw
                Great news! my hosting company say that I have privileges to run cron jobs! Woo hoo!

                Comment

                • devsusen
                  New Member
                  • Feb 2007
                  • 136

                  #9
                  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

                  Comment

                  • Motoma
                    Recognized Expert Specialist
                    • Jan 2007
                    • 3236

                    #10
                    Originally posted by devsusen
                    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.

                    Comment

                    Working...