AngularJS with Socket.io and MySQL data

3

The following query in cidade.php looks up how many requests are open in a particular city, which blocks a link. If it is 0 , the sum of query frees the link. If it is greater than 1 it blocks the link.

Angular Controller JS

app.controller('CityCtrl', function($rootScope, $http)
{
    $http.get('app/querys/cidade.php').success(function(data) {
      $rootScope.citys = data;
    });

Index.html

 <div ng-controller="CityCtrl">
  <div ng-repeat="city in citys">{{city.name}} : {{city.amount}}</div>
 </div>

But that's where the problem is, how can you always be checking how many requests you have open?

  • by WebSocket

  • or Soket.io.js

  • or setInterval

Which of these applications would be lighter?

    
asked by anonymous 25.10.2014 / 17:39

1 answer

4

Your question is still a little inconclusive, but I believe it is already possible to approach it so that you can draw your own conclusions.

First, Socket.IO is a NodeJS module. So, 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 do not know Socket.IO more in depth, but I believe it uses WebSocket to communicate with the client.

WebSocket maintains an active connection to the server, and therefore consumes resources constantly. So if your need is for server changes to be propagated to the client almost instantly, this is definitely your only option.

By setInterval , I am assuming that an AJAX request will be performed within the stipulated interval. One must then consider the time required to establish the TCP connection, which is at least 3x the latency between the client and the server (TCP uses Three-Way-Handshake), and in this I am not even considering the use of HTTPS protocol, which would increase much more time until the request starts to be 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 make it easier to use WebSocket in PHP, I recommend using the PHP WebSockets library. 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 PHP Builder .

The client does not have anything special: it is the same as NodeJS or PHP.

You should adapt this example to work on your hosting provider.

About slowness

I do not know if I understood correctly what you said in the comments, but if you have a request that takes 10 seconds with setInterval takes 30s, there is something very wrong there. The processing time of a request must always be in the milliseconds, which is also the order of the times for performing the handshake. So, if you have a 50ms latency to the server, and 100ms required to process the request, I would expect a minimum time of 250ms for the request completion (this is very close to the best case, inclusive).

However, 10 seconds for processing a request is an eternity, and simple switching to setInterval does not justify a 30s increase 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).

    
25.10.2014 / 20:13