nuwber / rabbitevents

Nuwber's RabbitEvents provides a simple observer implementation, allowing you to publishing and handling for various events that occur in your applications. For example, if you need to react to some event occurred in another API.
MIT License
121 stars 36 forks source link

There is a bug in payload transmission #33

Closed igor875126 closed 5 years ago

igor875126 commented 5 years ago

Hey, I've found a behavior that when you use not an wildcard listener the payload is given as string in the listener handle method, this string is the first element of payload array.

But if you wrap it in the additional array, then everything is working as expected, wildcard listeners tested too....

Example:

BroadcastEventServiceProvider

<?php

namespace App\Providers;

use App\Listeners\UserAuthenticatedEvent\UserAuthenticatedListener;

class BroadcastEventServiceProvider extends \Nuwber\Events\BroadcastEventServiceProvider
{
    protected $listen = [
        'gateway.user.authenticated' => [
            UserAuthenticatedListener::class
        ],
    ];
}

UserAuthenticatedListener

<?php

namespace App\Listeners\UserAuthenticatedEvent;

class UserAuthenticatedListener
{
    /**
     * This method is called when event happens
     * see app/Providers/BroadcastEventServiceProvider.php
     *
     * @param $payload
     */
    public function handle($payload)
    {
        dd($payload);
    }
}

Payload in rabbitmq message

{"event_id":"254","fingerprint":"c0feace7d38377bff938b590b3eb5e181d858381","user_agent":"PostmanRuntime\/7.17.1","user_id":11114022,"merchant_id":-5,"ip_address":"172.18.0.1"}

dd($payload)

"254"

With my fix - dd($payload)

array:6 [
  "event_id" => "254"
  "fingerprint" => "c0feace7d38377bff938b590b3eb5e181d858381"
  "user_agent" => "PostmanRuntime/7.17.1"
  "user_id" => 11114022
  "merchant_id" => -5
  "ip_address" => "172.18.0.1"
]

BTW: I am using PHP 7.3.9 + Laravel ^6.0 nuwber/rabbitevents 2.0 - same behavoir nuwber/rabbitevents 3.0-beta1 - same behavoir

masterjus commented 5 years ago

Hi @igor875126 I know that. It is mentioned in the documentation. You need to wrap your payload because it passes to a listener with call_user_func_array. Because of this, I've made new functionality with publish helper and trait. Please try it.

I'll be glad to get feedback from you if it works or not.

igor875126 commented 5 years ago

Hi @igor875126 I know that. It is mentioned in the documentation. You need to wrap your payload because it passes to a listener with call_user_func_array. Because of this, I've made new functionality with publish helper and trait. Please try it.

I'll be glad to get feedback from you if it works or not.

Hey,

thank you for your answer, unfortunately i cannot do this, because of microservice architecture and other libraries. I mean in the project where payload is beeing sent we are using another framework and another rabbit lib.

Please check my merge request, I am almost 100% sure that this will work with even not surrounded payloads.

masterjus commented 5 years ago

@igor875126 is this PR still actual?

igor875126 commented 5 years ago

@igor875126 is this PR still actual?

No, it's not actual any more. You can close it. Unfortunately, we are forced to fork your repository and continue developing to our needs.