What is the best way to create a socket with multiple connections that will perform an action in another API

Asked

Viewed 64 times

1

I am creating a webservice with PHP, to receive TCP connections, so I opened a socket that receives multiple connections. But when a device connects to the server I will have to search another third party API (via HTTP), and this API returns the data in about 3 seconds. (That’s quite the data volume) So that it doesn’t work as a queue the only way I found to do this is with threads, but I don’t know if it’s possible to return the data to the client connected to the server via thread, I wonder if there’s a better way to do this.

    <?php
error_reporting(E_ALL);

/* Allow the script to hang around waiting for connections. */
set_time_limit(0);

/* Turn on implicit output flushing so we see what we're getting as it comes in. */
ob_implicit_flush();

$address = '127.0.0.1';

$port = 10000;

// create a streaming socket, of type TCP/IP
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);

socket_bind($sock, $address, $port);

socket_listen($sock);

// create a list of all the clients that will be connected to us..
// add the listening socket to this list
$clients = array($sock);

while (true)
{
    // create a copy, so $clients doesn't get modified by socket_select()
    $read = $clients;
    $write = null;
    $except = null;

    // get a list of all the clients that have data to be read from
    // if there are no clients with data, go to next iteration
    if (socket_select($read, $write, $except, 0) < 1)
        continue;

    // check if there is a client trying to connect
    if (in_array($sock, $read))
    {
        $clients[] = $newsock = socket_accept($sock);

        socket_getpeername($newsock, $ip, $port);
        echo "New client connected: {$ip}\n";

        $key = array_search($sock, $read);
        unset($read[$key]);
    }

    // loop through all the clients that have data to read from
    foreach ($read as $read_sock)
    {
        // read until newline or 1024 bytes
        // socket_read while show errors when the client is disconnected, so silence the error messages
        $data = @socket_read($read_sock, 4096, PHP_BINARY_READ);

        // check if the client is disconnected
        if ($data === false)
        {
            // remove client for $clients array
            $key = array_search($read_sock, $clients);
            unset($clients[$key]);
            echo "client disconnected.\n";
            continue;
        }

        $data = trim($data);

        if (!empty($data))
        {
            $msg = json_decode($data,true); 

            if ($msg['requisicao'] == "01"){
                $lista_debitos = new ListaDebitos($read_sock,$msg);
                $lista_debitos->start();

                $key = array_search($read_sock, $clients);
                unset($clients[$key]);
            }   
        }

    } // end of reading foreach
}

// close the listening socket
socket_close($sock);
  • These data shall not be fractionated?

  • No, but even if they could, it would still work as a queue and the problem would persist, because the prediction is that the webservice will receive many connections in a few moments.

  • And how are you developing this web service? Can you post the code?

  • You are talking about socket or websocket , the most totally different types of technologies are similar, and the approaches are tabem to work

  • I entered the code @Filipel.Constante.

  • @Gabrielsousa Socket herself.

Show 1 more comment
No answers

Browser other questions tagged

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