Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is nothing wrong with using PHP's sleep, if you understand that PHP is single threaded and it will pause execution of the rest of the script.</p> <p>This is definitely not the "Right Thing" to do. What will happen when you start connecting more clients is that you will have many apache PHP threads which will all be sleeping waiting for the same event. You will run out of resources very quickly. What you are doing is essentially long polling. sleep or no sleep, its still bad.</p> <p>One of the better approaches would be to make this system more event based. When an event comes in, the data gets pushed out to all the clients. Dependent on what technology you have available to you this will be very flexible and will only incur the network latency. Something like WebSockets fits very well here.</p> <p>Since you are on a µC and can't do websockets easily, best thing to do would be to open a persistent TCP connection to the server, and just send data down it.</p> <p>The next best thing would be to make the script execute fast, by caching results, say in <a href="http://en.wikipedia.org/wiki/Memcached" rel="nofollow">memcached</a>. Memcached is essentially an in memory key value store. You would use your clientId or something that identifies a client uniquely as the key. The process would be something like this:</p> <p>Results data is updated from some external source and is added to the database. If the data is per client in nature then delete (invalidate) that client's key in memcache so when it requests the results it is forced to go directly to the database. If the results aren't per client when you record them, run through all the clients currently connected and invalidate their caches individually. This process will be fast as everything is in RAM.</p> <p>When the clients request results, the script should look first in memcache using the clientID as the key, if it isn't found query the database and put the result into memcache for later.</p> <p>I don't have any good memcache tutorials on hand, but a quick google search turned up this one, which doesn't suck: <a href="http://www.stevenmcmillan.co.uk/blog/2010/php5-memcached-example/" rel="nofollow">http://www.stevenmcmillan.co.uk/blog/2010/php5-memcached-example/</a></p> <p>I would also advise you to reconsider using raw TCP sockets. You are really missing out, as you can make a realtime connection between your clients and the server, and your code overall can be simpler. If your hosting doesn't allow it, get a better hosting provider. There are so many cheap ones, if you can't afford ~$5 a month, I worry about your nutrition. Two that I have atm are RamNode and DigitalOcean.</p> <p>Other than that you really need to describe your situation in a bit more detail.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload