Reputation: 175
How to send a PHP request to another user through Ajax?
For example, if user A
presses the button, user B
and nobody else has instant popup telling them about it. In my application, I'm using $_SESSION
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.
Upvotes: 1
Views: 41
Reputation: 372
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:
- User
A
posts data to a PHP program on the server.- The PHP program saves the user's action into a 'cache' table in a database.
- User
B
's client eventually makes an AJAX request to the server.- 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 userB
's client.- 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.
WebSocket RFC - https://www.rfc-editor.org/rfc/rfc6455
WebSocket Basics - http://www.html5rocks.com/en/tutorials/websockets/basics/
Upvotes: 1