Coder Social home page Coder Social logo

Comments (12)

Sevavietl avatar Sevavietl commented on June 10, 2024 3

I also don't see why to restrict body to string only.

There is no way you can send a php object as is.

I guess this is not true. The body can be JsonSerializable or an array and as the whole message will be encoded the body will be encoded too.

Using string for body results in double encoding and leads to much pain:)

There is no problem with ommiting return typehint of Message::getBody() in the interface and type-narrow it in the realisation.

from queue-interop.

makasim avatar makasim commented on June 10, 2024 1

I've given it another thought and now I tend to agree with you. The issue is clearly present.

Sure I'll review it!

Thank you all for your effort.

from queue-interop.

dexy86 avatar dexy86 commented on June 10, 2024 1

Hello guys
I also have the same question, why is body type string?
if i have a DTO object and i don't wan't it double encoded, how should i deal with that ?
the message that is the array which contains body, properties and headers (which i don't see the purpose of using in google pub/sub) is json_encoded before sending, so if i have a body value which is an object or an array i have to encode it or serialize it which is not optimal

e.g.

//don't look at the naming this is just to test
$dto = new StdClass();
$dto->id = 10;
$dto->computeId = 44;
$s = new StdClass();
$s->storeId = 4;
$s->stock = 11;
$dto->obj = $s;
// var_dump($dto);

$msg = ['body' => null, 'properties' => null, 'headers' => null];

$bodyMsg = json_encode($dto); //because body needs to be a string :/
$bodyMsgSerialized = serialize($dto); //serialized version

$msg['body'] = $bodyMsg;
echo "json encode bodyMsg: \n";
print_r(json_encode($msg));

$msg['body'] = $bodyMsgSerialized;
echo "\n\nphp serialize bodyMsgSerialized: \n";
print_r(json_encode($msg));

//but if body was a array/Obj = the suggested way
$msg['body'] = $dto;
echo "\n\narray/Obj example: \n";
print_r(json_encode($msg));

http://sandbox.onlinephpfunctions.com/code/61f3dd92697b40db568bbbd818959e9e493d57ae

suggestion would be to remove queue-interop as a dependency for php-enquque or fix it to use some config for message format/standard, so everyone could adjust for them how they need, cause unfortunately i can't see queue-interop to satisfy everyones needs :( (great idea though)

waoooooo 14days and still no response :D

from queue-interop.

makasim avatar makasim commented on June 10, 2024

You should serialize it, for example into json or xml. There is no way you can send a php object as is.

from queue-interop.

strider2038 avatar strider2038 commented on June 10, 2024

But it will be serialized twicely to send it via transport (redis, kafka, rabbitmq) - first time message body to string and second time message with serialized body into string. I think this is unnecessary overhead.

from queue-interop.

makasim avatar makasim commented on June 10, 2024

Why double?

Your code encodes a data to JSON so it becomes a string, then send it as the message body. In turn, on a consumer side, you get a string which your cod decode to your data structure.

The idea behind this decision is to keep encoding\decoding logic out of the scope of the Queue Interop.

from queue-interop.

Sevavietl avatar Sevavietl commented on June 10, 2024

@makasim thank you for the prompt answer.

Why double?

Because when you call json_encode on $body you get a JSON string. Then when you call json_encode on the whole message with the body as a string, the body gets double encode: you end up with escaped quotes:

$body = [
    'foo' => 'bar',
];

$message = [
    'body' => json_encode($body),    
];

var_dump(json_encode($message)); // => string(28) "{"body":"{\"foo\":\"bar\"}"}"

Ommiting string typehint does not couple encoding\decoding logic. As far as I can see the Queue Interop contains only interfaces and they contain no encoding\decoding logic with or without string. Please, correct me if I am wrong.

from queue-interop.

makasim avatar makasim commented on June 10, 2024

Ommiting string typehint does not couple encoding\decoding logic.

Most of the transports accept only a string. What should those transport do if something else given?

It would force us to introduce a kind of encoding\decoding interfaces so they can transform that data to a string. The other way is to throw an exception which is not interoperable (some transports would work but others dont)

from queue-interop.

Sevavietl avatar Sevavietl commented on June 10, 2024

It is hard to tell without wide domain knowledge. Can you, please, give me an example of transport where you can use only string for the body? Thank you in advance.

from queue-interop.

Sevavietl avatar Sevavietl commented on June 10, 2024

@makasim I have looked through the enqueue source code. And frankly speaking, I do not see any caveats of not using string body. Every transport implementation already accepts only message of specific type, so LSP doesn't hold there.

I can create fork for this repo and fork for enqueue, that will use it. Just as proof of concept. Will you consider to review them and continue discussion?

Thank you in advance.

from queue-interop.

Steveb-p avatar Steveb-p commented on June 10, 2024

@makasim can you look at #30 and, if there are no issues with it, merge it so we can work on it enqueue-dev?

from queue-interop.

dexy86 avatar dexy86 commented on June 10, 2024

Hello guys
I also have the same question, why is body type string?
if i have a DTO object and i don't wan't it double encoded, how should i deal with that ?
the message that is the array which contains body, properties and headers (which i don't see the purpose of using in google pub/sub) is json_encoded before sending, so if i have a body value which is an object or an array i have to encode it or serialize it which is not optimal

e.g.

//don't look at the naming this is just to test
$dto = new StdClass();
$dto->id = 10;
$dto->computeId = 44;
$s = new StdClass();
$s->storeId = 4;
$s->stock = 11;
$dto->obj = $s;
// var_dump($dto);

$msg = ['body' => null, 'properties' => null, 'headers' => null];

$bodyMsg = json_encode($dto); //because body needs to be a string :/
$bodyMsgSerialized = serialize($dto); //serialized version

$msg['body'] = $bodyMsg;
echo "json encode bodyMsg: \n";
print_r(json_encode($msg));

$msg['body'] = $bodyMsgSerialized;
echo "\n\nphp serialize bodyMsgSerialized: \n";
print_r(json_encode($msg));

//but if body was a array/Obj = the suggested way
$msg['body'] = $dto;
echo "\n\narray/Obj example: \n";
print_r(json_encode($msg));

http://sandbox.onlinephpfunctions.com/code/61f3dd92697b40db568bbbd818959e9e493d57ae

suggestion would be to remove queue-interop as a dependency for php-enquque or fix it to use some config for message format/standard, so everyone could adjust for them how they need, cause unfortunately i can't see queue-interop to satisfy everyones needs :( (great idea though)

from queue-interop.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.