mstdmstd's avatar

Outofmemory error trying debug MessageComponentInterface method vars

In my laravel 5.7 app I use MessageComponentInterface for chat app with a class like

<?php

namespace App\Classes\Socket;

use App\Classes\Socket\Base\BaseSocket;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class ChatSocket extends BaseSocket
{
    protected $clients;

    public function __construct()
    {
        $this->clients = new \SplObjectStorage;
    }

    public function onOpen(ConnectionInterface $conn) {
        $this->clients->attach($conn);
        echo '<pre>New Connection ::'.print_r($conn->resourceId,true).'</pre>';
    }

    public function onMessage(ConnectionInterface $from, $msg) {
        $numRecv= count($this->clients)-1;
         echo '<pre>onMessage $msg::' . print_r( $msg, true ) ;
//        var_dump($from);
        dump($from);
        die("-1 XXZ000");
        echo '<pre>onMessage $from::' . print_r( $from, true ) ;
        ...

The problem is that in onMessage event I want to this message write to db table, but I can not find where to get user_id of user who sent this message? I tried to debug outputing values to screen with

echo '<pre>onMessage $from::' . print_r( $from, true ) ;

But I got out of memory error, but in /etc/php/7.2/cli/php.ini I modified options:

max_execution_time = 3330
max_input_time = 240
memory_limit = 4048M
and restarted my server, but anyway I got outof memory error

using these methods:

//        var_dump($from);
        dump($from);
I got endless output in console and I could not catch anything...

How to debug these values ?

Thanks!

0 likes
3 replies
tpane24's avatar

Is the user Authorized? If so you could use the Auth facade to get their id like this: import Auth:

use Auth;

set $from to the user id:


$from = Auth::id();
mstdmstd's avatar

In file:///etc/php/7.2/cli/php.ini I modified

memory_limit = 8048M

and restarted apache

also I tried to check how many of memory in my script :

         echo '<pre>onMessage getNiceFileSize(memory_get_usage()) ::' . print_r( $this->getNiceFileSize(memory_get_usage()) , true ) ;
it shows :19241736 ~18 MiB 

error message :

 Out of memory (allocated 1623195648) (tried to allocate 1600131072 bytes)

I tried to calculated and got values 1.51 GiB and 1.49 GiB...

  1. why memory_get_usage returns such small value
  2. Why dumping gets 1.5 GiB? That is a big value
  3. Any ideas how to deal it?
  4. Auth::id() returns nothing and as far I understand it would not help, as message can be sent by some other logged user, but not currently logged...
mstdmstd's avatar

I still search for decision and I found this https://laravel.io/forum/01-16-2015-loading-laravels-session-using-ratchet article, byt when I tried to run it with method :

  public function onMessage(ConnectionInterface $from, $msg) {
       $from->session->start();
       $idUser = $from->session->get(Auth::getName());

I got error :

Undefined property: Ratchet\Server\IoConnection::$session

In the mentioned article there was a comment :

(you must decrypt cookie to get the session id in Laravel 5) :

I searche for this I found this

In web request context cookies are usually automatically encrypted and decrypted by the EncryptCookies middleware. 
So easiest option would be just to enable this middleware (and it's enabled by default in Laravel).

and in my app/Http/Kernel.php there is line with EncryptCookies

    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,

but that is web application, but I run console app. Can the reason of the error that I have to add EncryptCookies to some other group in app/Http/Kernel.php ?

Or why error ?

Please or to participate in this conversation.