Closed Orzubek-Rakhimov closed 1 month ago
Hi @Orzubek-Rakhimov
The "504 Gateway Timeout" implies an issue on server side. It typically occurs when request is initially handled by a proxy server, but the proxy doesn't get an answer from the actual server it delegates the request to.
Note that not all proxies support websocket requests. Nginx do support websocket, but as I understand (haven't used this setup myself) it need som extra configuration. To start with, some headers need to be included when forwarding as the handshake relies on these.
I'm not familiar with Rachet or setting up Nginx as a websocket proxy, but maybe someone else can provide some input.
It's all due to blocking code on the client side like
$message = $client->receive();
# or
$client->start()
i checked source code and find loops that didnt end and it dont let request end and triggered 504.
while ($this->running) {
try {
// Get streams with readable content
$readables = $this->streams->waitRead($this->timeout);
foreach ($readables as $key => $readable) {
try {
// Read from connection
if ($message = $this->connection->pullMessage()) {
$this->dispatch($message->getOpcode(), [$this, $this->connection, $message]);
}
} catch (MessageLevelInterface $e) {
// Error, but keep connection open
$this->logger->error("[client] {$e->getMessage()}");
$this->dispatch('error', [$this, $this->connection, $e]);
} catch (ConnectionLevelInterface $e) {
// Error, disconnect connection
$this->disconnect();
$this->logger->error("[client] {$e->getMessage()}");
$this->dispatch('error', [$this, $this->connection, $e]);
}
}
if (!$this->connection->isConnected()) {
$this->running = false;
}
$this->connection->tick();
$this->dispatch('tick', [$this]);
} catch (Exception $e) {
$this->disconnect();
$this->running = false;
// Low-level error
$this->logger->error("[client] {$e->getMessage()}");
$this->dispatch('error', [$this, null, $e]);
} catch (Throwable $e) {
$this->disconnect();
$this->running = false;
// Crash it
$this->logger->error("[client] {$e->getMessage()}");
$this->dispatch('error', [$this, null, $e]);
throw $e;
}
When I remove them from code all things work pretty good. Please let me i am wrong or not. Sorry for my english it is not my first language.
edit: without calling $client->start() messages dont send. BUt when i call start() it cause to 502
The start()
is for continuous subscription of messages sent by server, so it should be in a loop. To exit the loop, use close()
or stop()
depending on what you need.
The receive()
is to read a single message, but then you need to know if and how the server will respond. If it "hangs", it's because the server haven't responded - client is waiting for something to read.
The client do not trigger 504, that error is triggered elsewhere in your setup. Most likely a proxy.
I am using websocket-php for Client and cboden/ratchet for Server
Server side working nomrally but when i add this code to Client side index.php it causes to 504 Gateway Timeout (nginx). I've been struggling with this for two days, please help!
for Client side code :
for Server side code