Streaming data on Http in Javascript

1

Is there a way to send data about Http, but instead of all the information at once, send one part at a time?

For example: I make an Http request for GET /produtos . This will return all my products (assuming I did not put a limit). If I have 500 products, this will make the request time-consuming, until all products are processed - and I can only process the first product information on the client when the server finishes processing the last one.

I would like to know if, following this example, there is some way for the server to send one product at a time, in a streaming type - so client and server would be working simultaneously, rather than one at a time.

The server code for me is irrelevant, but I'd like to know at least which technique is used so that I can search. can I deploy to my server, I open a new question, because this should only be about the technique used and the client code)

Clarifying: I will be using Ajax, so yes, my request will be asynchronous. What I want is to make the server and the client work at the same time, instead of the server doing all of its work before the client starts working.

In a simple request, the workflow would basically be this:

Cliente faz requisição ->
 Servidor processa 500 produtos e envia ao cliente ->
  Cliente processa os 500 produtos

And my intention is to do this:

Cliente faz requisição ->
 Servidor processa um produto e envia ao cliente (e repete o processo até que não hajam mais produtos) ->
  Cliente processa um produto assim que o recebe (e aguarda por mais até que acabem)
    
asked by anonymous 01.07.2014 / 20:17

2 answers

1

At first what you are looking for seems to be a socket that link will find an implementation example in php , another form of solving this would be paging, bring 50 records at a time, making javascript receive data and store it in a local cache until you have all the content, but display the first 50 (using the query limit), in that site you find examples of socket server and client. You can implement later on using tcp, upd and make up stream if you want.

The socket will work on the client side and server side, the server will provide the information while the client will connect on the server to receive the data. This type of data transfer can user in TCP and UPD each has its advantages and disadvantages, The server is responsible for managing the connections a server can have from 0 to N connected clients which will limit the amount of connections at the logical level is the number of ports on each ip he is listening to and the hardware level of the equipment capacity and the link. When working with socket it is necessary to have attention on how the credentials management will be done, to control the privilege and the access. The socket is something that can be implemented on any platform today you can find socket including in javascript.

Example server socket in php:

/*********function to check new order******************/
function get_new_order()
{
$con=mysql_connect(HOST, USERNAME, PASSWORD);
mysql_select_db(DATABASE,  $con);
$sql="select  OrderId from customer_order where order_Status='0' "; //0 for new order
$query=mysql_query($sql,$con);
if(mysql_num_rows(  $query)>0)
{
return true;
}
else return  false;
}
/*************************************/
/********Socket Server*********************/
set_time_limit (0);
// Set the ip and port we will listen on
$address = '127.0.0.1';
$port = 6789;
// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0); // 0 for  SQL_TCP
// Bind the socket to an address/port
socket_bind($sock, 0, $port) or die('Could not bind to address');  //0 for localhost
// Start listening for connections
socket_listen($sock);
//loop and listen
while (true) {
/* Accept incoming  requests and handle them as child processes */
$client =  socket_accept($sock);
// Read the input  from the client – 1024000 bytes
$input =  socket_read($client, 1024000);
// Strip all white  spaces from input
$output =  ereg_replace("[ \t\n\r]","",$input)."
/*********function to check new order******************/
function get_new_order()
{
$con=mysql_connect(HOST, USERNAME, PASSWORD);
mysql_select_db(DATABASE,  $con);
$sql="select  OrderId from customer_order where order_Status='0' "; //0 for new order
$query=mysql_query($sql,$con);
if(mysql_num_rows(  $query)>0)
{
return true;
}
else return  false;
}
/*************************************/
/********Socket Server*********************/
set_time_limit (0);
// Set the ip and port we will listen on
$address = '127.0.0.1';
$port = 6789;
// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0); // 0 for  SQL_TCP
// Bind the socket to an address/port
socket_bind($sock, 0, $port) or die('Could not bind to address');  //0 for localhost
// Start listening for connections
socket_listen($sock);
//loop and listen
while (true) {
/* Accept incoming  requests and handle them as child processes */
$client =  socket_accept($sock);
// Read the input  from the client – 1024000 bytes
$input =  socket_read($client, 1024000);
// Strip all white  spaces from input
$output =  ereg_replace("[ \t\n\r]","",$input)."%pre%";
$message=explode('=',$output);
if(count($message)==2)
{
if(get_new_order()) $response='NEW:1';
else  $response='NEW:0';
}
else $response='NEW:0';
// Display output  back to client
socket_write($client, $response);
socket_close($client);
}
// Close the master sockets
socket_close($sock);
"; $message=explode('=',$output); if(count($message)==2) { if(get_new_order()) $response='NEW:1'; else $response='NEW:0'; } else $response='NEW:0'; // Display output back to client socket_write($client, $response); socket_close($client); } // Close the master sockets socket_close($sock);

this link will find a complete example of the socket in php and use Another interesting example can be found in the following here

    
01.07.2014 / 20:27
0

Hello, the ideal solution for you will be asynchronous communication. In an asynchronous request, there is no synchronization between requests, so we can send several requests in parallel, where each response returns when ready.

Making some changes to the client and server you will get what you want.

Examples

Customer

$.ajax({
    url: '/Produtos',
    async: true
}).done(function(data) {
    alert(data); //Deve retornar "Tudo certo"
});

Server

<?php
sleep(10);
echo json_encode("Tudo certo!!");
?>

In this way you can send several requests, and in your case, treat on the server how many products to return, even if it has 500, return something meaningful to the client side.

By default, $.ajax works asynchronously, if you want to synchronously just set the parameter async to false .

link

I hope I have helped.

    
01.07.2014 / 20:25