How websocket works in php

Asked

Viewed 10,998 times

10

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

When a user A connects to this server the server performs an action that takes 4 seconds for example, if before that deadline a new user B connect, it will have to wait for user execution A finish before making your run? or how it works? I’m lost.

I don’t know if I made myself clear..

1 answer

15


Websocket

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

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

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

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

  • https://github.com/reactphp/child-process)

    And there in the repository is the following description:

    Asynchronous Childprocess (en: Process asynchronous children)

    Example of use:

    $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 the websockets work in PHP will depend on how the code was written, the reactphp is already asynchronous and still supports child-process, then using well the library, the user B will not need to wait for the user A to interact with the websocket.

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

PHP is written in C++, so it probably depends on which php extension you refer to. But the basic thing is it connects by a socket, it will depend on you as a developer set, for example to make an HTTP request it is necessary to make a request by TCP.


HTTP servers

But if what you want to understand is how it works to manage client requests to a "normal" server that uses PHP (served production), I must tell you that it is not php that manages this, but rather the Apache or Nginx or lighttpd, which are HTTP servers.

  • Note that PHP is the back-end of the HTTP server, as well as instead of 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 share requests by childs (which are a kind of "thread"), in apache the process is synchronous but still has several Children which allows several people to access without needing one to wait for the other. You can set limits on Apache itself, remember the higher the limit the more memory will be needed.

On the other hand, Ngnix and lighttpd servers claim to be asynchronous, which probably divides the childs and allows a greater number of users to access at the same time, but it is still used these childs.

Childs are like threads or subprocesses, the connections are probably divided between these subprocesses and returned later.

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

But in the basic user B no need to wait for the A finish his execution.

  • So every connection or request that socket opens a thread for processing the same? (could help me chat on another question?)

  • Yes! But in my opinion this description applies more to normal http requests; websocket is already a whole different world. That your websocket server should be 100% PHP not? If it is he must have something similar to a "message loop" that receives and processes the requests; depending on how it was done he will only process one request at a time.

  • @Perrywerneck use Ratcher to handle the sockets, it is asynchronous. I do not know if this interferes.

  • @user3163662 I didn’t know this Ratchet (good tip!) but searching a little I found a question about using multiple threads in it: http://stackoverflow.com/questions/25704251/using-php-pthreads-with-ratchet-websocket

  • @user3163662 edited the answer, I think now it became clear to me your question.

  • @Guilhermenascimento assuming I used a common server, without being asynchronous, and the user A was performing a 1 minute process, so the user B would be obliged to wait that one minute, right? just to confirm.

  • 1

    @user3163662 common server do you refer to Apache, Ngnix or lighttpd? If it is you won’t need it, because everyone uses it childs. If you are talking about a very simple websocket, then it will depend on how it was written, assuming it is synchronous and the process on the back-end side is taking 1 minute you will probably have to wait. It was as I said it will depend on how it was written. Summing up as I said in the reply: websocket in php will depend on how it was written and common http servers use Childs, which makes most people not have to wait (it’s configurable). :)

  • Thank you!!! Merry Christmas!

  • 1

    That one Ratchet It’s very complicated to implement. I tried it once, but I couldn’t. Maybe I’ll try it again when I need to use Websockets. Congratulations on the answer

  • 1

    Thank you @Theprohands, I would like to try to help you, tell me, your difficulty is with Composer or running the websocket on a Shared or VPS server?

  • @Guilhermenascimento I can’t remember very well! One day (almost 4 months ago) I was trying to implement Ratchet, but it seems that an error was played. The way I was doing this implementation seemed kind of narrow, obviously so it wouldn’t work. Ah! And I was trying to run Websocket on localhost:8080 to see if it worked. I’ve never actually been able to use Websockets :/ . I’ve tried using online servers too, but it didn’t work (like this: https://www.websocket.org/echo.html). When I use online servers Websocket does not send a message to the other window.

  • 1

    @Theprohands seems a specific mistake, if you try to do it again and have difficulty you can ask a question on the site that I will try to help, until more ;)

  • @Guilhermenascimento Apache is responsible for managing Websocket’s part as well or would be PHP itself?

  • 1

    @apache cat has no relation to Websockets, websockets are run separately, usually use a port of their own and in most cases PHP itself creates the socket the port or data is passed via a PORT on the server or a proxy-reverse. If the hosting does not have support for this (usually common hosting does not have) then it will be impossible to startar a websocket.

  • @Guilhermenascimento can you ask me a question? I sent you a message on Skype but I didn’t get back.

Show 10 more comments

Browser other questions tagged

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