Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Hellsingoff's avatar

Reverb, queue and Serialization of 'Closure' is not allowed

For events to function, a queue is required. I set up a connection to a queue in phpredis and everything works fine.

But Reverb, when a message comes in, tries to throw an event that includes a non-serializable object. Thus, if I have an active queue in my project, Reverb doesn't function and crashes with the error "Serialization of 'Closure' is not allowed".

And if I disable the queue, all other events don't work.

Why does reverb even include a non-serializable object and try to serialize it? How to use it?

Part of Laravel\Reverb\Protocols\Pusher\Server:

    public function message(Connection $from, string $message): void
    {
        Log::info('Message Received', $from->id());
        Log::message($message);

        $from->touch();

        try {
            $event = json_decode($message, associative: true, flags: JSON_THROW_ON_ERROR);

            match (Str::startsWith($event['event'], 'pusher:')) {
                true => $this->handler->handle(
                    $from,
                    $event['event'],
                    empty($event['data']) ? [] : $event['data'],
                    $event['channel'] ?? null
                ),
                default => ClientEvent::handle($from, $event)
            };

            Log::info('Message Handled', $from->id());

            MessageReceived::dispatch($from, $message);
        } catch (Exception $e) {
            $this->error($from, $e);
        }
    }

Connection object is non-serializable ($from var)

Error in reverb logs (on any message):

Connection Established ................................. 321271308.292145186  
  Message Received ....................................... 321271308.292145186  
  
   1▕ { 
   2▕     "event": "pusher:subscribe", 
   3▕     "data": { 
   4▕         "channel": "presence-App.Models.User.6", 
   5▕         "auth": "sgppljyqr9dt3gm9gmvu:6cae445c761f464694826ff3f19f54fe3ca1906445cbbf37b2e523f0abcd8c7e... 

  Broadcasting To ................................. presence-App.Models.User.6  
  
   1▕ { 
   2▕     "event": "pusher_internal:member_added", 
   3▕     "data": { 
   4▕         "user_id": "6", 
   5▕         "user_info": true 
   6▕     }, 
   7▕     "channel": "presence-App.Models.User.6" 
   8▕ } 

  Message Handled ........................................ 321271308.292145186  

 [ERROR] Message from 321271308.292145186 resulted in an unknown error          

  Serialization of 'Closure' is not allowed ..................................  

Client side logs

[log] [PUSHER_MESSAGE_RECEIVED]
  {"event":"pusher_internal:subscription_succeeded","data":"{\"presence\":{\"count\":1,\"ids\":[\"6\"],\"hash\":{\"6\":true}}}","channel":"presence-App.Models.User.6"}
[log] [PUSHER_CHANNEL_EVENT]
  channel: presence-App.Models.User.6
  event: pusher:subscription_succeeded
  data: {presence: {count: 1, ids: [6], hash: {6: true}}}
[log] [PUSHER_CHANNEL_SUBSCRIPTION_SUCCESS]
  channel: presence-App.Models.User.6
  data: {presence: {count: 1, ids: [6], hash: {6: true}}}
[log] [PUSHER_MESSAGE_RECEIVED]
  {"event":"pusher:error","data":"{\"code\":4200,\"message\":\"Invalid message format\"}"}
  1. Auth was ok
  2. Message was successfully received (both logs, server and client)
  3. Subscription was ok (both sides)
  4. After subscription MessageReceived::dispatch($from, $message); tries to serialize $from and throw an Exception
  5. Got an error (both, server and client)

If i disable the queue connection in ENV, the error disappears. If i enable any queue, the error remains.

BROADCAST_CONNECTION=reverb
FILESYSTEM_DISK=local
QUEUE_CONNECTION=redis

Caching and blocking works through the same connection with Redis, it definitely works. And the error does not reach it, serialization fails.

0 likes
1 reply
Hellsingoff's avatar

After REMOVING Connect from vendor class i got this:

Message Received ....................................... 379536143.158263648  
  
   1▕ { 
   2▕     "event": "client-Test", 
   3▕     "data": null, 
   4▕     "channel": "presence-App.Models.User.6" 
   5▕ } 

  Broadcasting To ................................. presence-App.Models.User.6  
  
   1▕ { 
   2▕     "event": "client-Test", 
   3▕     "data": null, 
   4▕     "channel": "presence-App.Models.User.6" 
   5▕ } 

  Message Handled ........................................ 379536143.158263648  

==> /var/log/supervisor/queue_worker.out.log <==
  2024-12-08 20:37:27 App\Infrastructure\Listeners\PlayerEnteredListener hzxYPE9bGjzbcLeRXbj0yQWTIB2RL6ez  RUNNING
  2024-12-08 20:37:27 App\Infrastructure\Listeners\PlayerEnteredListener hzxYPE9bGjzbcLeRXbj0yQWTIB2RL6ez  0.47ms DONE
  2024-12-08 20:37:27 App\Infrastructure\Listeners\PlayerEnteredListener 2UvyRyEAwBgNzOW2icbkmNzudfWf8c8W  RUNNING
  2024-12-08 20:37:27 App\Infrastructure\Listeners\PlayerEnteredListener 2UvyRyEAwBgNzOW2icbkmNzudfWf8c8W  0.39ms DONE

==> /var/log/supervisor/queue_worker.out.log <==
[2024-12-08 20:37:27] local.INFO: Message received! {"class":"Laravel\\Reverb\\Events\\MessageReceived","message":"{\"event\":\"client-Test\",\"data\":null,\"channel\":\"presence-App.Models.User.6\"}"} 
[2024-12-08 20:37:27] local.INFO: Message received! {"class":"Laravel\\Reverb\\Events\\MessageReceived","message":"{\"event\":\"client-Test\",\"data\":null,\"channel\":\"presence-App.Models.User.6\"}"} 

For some unknown reason, one message generates 2 events, but... The serialization error itself disappeared and everything worked. Why? What's wrong with the vendor class?

Please or to participate in this conversation.