Angularjs with Socket.io and Mysql data

Asked

Viewed 785 times

3

The following query in cidade.php search how many requests are open in a given city, which blocks a link. If 0, the sum of query releases the link. If it is larger than 1 blocks the link.

Controller Angularjs

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 lies, how to always be checking how many requests you have open?

  • by Websocket

  • or Soket.io.js

  • or setInterval

Which of these applications would be lighter?

  • 2

    How regular is "always"? Once a minute? Per second? Immediately? For each of these answers there is a more efficient solution.

  • 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

1 answer

4


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).

  • Vinicio as I use the Websocket all that axei seemed to use Node.js I have a server in uolhost sera that has how to implement it there? or I’m looking for the wrong content?

  • face to with setinterval or same truth and 3x the request in script ta 10 sec in client was 30sec... as mecho with websocket can show me something?

  • Most people implement Websocket with Nodejs, but it is possible to implement it in other languages, including PHP. I will update the answer with a very short example.

  • 1

    Top Vinicios :D already managed to rotate hehe

  • Cool! Happy birthday!

  • congratulations nothing thank you very much :D

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.