N
N
nepster-web2014-04-27 23:05:05
PHP
nepster-web, 2014-04-27 23:05:05

SockJS, node.js, what if php doesn't have time?

I play with real time and made a simple link, actually an example of what will happen in the future:

Our client:

<script src="http://cdn.sockjs.org/sockjs-0.3.min.js"></script>

<div id="content"></div>

<script type="text/javascript">

    var content = document.getElementById('content');
    
    
    var sock = new SockJS('http://127.0.0.1:9999/echo');
    
    sock.onopen = function() {
       console.log('open');
    };
    
    
    sock.onmessage = function(e) {
       content.innerHTML = e.data;
    };
    
    
    sock.onclose = function() {
       console.log('close');
    };

</script>


node.js server
var http = require('http');
var sockjs = require('sockjs');
var http = require('http');

var options = {
    host: 'portf2.ru',
    path: '/poling/server.php'
};



var echo = sockjs.createServer();


echo.on('connection', function(conn) {
    
    setInterval(function(){
                
        var req = http.get(options, function(res) {
            
          //console.log('STATUS: ' + res.statusCode);
          //console.log('HEADERS: ' + JSON.stringify(res.headers));
        
          // Buffer the body entirely for processing as a whole.
          var bodyChunks = [];
          res.on('data', function(chunk) {
            // You can process streamed parts here...
            bodyChunks.push(chunk);
          }).on('end', function() {
            var body = Buffer.concat(bodyChunks);
            var response = JSON.parse(body);
            
            conn.write(response.date);
            
          })
        });
        
        
        
        req.on('error', function(e) {
          console.log('ERROR: ' + e.message);
        });

    }, 1000);

    
    conn.on('data', function(message) {
        conn.write(message);
    });
    
    
    conn.on('close', function() {});
});

var server = http.createServer();
echo.installHandlers(server, {prefix:'/echo'});
server.listen(9999, '0.0.0.0');


And api in php:
<?php 
$objDateTime = new DateTime('NOW');
$date = $objDateTime->format('H:i:s');
$ar = ['date'=>$date];

echo json_encode($ar);


In the future, everything will work as follows:
There is a page with players waiting for the game. This is a table that must be constantly updated. Well, for example, a new player has joined, we must show it, the player left the game, we need to show it, and so on.

Actually, php has an api that returns json data about the game (how many players, which players, etc.), the task of node.js is to receive this data and send it to the client that draws the table.

Now I have a number of questions:
1) What if php does not have time? Well, for example, I indicated that in setInterval the data will be updated every second, what will happen if php does not have time to process everything in 1 second? That is, a glitch on the server, a lot of information, a lot of traffic, what will happen in general?
In the current example, I tried to tweak php by adding sleep(10), and it started doing something strange, that is, it first waits for 10 seconds, then it shows the date, updates it 5 times for 1 second and waits again for 10 seconds.
What generally happens in this case and what is fraught with?

2) For example, php will handle everything will be fine, but what will happen on heavy loads? When will there be 10,000 users on the page, for example?

3) As I know, sockets work on a tspi connection and must guarantee the delivery of data to the client. In practice, is this true, or is data loss likely?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2014-04-27
@nepster-web

What happens if JS fails? From what you described, the cost of data delivery will be ten times higher than the cost of computing. To begin with, it is worth understanding that setInterval does not guarantee that the function will be called exactly in one second, there will also be fluctuations there due to the single-threaded JS. How to deal with this, I won’t tell you right away, it all depends on the application logic.
10,000 simultaneous requests to the server (just individual requests, not via WebSockets) can bring down a small server. That's what load testing is for.
TCP guarantees no data loss, in fact, in real-time things, TCP is used only if UDP gives a lot of packet loss (for example, UDP traffic is cut or something like that). That is, when the server sends a packet, it buffers it until the client acknowledges its receipt. If the server does not receive confirmation via ICMP that the packet has been sent within a certain time, it starts sending the entire buffer again. When the buffer is full, you can no longer send new packets until at least half is free, as far as I remember. In realtime, you should not be confused by the situation with packet loss (if the data is placed in one packet, of course), since they do not have much time to carry "actual data". it's easier to send the next packet. than to resend data that is no longer relevant.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question