P. Avery P. Avery - 1 year ago 61
PHP Question

Persisting server-side data across an array of clients

I have a PHP website and a SQL database. Users will be able to update this data with a mobile phone. I would like all users to receive these updates. How would I do this?

I could simply use ajax and a timer to have each client request updates at a given interval.

Is there a way to set a callback of sorts so that a service on the server calls a list of method instances? This would prevent unnecessary requests and transfer of data...

Answer Source

If you don't want to use a third-party solution like the one @Alexandre Martin suggested, you could use Server Sent Events. The flow is as follows:

  1. Each user subscribes to a channel (think of it as registering an event listener on a connection). On the server side, a script is running continuously to track for changes
  2. A user posts an update to the server.
  3. The script notices the update and issues an event
  4. All clients who subscribed to the channel receive the event, read its data property and update their local models.

See this introduction, with sample PHP implementation.

You could also use Web Sockets. Here is a comparison of the two. Before implementing either, consider browser compatibility.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download