It is still somewhat inconclusive your question, but I believe it is already possible to approach so that you can draw your own conclusions.
First, Socket.IO is a module of Nodejs. Therefore, if your application is in PHP, using it should not be an option since it would be necessary to have a Nodejs server only for this service. I don’t know Socket.IO any further, but I believe he uses Websocket to communicate with the customer.
Websocket maintains an active connection to the server, and thus constantly consuming resources. So, if your need is for the server changes to be propagated to the client almost instantaneously, no doubt this is your only option.
For setInterval
, I am assuming an AJAX request will be made within the stipulated interval. One should then consider the time needed to establish the TCP connection, which is at least 3x the latency between the client and the server (the TCP uses Three-Way-Handshake), and in this I am not if considering the use of HTTPS, which would increase much longer until the requisition start being trafficked due to SSL/TLS protocol negotiation.
In my opinion, if you want updates to occur in less than 2 seconds, I would seriously study Websocket. Otherwise (which is the vast majority of cases), setInterval
will be the most appropriate.
Example with Websocket
To facilitate the use of Websocket in PHP, I recommend using the library PHP Websockets. The skeleton of an application using this library is as follows:
#!/usr/bin/env php
<?php
require_once('./WebSockets.php');
class echoServer extends WebSocketServer {
protected function process ($user, $message) {
// processas as mensagens do cliente. No caso, é um eco apenas.
$this->send($user, $message);
}
protected function connected ($user) {
// inicialização executada quando um cliente é conectado.
}
protected function closed ($user) {
// limpeza executada quando o cliente é desconectado.
}
}
// cria o servidor na porta 9000 em todas as interfaces.
$echo = new echoServer("0.0.0.0", "9000");
try {
$echo->run();
} catch ( \Exception $e ) {
$echo->stdout($e->getMessage());
}
This example was taken from the site PHP Builder.
The client has nothing special: it is the same whether Nodejs or PHP.
You must adapt this example to work on your hosting provider.
Sluggish
I’m not sure I understand what you said in the comments, but if you have a requisition taking 10 seconds with setInterval
There’s something seriously wrong there. The processing time of a request should always be in the Mili-seconds house, which is even the order of the times for the realization of Handshake. So, if you have a 50ms latency for the server, and 100ms required to process the request, I would expect a minimum time of 250ms for completion of the request (this is very close to the best case, including).
However, 10 seconds for processing a request is an eternity, and the simple change to setInterval
doesn’t justify a 30-second raise unless the server is suffering from Ddos attacks. Consider optimizing your queries, analyzing algorithms, or even switching to a better hosting (or even a VPS or Cloud).
How regular is "always"? Once a minute? Per second? Immediately? For each of these answers there is a more efficient solution.
– Vinícius Gobbo A. de Oliveira
then depends the client will send the data to mysql... then forward update whatever if it is per second or immediately... more has to be as light as possible
– Willian