简体   繁体   中英

Can multiple clients receive the same data (with AJAX) sent from one unique php, at the same time?

Lately I've been researching about technologies with which I could develop a simple chat using PHP and AJAX. Since I prefer not to install any software, I've decided to use SSE (Server Sent Events) instead of websockets.

This method allows the client who calls the php file to receive data without making a request, which is what I want, but the problem comes with the fact that every new client connecting executes the same php file again, instead of accessing the one that is already running and getting the same responses than every other client.

This means that if multiple clients calling the php file to receive data, the server will overload at some point. Knowing that, would it be possible to make every client receive the data coming only from one php executing file?

Here is what is going on:

实际发生了什么

This is okay, but as you can see, I end up having the same file executing two times, one for every client.

Here is what I want to do:

我想要的是

As you can see here, there is only one php file sending data, but multiple clients are receiving it.

How is this possible?

myFile.php

<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');

 $number = 0;

while(true){

    echo "data: $number ";
    echo "\n\n";

    $number++;

    sleep(1);
    @ob_flush();
    flush();
}
?>

client.html

<!DOCTYPE html>
<html>
<body>

<h1>Getting server updates</h1>
<div id="result"></div>

<script>
if(typeof(EventSource) !== "undefined") {
  var source = new EventSource("myFile.php");
  source.onmessage = function(event) {
    document.getElementById("result").innerHTML += event.data + " ";
  };
} else {
  document.getElementById("result").innerHTML = "Your browser doesn´t support SSE (server-sent events)";
}
</script>

</body>
</html>

Thanks in advance.

Okay so I've finally solved the question. The answer is NO, multiple clients can't receive data from one single executing SSE script, since that would mean a huge security vulnerability for any client that uses it.

Apart from that, SSE is design to lock the session of the client who is opening the scrip until it ends or closes. This way, if some session parameter changes during the execution of the SSE, it won't read it. The only way to change session parameters during the execution, is by using the session_commit() function to commit those session changes.

This also means that if the session is locked, you the client won't be able to receive any other http response until the SSE script closes. Every response will stay on pending status until that happens. This applies to ajax, whil also works with http asyn requests. At the time the SSE lock the session, the client will stop receiving ajax responses until the SSE script stops, or until the SSE script uses the session_commint() function.

This post helped me to understand how it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM