0

I'm aiming to display dots with javascript by their coordinates. Each person click on an image, (X,Y) will be stored in the database. On the same image will be displayed all dots, when a person is visualing the image with dots and another person will submit new dot, this last will appears because array_x and array_y tabs will be refreshed every 1s.

The question is : is it the best way in terms of using server ressources of doing that ? suppose i've 1000 persons that will participate to this study, that signify that for one person there is at least one request every 1s. Suppose that one person will spend 30s, that will be a huge amount of requests.

I am afraid to have a server breakdown due to multiple requests. Is it a way more guaranteed than this one ?

My js :

window.setInterval(loadNewPosts, 1000); //load simultaneous choice in 1 second

function loadNewPosts(){
$.ajax({
      type: "GET",
      cache: false,
      dataType: "json",
      url: "latest.php", 
      data: "current_id=" + current_id +"&nextType=" + nextType,
      success: function(data) {

        for (var i = 0; i < data['array_x'].length; i++) {
            array_x.push(data['array_x'][i]);
            array_y.push(data['array_y'][i]);
        }

    }
    });
}

my php latest.php :

$servername = "";
$username = "";
$password = "";
$dbname = "";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$current_id = intval($_GET['current_id']);
$Type = (string)$_GET['nextType'];
$sql = "SELECT * FROM `table` WHERE id > $current_id and Type='".$Type."'";
$result = mysqli_query($conn, $sql);
$array_x= [];
$array_y= [];


if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        array_push($array_x,$row["X"]);
        array_push($array_y,$row["Y"]);
   }
} else {
    echo "";
}

mysqli_close($conn);

// return the posts as a JSON object    
header('Content-Type: application/json');
$data=array(
  'array_x' => $array_x,
  'array_y' => $array_y
);
echo json_encode($data);
ranell
  • 683
  • 13
  • 29
  • This looks similar to the blog post by reddit - https://redditblog.com/2017/04/13/how-we-built-rplace/, they've used Redis to store the data. – Borys Serebrov Oct 16 '17 at 19:56
  • 1000 people issuing 3 requests per second is only 3k requests per second. Any good server should be able to handle that. You'll have more concern with how much time is spent transmitting the data than how well the server can handle it. That being said, you are talking about interactions like those used in gaming - so you might look into RTC (Real-Time-Communications) as an approach. Have the server "listening in" on the RTC chatter to save states, and sometimes refresh directly from the server. – theGleep Oct 16 '17 at 19:57
  • 1
    Your code is vulnerable to [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection) attacks. You should use prepared statements with bound parameters, via either the [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) drivers. [**This post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) has some good examples. – Alex Howansky Oct 16 '17 at 19:57

0 Answers0