Kajcioch Kajcioch - 1 year ago 106
Ajax Question

How to make PHP send request to Ajax?

How to send a PHP request to another user through Ajax?

For example, if user

presses the button, user
and nobody else has instant popup telling them about it. In my application, I'm using
to declare a logged user and I'm using my own written login system built from scratch.

I just want PHP to send request to specific user with some data.

Answer Source

As Barmar said, WebSockets is the de facto standard to handle continuous polling scenarios.

However, if your application does not need to be updated in real time and you will never have over a certain amount of users, you could use a JavaScript timer to load the resulting XML from an AJAX transaction and update the page via the DOM. Be conscious of how difficult it is to scale continuously polling applications.

Should you opt to go with continuously polling AJAX, an expected design would be along these lines:

  1. User A posts data to a PHP program on the server.
  2. The PHP program saves the user's action into a 'cache' table in a database.
  3. User B's client eventually makes an AJAX request to the server.
  4. Any items in the 'cache' table that contain that user's user ID ($_SESSION['user_id'] or something to that effect) will be returned to user B's client.
  5. User B's client alerts them that a message was received.

Keep in mind that even if you only poll every 30 seconds, with enough users you could end up DDoS-ing your own server while polling for messages.