How the websocket works in php

7

I have this doubt, for example, I have a server written in PHP.

When a user A connects to that server, the server performs an action that takes 4 seconds for example, if before that time a new user B connects, it will have to wait for the user to execute A finish before you run? or how it works? I'm lost.

I do not know if I understood myself.

    
asked by anonymous 23.12.2015 / 23:52

1 answer

10

WebSocket

In websockets the process is very similar to normal HTTP, but the difference is that we do not close the connection and we do not need to request the server again to know when something new exists, the event inside ws receives signals or data directly from the server .

If you write a basic websocket it will probably be synchronous, if the user A sends something too long, the process will probably be "stopped" until finished, but at the time of sending the other users to the long message from the user A all will probably receive at the same time.

However when reading about ratchetphp/Ratchet , more specifically this message:

  • link

    It states that Ratchet is asynchronous and indicates two links. If you look at the repository you will see the following description:

      

    Asynchronous socket server

Also in the link indicated in the github comment, there is the option to use "children process":

  • link )

    And there in the repository is the following description:

      

    Asynchronous ChildProcess (en: Asynchronous child process)

    Example usage:

    $loop = React\EventLoop\Factory::create();
    
    $process = new React\ChildProcess\Process('echo foo');
    
    $process->on('exit', function($exitCode, $termSignal) {
        // ...
    });
    
    $loop->addTimer(0.001, function($timer) use ($process) {
        $process->start($timer->getLoop());
    
        $process->stdout->on('data', function($output) {
            // ...
        });
    });
    
    $loop->run();
    

In short: So how websockets work in PHP will depend on how the code was written, reactphp is already asynchronous and still supports child-process , so using the library well, the user B will not have to wait for the A user to be able to interact with the websocket.

A socket within php works much like a normal socket, we usually use fsockopen or stream , or even other libraries as curl . >

PHP is written in C ++, so it will probably depend on what php extension you are referring to. But the basics is that it connects through a socket, it will depend on you as a developer to define, for example to make an HTTP request it is necessary to make a request for TCP.

HTTP Servers

But if what you want to understand is how client request management works for a "normal" server that uses PHP (production server), I have to tell you that it is not the php that manages this, a href="https://httpd.apache.org"> Apache or Nginx or lightTPD , which are HTTP servers.

  • Note that PHP is the backend of the HTTP server, as opposed to PHP, you can use Python or other interpreters / compilers, so PHP responds to the HTTP server and this same server responds to the client (user's browser).

Both Apache and other servers divide requests by childs (which are a kind of "thread"), in apache the process is synchronous but still has several childs which allows several people to access without having to wait the other. You can set limits on Apache itself, remember the larger the limit the more memory you will need.

The Ngnix and lightTPD servers are said to be asynchronous, which probably divides the childs differently and allows a larger number of users to access at the same time, but still use these childs . >

The childs are like threads or subprocesses, connections are likely to be split between these subprocesses and returned later.

Each type of server has its own way of managing the connections and returning the response of requests, this can also vary with the type of server, such as a server that uses a Linux kernel or a Windows-based server.

But in the basics the user B does not have to wait for the A to finish its execution.

    
24.12.2015 / 00:20
___ ___ erkimt I extend an abstract class or concrete? ______ qstntxt ___

When I need to extend a class, following the concept of Object Orientation, should I extend my code from an abstract or non-abstract class? What is the best practice to join?

    
______ azszpr89237 ___

There is no better option, you extend the class you need to extend. Whether it is abstract or not, it makes no difference to its code other than the fact that an abstract will possibly have unimplemented methods and its new class will have the obligation to create an implementation for all abstract methods contained in the class. optional).

If you want to know if it's better to create an abstract class or not, then it depends on what you want. An abstract class can not be instantiated. It is designed to be used as a template for other classes. Non-abstract can be used as models but can also be instantiated directly. You just make it an abstract class if you want to ban its instantiation (which is bound, if it is incomplete).

Of course, if the class has methods without implementation, they act as contracts for the derived classes to follow, that is, they function as if it were an interface, then the class must necessarily be abstract. Unable to instantiate classes with methods without implementation.

For example. If you have a class %code% and the derivatives of it %code% and %code% . Probably you do not need and maybe can not instantiate only the %code% . It is probably incomplete. You just created it to support the two (who knows other) derivatives I mentioned. It is almost an interface, but probably has variables and some methods with implementation. So %code% should probably be abstract.

Remembering that you can only inherit from a class. Abstract or not. Interface can several.

    
___
What is the pointer-events property for?